package com.cheng.blzb.ui.dialog

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.cheng.blzb.R
import com.cheng.blzb.databinding.DialogDatePickerBinding
import com.example.base.extensions.onClick
import com.example.base.utils.ScreenUtils
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import java.util.Calendar
import java.util.Date

class DatePickerDialog : DialogFragment() {
    private val title by lazy {arguments?.getString("title")}
    private val time by lazy { arguments?.getLong("time") ?: System.currentTimeMillis() }
    private val startTime by lazy { arguments?.getLong("start_time") ?: 0 }
    private val endTime by lazy { arguments?.getLong("end_time") ?: System.currentTimeMillis() }
    private val dateMode by lazy { arguments?.getInt("date_mode") ?: DateMode.YEAR_MONTH_DAY }

    private var selectedTime: Calendar = Calendar.getInstance()

    private var mOnBackListener: ((Long) -> Unit)? = null     //回调事件

    lateinit var binding: DialogDatePickerBinding

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window?.attributes
        windowParams?.dimAmount = 0.7f
        windowParams?.width = ScreenUtils.getWindowSize().x
        windowParams?.gravity = Gravity.BOTTOM
        windowParams?.windowAnimations = R.style.dialog_bottom
        dialog?.window?.attributes = windowParams
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_date_picker, null)
        binding = DialogDatePickerBinding.bind(view)

        binding.tvTitle.text = title

        selectedTime.timeInMillis = time

        binding.datePicker.setRange(DateEntity.target(Date(startTime)), if (endTime != 0L) DateEntity.target(Date(endTime)) else null, DateEntity.target(Date(time)))
        binding.datePicker.setDateMode(dateMode)
        binding.datePicker.setOnDateSelectedListener { year, month, day ->
            selectedTime.set(Calendar.YEAR, year)
            selectedTime.set(Calendar.MONTH, month - 1)
            selectedTime.set(Calendar.DAY_OF_MONTH, day)
        }

        binding.tvConfirm.onClick {
            mOnBackListener?.invoke(selectedTime.timeInMillis)
            dismiss()
        }

        binding.tvCancel.onClick {
            dismiss()
        }

        val dialog = Dialog(requireContext())
        dialog.setContentView(view)
        return dialog
    }

    fun setOnSelectListener(listener: ((Long) -> Unit)) {
        mOnBackListener = listener
    }

    companion object {

        fun newInstance(
            title: String? = "",
            time: Long = System.currentTimeMillis(),
            startTime: Long = 0,
            endTime: Long = System.currentTimeMillis(),
            dateMode: Int = DateMode.YEAR_MONTH_DAY
        ): DatePickerDialog {
            val arg = Bundle()
            val fragment = DatePickerDialog()
            arg.putString("title", title)
            arg.putLong("time", time)
            arg.putLong("start_time", startTime)
            arg.putLong("end_time", endTime)
            arg.putInt("date_mode", dateMode)
            fragment.arguments = arg
            return fragment
        }
    }
}