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

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.DialogFragment
import com.ux.video.file.filerecovery.R
import com.ux.video.file.filerecovery.databinding.CommonLayoutSortItemBinding
import com.ux.video.file.filerecovery.databinding.DialogSortBinding
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.datepicker.CalendarConstraints
import com.google.android.material.datepicker.DateValidatorPointForward
import java.text.SimpleDateFormat
import java.util.*

class DatePickerDialogFragment(val onClickSort: (type: Int) -> Unit) : DialogFragment() {

    private lateinit var binding: DialogSortBinding

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            setGravity(Gravity.BOTTOM)
            setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DialogSortBinding.inflate(inflater)

        return binding.root
    }

    fun showDatePicker() {
        // 创建日期选择器构建器
        val builder = MaterialDatePicker.Builder.datePicker()
        builder.setTitleText("选择日期")

        // 可选：限制可选日期，比如只能选择今天之后的日期
        val constraintsBuilder = CalendarConstraints.Builder()
        constraintsBuilder.setValidator(DateValidatorPointForward.now()) // 今天之后
        builder.setCalendarConstraints(constraintsBuilder.build())

        val datePicker = builder.build()

        // 显示日期选择器
//        datePicker.show(supportFragmentManager, "MATERIAL_DATE_PICKER")

        // 监听用户选择
        datePicker.addOnPositiveButtonClickListener { selection ->
            // selection 是 Long 类型的时间戳（UTC 毫秒）
            val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
            val dateString = sdf.format(Date(selection))
            println("用户选择的日期：$dateString")
        }
    }
}
