package com.abbidot.tracker.dialog

import android.content.Context
import android.view.View
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.databinding.DialogCalenderLayoutBinding
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.hjq.toast.Toaster

/**
 *Created  by .yzq on 2022/1/7/007.
 * @link
 * @description:显示日历弹窗
 * @param canSelectFutureDate 平时正常日期，true不能选择未来时间
 * @param minLastDayRange 最小天数能选最近多少天，0表示没有限制
 */
class ShowCalenderDialog(
    context: Context,
    onSelectListener: OnDialogSelectListener? = null,
    canSelectFutureDate: Boolean = false,
    calenderShowTimestamp: Long = 0,
    minYear: Int = 0, minLastDayRange: Int = 0,
) : BaseDialog<DialogCalenderLayoutBinding>(DialogCalenderLayoutBinding::inflate, context) {

    private val mCanSelectFutureDate = canSelectFutureDate
    private val mOnSelectListener = onSelectListener
    private val mCalenderShowTimestamp = calenderShowTimestamp

    private var mShowYearMonthDialog: ShowYearMonthDialog? = null
    private lateinit var mMonths: Array<String>
    private var mShowYear = 0
    private var mShowMonth = 0
    private var mMinYear = minYear
    private val mMinLastDayRange = minLastDayRange

    override fun initView() {
        mMonths = context.resources.getStringArray(R.array.array_month)
        mViewBinding.apply {
            cvShowSelectCalender.let {
                mShowYear = it.curYear
                if (mMinYear == 0) mMinYear = mShowYear - 1
                mShowMonth = it.curMonth
                if (!mCanSelectFutureDate) {
                    val calendar = java.util.Calendar.getInstance()
                    val cYear = calendar.get(java.util.Calendar.YEAR)
                    val month = calendar.get(java.util.Calendar.MONTH) + 1
                    val day = calendar.get(java.util.Calendar.DAY_OF_MONTH)
                    if (mMinLastDayRange > 0) {
                        val minTimestamp = Utils.getBeforeHowTimestamp(
                            calendar.timeInMillis, mMinLastDayRange.toLong()
                        )
                        val minDate =
                            Utils.formatTime(minTimestamp, Utils.DATE_FORMAT_PATTERN_CN).split("-")
                        it.setRange(
                            minDate[0].toInt(),
                            minDate[1].toInt(),
                            minDate[2].toInt(),
                            cYear,
                            month,
                            day
                        )
                    } else {
                        it.setRange(mMinYear, 1, 1, cYear, month, day)
                    }
                }
                updateMonthYear()

                it.setOnMonthChangeListener { year, month ->
                    mShowMonth = month
                    mShowYear = year
                    updateMonthYear()
                }

                it.setOnCalendarSelectListener(object : CalendarView.OnCalendarSelectListener {
                    override fun onCalendarOutOfRange(calendar: Calendar?) {
                        Toaster.show(R.string.txt_time_out_of_range)
                    }

                    override fun onCalendarSelect(calendar: Calendar, isClick: Boolean) {
                        if (!isClick) return
                        mOnSelectListener?.onSelectClick(this@ShowCalenderDialog, calendar)
                        dismiss()
                    }
                })

                //根据传入的时间戳进行还原显示
                setSelectDate(mCalenderShowTimestamp)
            }

            setOnClickListenerViews(
                ivSelectCalendarMonthLeft, ivSelectCalendarMonthRight, tvSelectCalendarYearMonth
            )
        }
    }

    /**
     * 根据传入的时间戳进行还原显示
     * @param showTimestamp 13位时间戳
     */
    private fun setSelectDate(showTimestamp: Long) {
        if (showTimestamp > 0) {
            val calendar = java.util.Calendar.getInstance().apply {
                timeInMillis = showTimestamp
            }
            mViewBinding.cvShowSelectCalender.scrollToCalendar(
                calendar.get(java.util.Calendar.YEAR),
                calendar.get(java.util.Calendar.MONTH) + 1,
                calendar.get(java.util.Calendar.DAY_OF_MONTH)
            )
        }
    }

    private fun updateMonthYear() {
        val curMonthYear = "${mMonths[mShowMonth - 1]} $mShowYear"
        mViewBinding.tvSelectCalendarYearMonth.text = curMonthYear
    }


    override fun onClick(v: View) {
        mViewBinding.apply {
            when (v) {
                ivSelectCalendarMonthRight -> {
                    cvShowSelectCalender.scrollToNext(true)
                }

                ivSelectCalendarMonthLeft -> {
                    cvShowSelectCalender.scrollToPre(true)
                }

                tvSelectCalendarYearMonth -> {
                    if (null == mShowYearMonthDialog) {
                        mShowYearMonthDialog = ShowYearMonthDialog(
                            context, cvShowSelectCalender, minYear = mMinYear
                        )
                    }
                    mShowYearMonthDialog?.show()
                }
            }
        }
    }
}