package com.unis.sxzcl.widget.menu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.unis.sxzcl.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;


/**
 * 包名: com.unis.sxzcl.widget.menu
 * 类名: MenuDateTimeView
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2017/10/23
 * 更新人:
 * 更新时间: 2020/11/26 15:41
 * 更新说明:
 * 版本号: 1.0
 */
public class MenuDateTimeView implements View.OnClickListener {

    private Context mContext;
    private View dateTimeView;
    private DatePicker menu_date;
    private TimePicker menu_time;
    private TextView tvTimeTitle;
    private String viewTag;
    private DateTimeCakkBack dateTimeCakkBack;

    public MenuDateTimeView(Context mContext, String viewTag) {
        this.mContext = mContext;
        this.viewTag = viewTag;
        init();
    }

    private void init() {
        dateTimeView = LayoutInflater.from(mContext).inflate(R.layout.menu_date_time_layout, null);
        menu_date = dateTimeView.findViewById(R.id.menu_date);
        menu_time = dateTimeView.findViewById(R.id.menu_time);
        tvTimeTitle = dateTimeView.findViewById(R.id.tv_time_title);
        menu_time.setIs24HourView(true);
        dateTimeView.findViewById(R.id.menu_enter).setOnClickListener(this);
        dateTimeView.findViewById(R.id.menu_clear).setOnClickListener(this);
    }

    public MenuDateTimeView showTimePicker(boolean show) {
        if (show) {
            menu_time.setVisibility(View.VISIBLE);
            tvTimeTitle.setVisibility(View.VISIBLE);
        } else {
            menu_time.setVisibility(View.GONE);
            tvTimeTitle.setVisibility(View.GONE);
        }
        return this;
    }

    //获取控件
    public View getView() {
        return dateTimeView;
    }

    //设置监听
    public MenuDateTimeView setOnDateTimeListener(DateTimeCakkBack listener) {
        this.dateTimeCakkBack = listener;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_enter:
                //时间选择完成
                if (dateTimeCakkBack != null) {
                    Calendar calendar = Calendar.getInstance();
                    String patten;
                    if (menu_time.getVisibility() == View.VISIBLE) {
                        patten = "yyyy-MM-dd hh:mm:ss";
                    } else {
                        patten = "yyyy-MM-dd";
                    }
                    calendar.set(menu_date.getYear(), menu_date.getMonth(), menu_date.getDayOfMonth(),
                            menu_time.getCurrentHour(), menu_time.getCurrentMinute());
                    String result = new SimpleDateFormat(patten).format(calendar.getTime());

                    dateTimeCakkBack.onDateTime(viewTag, result, menu_date.getYear(), menu_date.getMonth(),
                            menu_date.getDayOfMonth(), menu_time.getCurrentHour(), menu_time.getCurrentMinute());
                }
                break;
            case R.id.menu_clear:
                //重置时间标题
                if (dateTimeCakkBack != null)
                    dateTimeCakkBack.onDateTimeClear(viewTag);
                break;
        }
    }

    public interface DateTimeCakkBack {
        void onDateTime(String viewTag, String result, int year, int month, int day, int hour, int minute);

        void onDateTimeClear(String viewTag);
    }
}
