package com.ux.video.file.filerecovery.photo

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.toDrawable
import androidx.core.graphics.toColorInt
import androidx.fragment.app.DialogFragment
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.databinding.DialogFilterCustomerDateBinding
import com.ux.video.file.filerecovery.utils.Common
import com.ux.video.file.filerecovery.utils.CustomTextView
import org.jaaksi.pickerview.picker.TimePicker
import org.jaaksi.pickerview.picker.TimePicker.Companion.TYPE_DATE
import org.jaaksi.pickerview.picker.TimePicker.OnTimeSelectListener
import org.jaaksi.pickerview.widget.DefaultCenterDecoration
import java.util.Date

class DatePickerDialogFragment(
    var mContext: Context,
    var title: String,
    var onPickerChooseListener: (selectedDate: Date)-> Unit,
    var onClickCancel:()-> Unit
) :
    DialogFragment(), OnTimeSelectListener {

    private lateinit var binding: DialogFilterCustomerDateBinding
    private lateinit var timerPicker: TimePicker
    private var defaultSelectedDate = System.currentTimeMillis()
    private var defaultStartDate = 1540361760000L
    private var defaultEndDate = System.currentTimeMillis()
    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            setGravity(Gravity.CENTER)
            setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DialogFilterCustomerDateBinding.inflate(inflater)
        initDatePickerView()
        binding.run {
            textTitle.text =title
            tvCancel.setOnClickListener {
                if (!timerPicker.canSelected()) return@setOnClickListener
                dismiss()
                onClickCancel.invoke()
            }
            tvOk.setOnClickListener {
                if (!timerPicker.canSelected()) return@setOnClickListener
                dismiss()
                timerPicker.onConfirm()

            }
        }

        return binding.root
    }


    fun setCurrentSelectedDate(dateTimer: Date){
        defaultSelectedDate = dateTimer.time
    }

    fun setRangeDate(start: Date){
        defaultStartDate = start.time
//        defaultEndDate = end
    }

    private fun initDatePickerView() {
        var index = 0
        var centerDrawable: Drawable? = null
        timerPicker = TimePicker.Builder(requireContext(), TYPE_DATE, this)
            .setRangDate(defaultStartDate, defaultEndDate)
//            .setContainsStarDate(true)
            //.setTimeMinuteOffset(10)
            // 设置选中时间
            .setSelectedDate(defaultSelectedDate)
            .setInterceptor { pickerView, params ->
                pickerView.run {
                    visibleItemCount = 5
                    setTextSize(15, 19)
                    setFont(CustomTextView.bold)
                    setColor(
                        Common.getColorInt(requireContext(), R.color.main_title),
                        Common.getColorInt(requireContext(), R.color.main_sub_title)
                    )
                    centerDrawable = when (index) {
                        0 -> ContextCompat.getDrawable(
                            mContext,
                            R.drawable.bg_rectangle_15787880_left_13
                        )

                        1 -> ContextCompat.getDrawable(
                            mContext,
                            R.drawable.bg_rectangle_15787880_center
                        )

                        else -> ContextCompat.getDrawable(
                            mContext,
                            R.drawable.bg_rectangle_15787880_right_13
                        )
                    }
                    index = index + 1
                    val centerDecoration =
                        DefaultCenterDecoration(requireContext())
                            .setLineColor("#00000000".toColorInt())
                            //.setMargin(margin, -margin, margin, -margin)
                            .setLineWidth(0f)
                            .setDrawable(centerDrawable)
                    setCenterDecoration(centerDecoration)
                }
            }
            .setFormatter(object : TimePicker.DefaultFormatter() {
                // 自定义Formatter显示去年，今年，明年
                @SuppressLint("DefaultLocale")
                override fun format(
                    picker: TimePicker,
                    type: Int,
                    position: Int,
                    value: Long
                ): CharSequence {
                    if (type == TimePicker.TYPE_YEAR) {
                        return String.format("%d", value)
                    } else if (type == TimePicker.TYPE_MONTH) {
                        return String.format("%d", value)
                    } else if (type == TimePicker.TYPE_DAY) {
                        return String.format("%d", value)
                    }
                    return super.format(picker, type, position, value)
                }
            }).create()
        timerPicker.view().let {
            (it.parent as? ViewGroup)?.removeView(it)
            binding.pickerContainer.addView(it)
        }


    }

    override fun onTimeSelect(
        picker: TimePicker,
        date: Date
    ) {
        defaultSelectedDate = date.time
        onPickerChooseListener(date)
        Common.showLog("--------onTimeSelect=${Common.getChineseFormatDate(date)}")
    }

}
