package com.abbidot.tracker.dialog

import android.content.Context
import android.view.View
import android.widget.NumberPicker
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.databinding.DialogYearAndMonthLayoutBinding
import com.abbidot.tracker.util.ViewUtil
import com.haibin.calendarview.CalendarView
import com.hjq.toast.Toaster
import java.util.Calendar


/**
 *Created  by .yzq on 2022/1/7/007.
 * @link
 * @description:显示日历弹窗
 * @param canSelectFutureDate 平时正常日期，true不能选择未来时间
 */
class ShowYearMonthDialog(
    context: Context,
    calendarView: CalendarView,
    canSelectFutureDate: Boolean = false,
    minYear: Int = 2023
) : BaseDialog<DialogYearAndMonthLayoutBinding>(DialogYearAndMonthLayoutBinding::inflate, context),
    NumberPicker.OnValueChangeListener {

    private val mCalendarView = calendarView
    private lateinit var mMonths: Array<String>

    private lateinit var mCalendar: Calendar
    private val mCanSelectFutureDate = canSelectFutureDate
    private val mMinYear = minYear

    override fun initView() {
//        mMonths=context.resources.getStringArray(R.array.month_string_array)
        mMonths = context.resources.getStringArray(R.array.array_month)

        mCalendar = Calendar.getInstance()
        val cYear = mCalendar.get(Calendar.YEAR)
        val month = if (mCanSelectFutureDate) {
            12
        } else mCalendar.get(Calendar.MONTH) + 1

        mViewBinding.apply {

            npDialogYear.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    context,
                    it,
                    mMinYear,
                    cYear,
                    cYear,
                    16,
                    valueChangeListener = this@ShowYearMonthDialog
                )
            }

            npDialogMonth.let {
                ViewUtil.instance.setNumberPickerAttributes(
                    context, it, 1, month, month, 16, valueChangeListener = this@ShowYearMonthDialog
                )
                it.displayedValues = mMonths
            }

            setOnClickListenerViews(btnSelectYearMonthOk, btnSelectYearMonthCancel)
        }
    }

    override fun onClick(v: View) {
        mViewBinding.apply {
            when (v) {
                btnSelectYearMonthCancel -> dismiss()
                btnSelectYearMonthOk -> {
                    val year = npDialogYear.value
                    val month = npDialogMonth.value
                    mCalendarView.minRangeCalendar.let {
                        //确定后只能选择最小的年月日
                        if (it.year >= year) {
                            if (it.month == month) {
                                mCalendarView.scrollToCalendar(it.year, it.month, it.day, true)
                            } else if (it.month > month) {
                                Toaster.show(R.string.txt_time_out_of_range)
                                mCalendarView.scrollToCalendar(it.year, it.month, it.day, true)
                            } else mCalendarView.scrollToCalendar(year, month, 1, true)
                        } else {
                            mCalendarView.scrollToCalendar(year, month, 1, true)
                        }
                    }
                    dismiss()
                }
            }
        }
    }

    override fun onValueChange(picker: NumberPicker, oldVal: Int, newVal: Int) {
        mViewBinding.apply {
            when (picker) {
                npDialogYear -> {
                    if (!mCanSelectFutureDate) {
                        val month = mCalendar.get(Calendar.MONTH) + 1 // 月份是从0开始的
                        val monthMax = if (newVal == mCalendar.get(Calendar.YEAR)) {
                            month
                        } else {
                            12
                        }
                        npDialogMonth.maxValue = monthMax
                    }
                }
            }
        }
    }
}