package com.rehome.meetingbook.weiget;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;


import com.rehome.meetingbook.R;

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


/**
 * Create By HuangWenFei
 * 创建日期：2022-12-09 15:51
 * 描述：时间日期选择器
 */
public class DateTimeMinutePickDialog extends Dialog implements View.OnClickListener, DatePicker.OnDateChangedListener,
        TimePicker.OnTimeChangedListener {

    private final TextView tv_dialog_title;
    private TextView tv_dialog_msg;
    private final TextView tv_dialog_commit;
    private final TextView tv_dialog_cancel;
    private final View lay_dialog_title;
    private final View lay_dialog_cancel;
    private View lay_dialog_commit;
    private final DateTimeMinutePickDialog.CommitClickListener commitClickListener;
    private final DatePicker datePicker;
    private final TimePicker timePicker;
    private String dateTime, outPutDateTime, outPutDateTime1, outPutDateTime2;
    private Calendar calendar = Calendar.getInstance();
    private final Calendar nowCalendar = Calendar.getInstance();//现在时间
    private Calendar tempCalendar = Calendar.getInstance();//历史时间


    private final SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
    private final SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private final SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
    private final SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DateTimeMinutePickDialog(Context context, DateTimeMinutePickDialog.CommitClickListener commitClickListener) {
        super(context);
        this.commitClickListener = commitClickListener;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setContentView(R.layout.layout_datetime_dialog);

        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (getScreenWidth(context)) * 2 / 5;
        window.setGravity(Gravity.CENTER);

        lay_dialog_title = findViewById(R.id.lay_dialog_title);
        tv_dialog_title = findViewById(R.id.dialog_title);

        lay_dialog_cancel = findViewById(R.id.lay_dialog_cancel);
//        lay_dialog_commit = findViewById(R.id.lay_dialog_commit);

        tv_dialog_cancel = findViewById(R.id.dialog_cancel);
        tv_dialog_commit = findViewById(R.id.dialog_commit);

        datePicker = findViewById(R.id.datePicker);
        timePicker = findViewById(R.id.timePicker);

        if (calendar != null) {
            initDateTime(datePicker, timePicker, calendar);
        }

        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(this);
        tv_dialog_cancel.setOnClickListener(this);
        tv_dialog_commit.setOnClickListener(this);
    }

    public DateTimeMinutePickDialog(Context context, Calendar calendar,boolean isStartTime,DateTimeMinutePickDialog.CommitClickListener commitClickListener) {
        super(context);
        this.commitClickListener = commitClickListener;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setContentView(R.layout.layout_datetime_dialog);

        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (getScreenWidth(context)) * 2 / 5;
        window.setGravity(Gravity.CENTER);

        lay_dialog_title = findViewById(R.id.lay_dialog_title);
        tv_dialog_title = findViewById(R.id.dialog_title);

        lay_dialog_cancel = findViewById(R.id.lay_dialog_cancel);
//        lay_dialog_commit = findViewById(R.id.lay_dialog_commit);

        tv_dialog_cancel = findViewById(R.id.dialog_cancel);
        tv_dialog_commit = findViewById(R.id.dialog_commit);

        datePicker = findViewById(R.id.datePicker);
        timePicker = findViewById(R.id.timePicker);

        this.calendar=calendar;
        if (calendar != null) {
            if(isStartTime){
                initDateTime(datePicker, timePicker, calendar,true);
            }else{
                initDateTime(datePicker, timePicker, calendar,false);
            }
        }

        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(this);
        tv_dialog_cancel.setOnClickListener(this);
        tv_dialog_commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_cancel:
                dismiss();
                break;
            case R.id.dialog_commit:
                commitClickListener.confirm(outPutDateTime, outPutDateTime1,outPutDateTime2,calendar);
                dismiss();
                break;
        }
    }

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        onDateChanged(null, 0, 0, 0);
    }

    /**
     * 时间改变时候回调
     *
     * @param view
     * @param year        年
     * @param monthOfYear 月
     * @param dayOfMonth  日
     */
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
        tempCalendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
                timePicker.getCurrentMinute(),0);

        /**
         * 如果当前时间大于选择的时间，就重置时间
         */
//        if (nowCalendar.getTimeInMillis() > tempCalendar.getTimeInMillis()) {
//            changeDateTime(datePicker, timePicker, nowCalendar);
//        } else {
        calendar = tempCalendar;
        dateTime = simpleDateFormat1.format(calendar.getTime());
        tv_dialog_title.setText(dateTime);
        outPutDateTime = simpleDateFormat2.format(calendar.getTime());
        outPutDateTime1 = simpleDateFormat3.format(calendar.getTime());
        outPutDateTime2 = simpleDateFormat4.format(calendar.getTime());
//        }
    }

    /**
     * 改变时间
     *
     * @param datePicker 日期选择控件
     * @param timePicker 时间选择控件
     * @param calendar   日历类
     */
    private void changeDateTime(DatePicker datePicker, TimePicker timePicker, Calendar calendar) {
        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), this);
        //设置不显示年
        if (datePicker != null) {
            ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
        }
        tv_dialog_title.setText(simpleDateFormat1.format(calendar.getTime()));

        //设置显示时分
        timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
    }

    /**
     * 初始化时间
     *
     * @param datePicker 日期选择控件
     * @param timePicker 时间选择控件
     * @param calendar   日历类
     */
    private void initDateTime(DatePicker datePicker, TimePicker timePicker, Calendar calendar) {
//        calendar.add(Calendar.DATE, 1);
        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), this);
//        设置不显示年
        if (datePicker != null) {
            ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
        }

        calendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
                timePicker.getCurrentMinute(),0);


        tv_dialog_title.setText(simpleDateFormat1.format(calendar.getTime()));
        //设置显示时分
        timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
        tempCalendar = calendar;
        outPutDateTime = simpleDateFormat2.format(calendar.getTime());
        outPutDateTime1 = simpleDateFormat3.format(calendar.getTime());
        outPutDateTime2 = simpleDateFormat4.format(calendar.getTime());
    }

    /**
     * 初始化时间
     *
     * @param datePicker 日期选择控件
     * @param timePicker 时间选择控件
     * @param calendar   日历类
     */
    private void initDateTime(DatePicker datePicker, TimePicker timePicker, Calendar calendar,boolean isStartTime) {
//        calendar.add(Calendar.DATE, 1);
        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), this);
//        设置不显示年
        if (datePicker != null) {
            ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
        }

        if(isStartTime){
            calendar.set(datePicker.getYear(), datePicker.getMonth(),
                    datePicker.getDayOfMonth(), 9,
                    0,0);
        }else{
            calendar.set(datePicker.getYear(), datePicker.getMonth(),
                    datePicker.getDayOfMonth(), 16,
                    0,0);
        }


        tv_dialog_title.setText(simpleDateFormat1.format(calendar.getTime()));
        //设置显示时分
        timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
        tempCalendar = calendar;
        outPutDateTime = simpleDateFormat2.format(calendar.getTime());
        outPutDateTime1 = simpleDateFormat3.format(calendar.getTime());
        outPutDateTime2 = simpleDateFormat4.format(calendar.getTime());
    }

    public interface CommitClickListener {
        /**
         * @param outPutDate  精确到分
         * @param outPutDate1 精确到天
         * @param outPutDate2 精确到秒
         */
        void confirm(String outPutDate, String outPutDate1, String outPutDate2,Calendar calendarCallBack);
    }

    //获取屏幕宽度
    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();
    }

}