package com.example.base.dialog

import android.app.Dialog
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CalendarView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import com.example.base.R
import com.example.base.extensions.getYYYYMM2
import com.example.base.extensions.getYYYYMMDD2
import com.example.base.extensions.onClick
import com.example.base.widget.MonthView
import org.jetbrains.anko.find
import java.util.Calendar

/**
 * 这是一个日期选择控件
 */

class DateDialogFragment : DialogFragment() {

    annotation class DateMode {
        companion object {
            const val NONE = -1
            const val YEAR_MONTH_DAY = 0
            const val YEAR_MONTH = 1
            const val MONTH_DAY = 2
        }
    }

    private var mCurrentTime: Long = 0
    private var mStartDate: Long = 0
    private var mEndDate: Long = 0
    private var mDateMode: Int = 0

    private var mOnSelectedListener: ((time: Long) -> Unit)? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.shape_white_cor8))
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        mCurrentTime = arguments?.getLong(ARG_TIME, System.currentTimeMillis()) ?: 0
        mStartDate = arguments?.getLong(START_DATE, 0)!!
        mEndDate = arguments?.getLong(END_DATE, System.currentTimeMillis())!!
        mDateMode = arguments?.getInt(DATE_MODE, DateMode.YEAR_MONTH_DAY)!!


        val v = View.inflate(context, R.layout.mvvm_dialog_date, null)
        initView(v)

        v.find<TextView>(R.id.tv_ok).onClick {
            mOnSelectedListener?.invoke(mCurrentTime)
            dismiss()
        }
        v.find<TextView>(R.id.tv_cancel).onClick {
            dismiss()
        }

        val mDialog = AlertDialog.Builder(requireContext())
        mDialog.setView(v)
        return mDialog.create()
    }

    override fun onStart() {
        super.onStart()
        val dm = DisplayMetrics()
        requireActivity().windowManager.defaultDisplay.getMetrics(dm)
        dialog!!.window!!.setLayout(
            (dm.widthPixels * 0.8).toInt(),
            dialog!!.window!!.attributes.height
        )
    }

    fun setOnSelectedListener(listener: ((time: Long) -> Unit)) {
        mOnSelectedListener = listener
    }

    private fun initView(view: View) {

        val tvTime = view.findViewById<TextView>(R.id.tvTime)
        val calendarView = view.findViewById<CalendarView>(R.id.calendarView)
        val monthView = view.findViewById<MonthView>(R.id.monthView)

        val calendar = Calendar.getInstance()
        calendar.timeInMillis = mCurrentTime
        if (mDateMode == DateMode.YEAR_MONTH) {
            calendarView.visibility = View.GONE
            monthView.visibility = View.VISIBLE
            tvTime.text = mCurrentTime.getYYYYMM2()
            monthView.setRange(mStartDate, mEndDate)
            monthView.setDate(mCurrentTime)
            monthView.setOnMonthSelectedListener { year, month ->
                calendar.set(Calendar.YEAR, year)
                calendar.set(Calendar.MONTH, month)
                calendar.set(Calendar.DAY_OF_MONTH, 1)
                mCurrentTime = calendar.time.time
                tvTime.text = mCurrentTime.getYYYYMM2()
            }
        } else {
            calendarView.visibility = View.VISIBLE
            monthView.visibility = View.GONE
            tvTime.text = mCurrentTime.getYYYYMMDD2()
            calendarView.minDate = mStartDate
            calendarView.maxDate = mEndDate
            calendarView.date = mCurrentTime
            calendarView.firstDayOfWeek = Calendar.MONDAY
            calendarView.setOnDateChangeListener { _, year, month, dayOfMonth ->
                calendar.set(Calendar.YEAR, year)
                calendar.set(Calendar.MONTH, month)
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
                mCurrentTime = calendar.time.time
                tvTime.text = mCurrentTime.getYYYYMMDD2()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mOnSelectedListener = null
    }

    companion object {
        private const val ARG_TIME = "time"
        private const val START_DATE = "start_date"
        private const val END_DATE = "end_date"
        private const val DATE_MODE = "date_mode"
        fun newInstance(
            time: Long = System.currentTimeMillis(),
            startDate: Long = 0,
            endDate: Long = System.currentTimeMillis(),
            dateMode: Int = DateMode.YEAR_MONTH_DAY
        ): DateDialogFragment {
            val arg = Bundle()
            arg.putLong(ARG_TIME, time)
            arg.putLong(START_DATE, startDate)
            arg.putLong(END_DATE, endDate)
            arg.putInt(DATE_MODE, dateMode)
            val fragment = DateDialogFragment()
            fragment.arguments = arg
            return fragment
        }
    }

}













