/**
 * 日期选择器 Helper
 * 从 EZPlayBackListActivity 拆分出来，负责顶部日历点击事件、日期选择弹窗、日期变更通知等逻辑
 */
package com.videogo.ui.playback.helper;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.DatePicker;

import com.videogo.ui.common.EZBusinessTool;
import com.videogo.ui.playback.EZPlayBackListActivity;
import com.videogo.util.LogUtil;
import com.videogo.util.Utils;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;

import ezviz.ezopensdk.R;

/**
 * 日期选择器 Helper
 * 管理顶部日历按钮点击、日期选择弹窗、日期变更后触发录像重新查询
 * <p>
 * 直接持有 Activity 引用，生命周期与 Activity 一致
 */
public class RecordDateHelper {

    private static final String TAG = "RecordDateHelper";

    private final EZPlayBackListActivity mActivity;// Activity 引用
    public boolean isDateSelected = false;// 是否为选择日期事件

    public RecordDateHelper(EZPlayBackListActivity activity) {
        this.mActivity = activity;
        initView();
    }

    /**
     * 初始化日期选择器相关视图
     */
    private void initView() {
        // 标题栏日历时间选择事件
        View.OnClickListener goToCalendarClickListener = v -> {
            if (EZBusinessTool.getMinDate() != null && new Date().before(EZBusinessTool.getMinDate())) {
                Utils.showToast(mActivity, R.string.calendar_setting_error);
                return;
            }
            showDatePicker();
        };
        mActivity.mPortraitTitleBar.addTitleButton(R.drawable.remote_cal_selector, goToCalendarClickListener);
        mActivity.mPortraitTitleBar.setOnTitleClickListener(goToCalendarClickListener);
    }

    // region --- 公开方法 ---

    /**
     * 显示日期选择器弹窗
     */
    public void showDatePicker() {
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTime(mActivity.queryDate);
        DatePickerDialog dpd = new DatePickerDialog(mActivity, null, nowCalendar.get(Calendar.YEAR),
                nowCalendar.get(Calendar.MONTH), nowCalendar.get(Calendar.DAY_OF_MONTH));

        dpd.setCancelable(true);
        dpd.setTitle(R.string.select_date);
        dpd.setCanceledOnTouchOutside(true);
        dpd.setButton(DialogInterface.BUTTON_POSITIVE, mActivity.getString(R.string.certain), (dg, which) -> {
            DatePicker dp = null;
            Field[] fields = dg.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (field.getName().equals("mDatePicker")) {
                    try {
                        dp = (DatePicker) field.get(dg);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (dp != null) {
                dp.clearFocus();
                Calendar selectCalendar = Calendar.getInstance();
                selectCalendar.set(Calendar.YEAR, dp.getYear());
                selectCalendar.set(Calendar.MONTH, dp.getMonth());
                selectCalendar.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
                isDateSelected = true;
                mActivity.queryDate = selectCalendar.getTime();
                mActivity.mRecordQueryHelper.onDateChanged();
            }
        });
        dpd.setButton(DialogInterface.BUTTON_NEGATIVE, mActivity.getString(R.string.cancel), (dialog, which) -> {
            LogUtil.d("Picker", "Cancel!");
            if (!mActivity.isFinishing()) {
                dialog.dismiss();
            }

        });
        dpd.show();
    }

    // endregion
}
