package com.example.base.dialog

import android.app.Dialog
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import android.widget.DatePicker
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.example.base.R
import com.example.base.extensions.getYYYYMM
import java.util.Calendar

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

class YearMonthDialogFragment : DialogFragment() {

    private var mCurrentTime: Long = 0

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


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

        mCurrentTime = arguments?.getLong(ARG_TIME, System.currentTimeMillis()) ?: 0

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

        val mDialog = AlertDialog.Builder(requireContext())
        mDialog.setView(v)
        mDialog.setNegativeButton("取消", null)
        mDialog.setPositiveButton("确定") { _, _ ->
            mOnSelectedListener?.invoke(mCurrentTime)
        }


        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 dayPickerView = view.findViewById<View?>(resources.getIdentifier("android:id/day", null, null))
        dayPickerView?.visibility = View.GONE

        val tvTime = view.findViewById<TextView>(R.id.tvTime)
        val datePicker = view.findViewById<DatePicker>(R.id.datePicker)

        val calendar = Calendar.getInstance()
        calendar.timeInMillis = mCurrentTime

        tvTime.text = mCurrentTime.getYYYYMM()

        datePicker.init(
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH)
        ) { _, year, monthOfYear, dayOfMonth ->
            calendar.set(Calendar.YEAR, year)
            calendar.set(Calendar.MONTH, monthOfYear)
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
            val time = calendar.time.time
            tvTime.text = time.getYYYYMM()
            mCurrentTime = time
        }
    }

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

    companion object {
        private const val ARG_TIME = "time"
        fun newInstance(time: Long = System.currentTimeMillis()): YearMonthDialogFragment {
            val arg = Bundle()
            arg.putLong(ARG_TIME, time)
            val fragment = YearMonthDialogFragment()
            fragment.arguments = arg
            return fragment
        }
    }

}













