package com.mq.mylibrary.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.mq.mylibrary.R;
import com.mq.mylibrary.tool.DateTool;
import com.mq.mylibrary.tool.LogTool;
import com.mq.mylibrary.tool.StringTool;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 创建人 mQ
 * 创建时间 2019/8/9 0009 17:50
 * 文件名称 DateTimeDialog
 * 说明:
 **/
public class DateTimeDialog {

    private Context mContext;
    private AlertDialog mDialog;
    private DatePicker mViewDate;
    private TimePicker mViewTime;
    private DateTimeCallBack mCallBack;
    private View mView;
    private String mDate;
    private String mTime;

    public DateTimeDialog(Context context) {
        mContext = context;
        initDialog();
    }

    private void initDialog() {
//        mDialog = new SystemDialog(mContext)
//                .setCancelable(false)
//                .setHideTitle()
//                .setContentView(getView())
//                .setLeftButton("取消", null)
//                .setRightButton("确认", new SystemDialog.RightCallBack() {
//                    @Override
//                    public void onRightListener(SystemDialog dialog) {
//                        dismiss();
//                        if (mCallBack != null)
//                            mCallBack.onDateTimeResultListener(mView, mDate, mTime);
//                    }
//                });
        mDialog = new AlertDialog.Builder(mContext)
                .setCancelable(false)
                .setView(getView())
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                        if (mCallBack != null)
                            mCallBack.onDateTimeResultListener(mView, mDate, mTime);
                    }
                }).create();
    }

    /**
     * 获取自定义布局
     */
    private View getView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_date_time, null, false);
        mViewDate = (DatePicker) view.findViewById(R.id.view_date);
        mViewTime = (TimePicker) view.findViewById(R.id.view_time);
        mViewTime.setIs24HourView(true);
        mViewTime.setOnTimeChangedListener(mTimeChangedListener);
        return view;
    }

    private void setTimeView(Calendar calendar) {
        mViewDate.init(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                mDateChangedListener);

        mViewTime.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        mViewTime.setCurrentMinute(calendar.get(Calendar.MINUTE));
    }

    private DatePicker.OnDateChangedListener mDateChangedListener = new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            int month = monthOfYear + 1;
            String date = year + "-" + (month < 10 ? "0" + month : month) + "-" +
                    (dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth);
            LogTool.i("选择日期:" + date);
            mDate = date;
        }
    };

    private TimePicker.OnTimeChangedListener mTimeChangedListener = new TimePicker.OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            String time = getTime(hourOfDay, minute);
            LogTool.i("选择时间:" + time);
            mTime = time;
        }
    };


    private String getTime(int hourOfDay, int minute) {
        DecimalFormat df = new DecimalFormat("00");
        String mHour = df.format(hourOfDay);
        String mMinute = df.format(minute);
        return mHour + ":" + mMinute + ":00";
    }

    /**
     * 设置显示时间
     */
    public DateTimeDialog setTime(View view, String dateTime) {
        mView = view;
        if (StringTool.notEmpty(dateTime)) {
            Calendar calendar = Calendar.getInstance();
            LogTool.i("传入时间:" + dateTime);
//            if (mViewDate.getVisibility() == View.VISIBLE) {
            Long timeLong = DateTool.getStamp(dateTime, DateTool.FULL_DATE);
            LogTool.i("转成毫秒:" + timeLong);
            if (timeLong == null)
                return this;
            Date date = new Date(timeLong*1000);
            LogTool.i("转成时间:" + date.toString());
            calendar.setTime(date);

            String[] s = dateTime.split(" ");
            mDate = s[0];
            mTime = s[1];
//            } else {
//                mTime = dateTime;
//            }
            setTimeView(calendar);

        }
        return this;
    }

    public DateTimeDialog setCallBack(DateTimeCallBack callBack) {
        mCallBack = callBack;
        return this;
    }

    /**
     * 设置标题
     */
    public DateTimeDialog setTiele(String title) {
        mDialog.setTitle(title);
        return this;
    }

    /**
     * 显示对话框
     */
    public DateTimeDialog show() {
        mDialog.show();
        return this;
    }

    /**
     * 隐藏对话框
     */
    public DateTimeDialog dismiss() {
        mDialog.dismiss();
        return this;
    }

    /**
     * 获取对话框
     */
    public AlertDialog getDialog() {
        return mDialog;
    }

    public interface DateTimeCallBack {
        public void onDateTimeResultListener(View view, String date, String time);
    }

}
