package pxds.czbe.twvj

import android.graphics.Typeface
import android.os.Bundle
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.BfpbjtBinding
import pxds.dlpf.buoz.fico.Wncp.showTimeSelectDialog
import pxds.etmo.Ngma
import pxds.hpls.xkxo.Fito
import pxds.hpls.xkxo.Fito.Companion.UI_SCREEN_TIME_MODE
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string
import pxds.lmmd.zmwf.Onaw.PAST_60_MINUS_QUERY
import pxds.lmmd.zmwf.Onaw.SEVEN_DAYS_QUERY
import pxds.lmmd.zmwf.Onaw.TODAY_QUERY
import pxds.lmmd.zmwf.Onaw.YESTERDAY_QUERY
import pxds.lmmd.zmwf.Wecj
import pxds.vazp.Rorl
import java.text.SimpleDateFormat
import kotlin.time.Duration.Companion.hours
import kotlin.time.DurationUnit

class Kaxp : Wecj<BfpbjtBinding>() {

    override val binding: BfpbjtBinding by lazy {
        BfpbjtBinding.inflate(layoutInflater)
    }

    private lateinit var adapter: Fito

    private val dataList = arrayListOf<Rorl>()

    private var simpleDateFormat2 = SimpleDateFormat(R.string.އށބސސ_D.string())

    fun sevenDaysUI(isReFresh: Boolean = true) {
        binding.dwjlpjd.text = R.string.ޟޢޟވޙ_D.string()
        if (isReFresh) {
            (requireActivity() as Ngma).changeTimeRefresh(SEVEN_DAYS_QUERY, 1)
        }
    }

    fun yesterdayUI(isReFresh: Boolean = true) {
        binding.dwjlpjd.text = R.string.ދޏގށޜ_D.string() + simpleDateFormat2.format(
                System.currentTimeMillis() - 24.hours.toLong(
                    DurationUnit.MILLISECONDS
                )
            )
        if (isReFresh) {
            (requireActivity() as Ngma).changeTimeRefresh(YESTERDAY_QUERY, 1)
        }
    }

    fun todayUI(isReFresh: Boolean = true) {
        binding.dwjlpjd.text = R.string.ކޣނޤޥ_D.string() + simpleDateFormat2.format(System.currentTimeMillis())
        if (isReFresh) {
            (requireActivity() as Ngma).changeTimeRefresh(TODAY_QUERY, 1)
        }
    }

    fun recent60UI(isReFresh: Boolean = true) {
        binding.dwjlpjd.text = R.string.ގނފގޒ_D.string()
        if (isReFresh) {
            (requireActivity() as Ngma).changeTimeRefresh(PAST_60_MINUS_QUERY, 1)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    private fun setTextFont() {
        val fontMedium = R.string.ޜޢޔޒޞ_D.string()
        val fontMediumTypeface = Typeface.create(fontMedium, Typeface.NORMAL)
        binding.dwjlpjd.setTypeface(fontMediumTypeface)
    }

    fun setScreenData(dataList: ArrayList<Rorl>) {
        this.dataList.clear()
        this.dataList.addAll(dataList.filter { it.screenTime > 0 })
        if (isVisible) {
            adapter.setData(this.dataList)
        }
    }

    override fun onResume() {
        super.onResume()
    }

    override fun setView() {
        setTextFont()
        binding.wifbgxg.setTrackedOnClickListener {
            requireContext().showTimeSelectDialog {
                when (it) {
                    PAST_60_MINUS_QUERY -> {
                        recent60UI()
                    }

                    TODAY_QUERY -> {
                        todayUI()
                    }

                    YESTERDAY_QUERY -> {
                        yesterdayUI()
                    }

                    SEVEN_DAYS_QUERY -> {
                        sevenDaysUI()
                    }
                }
            }
        }
        adapter = Fito(requireActivity(), UI_SCREEN_TIME_MODE)
        binding.hynncaa.adapter = adapter
        adapter.setData(dataList)
        todayUI(false)
    }
}