package com.zxhyis.toolss.ui.dialog

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.zxhyis.toolss.databinding.DialogTimeSelectBinding
import com.zxhyis.toolss.logic.setOnClick
import com.zxhyis.toolss.ui.page.viewmodel.AppViewModel
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.setOnClick(javaClass.simpleName) {
            dialog.dismiss()
            click.invoke(AppViewModel.PAST_60_MINUS_QUERY)
        }
        binding.tvToday.setOnClick(javaClass.simpleName) {
            dialog.dismiss()
            click.invoke(AppViewModel.TODAY_QUERY)
        }
        binding.tvYesterday.setOnClick(javaClass.simpleName) {
            dialog.dismiss()
            click.invoke(AppViewModel.YESTERDAY_QUERY)
        }
        binding.tv7Days.setOnClick(javaClass.simpleName) {
            dialog.dismiss()
            click.invoke(AppViewModel.SEVEN_DAYS_QUERY)
        }
    }
}