package com.test.basd.smartjunkcleaner.view

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.test.basd.smartjunkcleaner.databinding.DialogTimeSelectBinding
import com.test.basd.smartjunkcleaner.helps.TimeUtils.PAST_60_MINUS_QUERY
import com.test.basd.smartjunkcleaner.helps.TimeUtils.SEVEN_DAYS_QUERY
import com.test.basd.smartjunkcleaner.helps.TimeUtils.TODAY_QUERY
import com.test.basd.smartjunkcleaner.helps.TimeUtils.YESTERDAY_QUERY
import java.text.SimpleDateFormat
import kotlin.time.Duration.Companion.days
import kotlin.time.DurationUnit

object TimeSelectDialog {

    @SuppressLint("SimpleDateFormat", "SetTextI18n")
    fun Context.showTimeSelectDialog(click: (mode: Int) -> Unit) {
        val dialog = BottomSheetDialog(this)
        val binding = DialogTimeSelectBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)
        dialog.show()

        //修改dialog的尺寸
        val lp = dialog.window?.attributes
        lp?.width = ViewGroup.LayoutParams.MATCH_PARENT
        lp?.height = ViewGroup.LayoutParams.WRAP_CONTENT
        dialog.window?.attributes = lp
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        val simpleDateFormat2 = SimpleDateFormat("(yyyy/MM/dd)")
        binding.tvToday.text = "Today " + simpleDateFormat2.format(System.currentTimeMillis())
        binding.tvYesterday.text =
            "Yesterday " + simpleDateFormat2.format(System.currentTimeMillis() - 1.days.toLong(DurationUnit.MILLISECONDS))

        binding.tv60.setOnClickListener {
            dialog.dismiss()
            click.invoke(PAST_60_MINUS_QUERY)
        }
        binding.tvToday.setOnClickListener {
            dialog.dismiss()
            click.invoke(TODAY_QUERY)
        }
        binding.tvYesterday.setOnClickListener {
            dialog.dismiss()
            click.invoke(YESTERDAY_QUERY)
        }
        binding.tv7Days.setOnClickListener {
            dialog.dismiss()
            click.invoke(SEVEN_DAYS_QUERY)
        }
    }
}