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 com.mq.mylibrary.R;
import com.mq.mylibrary.tool.DateTool;
import com.mq.mylibrary.tool.LogTool;

import java.util.Calendar;

/**
 * 创建人 mQ
 * 创建时间 2019/8/23 0023 15:10
 * 文件名称 StartEndDateDialog
 * 说明:
 **/
public class StartEndDateDialog {

    private Context mContext;
    private AlertDialog mDialog;
    private DatePicker mDpStart;
    private DatePicker mDpEnd;
    private DateCallBack mCallBack;
    private int startYear;
    private int startMonth;
    private int startDay;
    private int endYear;
    private int endMonth;
    private int endDay;

    public StartEndDateDialog(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) {
//                        String startDate = getDateStr(startYear, startMonth, startDay);
//                        String endDate = getDateStr(endYear, endMonth, endDay);
//                        if (mCallBack != null)
//                            mCallBack.onDateResultListener(startDate, endDate);
//                    }
//                });


        mDialog = new AlertDialog.Builder(mContext)
                .setCancelable(false)
                .setView(getView())
                .setNegativeButton("取消", null)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dismiss();
                        String startDate = getDateStr(startYear, startMonth, startDay);
                        String endDate = getDateStr(endYear, endMonth, endDay);
                        if (mCallBack != null)
                            mCallBack.onDateResultListener(startDate, endDate);
                    }
                }).create();
    }

    /**
     * 获取自定义布局
     */
    private View getView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_start_end_date, null, false);
        mDpStart = (DatePicker) view.findViewById(R.id.dp_start);
        mDpEnd = (DatePicker) view.findViewById(R.id.dp_end);
        setDateView();
        setDateView();
        return view;
    }

    private void setDateView() {
        Calendar calendar = Calendar.getInstance();
        startYear = calendar.get(Calendar.YEAR);
        startMonth = calendar.get(Calendar.MONTH);
        startDay = calendar.get(Calendar.DAY_OF_MONTH);
        mDpStart.init(
                startYear,
                startMonth,
                startDay,
                mDateChangedListener);

        endYear = startYear;
        endMonth = startMonth;
        endDay = startDay;
        mDpEnd.init(
                endYear,
                endMonth,
                endDay,
                mDateChangedListener);
    }

    private DatePicker.OnDateChangedListener mDateChangedListener = new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            int id = view.getId();
            String tag = "";
            if (id == R.id.dp_start) {
                startYear = year;
                startMonth = monthOfYear;
                startDay = dayOfMonth;
                String start = getDateStr(startYear, startMonth, startDay);
                long startDate = DateTool.getStamp(start, DateTool.YTD_DATE);
                String end = getDateStr(endYear, endMonth, endDay);
                long endDate = DateTool.getStamp(end, DateTool.YTD_DATE);
                if (startDate > endDate) {
                    mDpEnd.init(year, monthOfYear, dayOfMonth, mDateChangedListener);
                    endYear = startYear;
                    endMonth = startMonth;
                    endDay = startDay;
                }
                tag = "选择开始日期: ";
            } else if (id == R.id.dp_end) {
                endYear = year;
                endMonth = monthOfYear;
                endDay = dayOfMonth;
                String start = getDateStr(startYear, startMonth, startDay);
                long startDate = DateTool.getStamp(start, DateTool.YTD_DATE);
                String end = getDateStr(endYear, endMonth, endDay);
                long endDate = DateTool.getStamp(end, DateTool.YTD_DATE);
                if (endDate < startDate) {
                    mDpStart.init(year, monthOfYear, dayOfMonth, mDateChangedListener);
                    startYear = endYear;
                    startMonth = endMonth;
                    startDay = endDay;
                }
                tag = "选择结束日期: ";
            }
            LogTool.i(tag + getDateStr(year, monthOfYear, dayOfMonth));
        }
    };

    private String getDateStr(int year, int monthOfYear, int dayOfMonth) {
        int month = monthOfYear + 1;
        return year + "-" + (month < 10 ? "0" + month : month) + "-" +
                (dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth);
    }

    public StartEndDateDialog setCallBack(DateCallBack callBack) {
        mCallBack = callBack;
        return this;
    }

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

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

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

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

    public interface DateCallBack {
        public void onDateResultListener(String startDate, String endDate);
    }

}
