package com.test.basd.smartjunkcleaner.fragment

import android.annotation.SuppressLint
import android.graphics.Typeface
import android.os.Bundle
import com.test.basd.smartjunkcleaner.activity.RecentAppActivity
import com.test.basd.smartjunkcleaner.adapter.RecentAppAdapter
import com.test.basd.smartjunkcleaner.adapter.RecentAppAdapter.Companion.UI_SCREEN_TIME_MODE
import com.test.basd.smartjunkcleaner.bean.AppBean
import com.test.basd.smartjunkcleaner.databinding.FragmentScreenTimeBinding
import com.test.basd.smartjunkcleaner.helps.BaseFragment
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 com.test.basd.smartjunkcleaner.view.TimeSelectDialog.showTimeSelectDialog
import java.text.SimpleDateFormat
import kotlin.time.Duration.Companion.hours
import kotlin.time.DurationUnit


/**
 */
class ScreenTimeFragment : BaseFragment<FragmentScreenTimeBinding>() {

    private var simpleDateFormat2 = SimpleDateFormat("(yyyy/MM/dd)")
    private lateinit var adapter: RecentAppAdapter
    private val dataList = arrayListOf<AppBean>()

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

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


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

                    TODAY_QUERY -> {
                        todayUI()
                    }

                    YESTERDAY_QUERY -> {
                        yesterdayUI()
                    }

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

    private fun setTextFont() {
        val fontMedium = "sans-serif-medium"
        val fontMediumTypeface = Typeface.create(fontMedium, Typeface.NORMAL)
        binding.tvDate.setTypeface(fontMediumTypeface)
    }

    @SuppressLint("SetTextI18n")
    fun todayUI(isReFresh: Boolean = true) {
        binding.tvDate.text = "Today" + simpleDateFormat2.format(System.currentTimeMillis())
        if (isReFresh) {
            (requireActivity() as RecentAppActivity).changeTimeRefresh(TODAY_QUERY, 1)
        }
    }

    @SuppressLint("SetTextI18n")
    fun recent60UI(isReFresh: Boolean = true) {
        binding.tvDate.text = "Recent 60 minus"
        if (isReFresh) {
            (requireActivity() as RecentAppActivity).changeTimeRefresh(PAST_60_MINUS_QUERY, 1)
        }
    }

    @SuppressLint("SetTextI18n")
    fun yesterdayUI(isReFresh: Boolean = true) {
        binding.tvDate.text =
            "Yesterday" + simpleDateFormat2.format(
                System.currentTimeMillis() - 24.hours.toLong(
                    DurationUnit.MILLISECONDS
                )
            )
        if (isReFresh) {
            (requireActivity() as RecentAppActivity).changeTimeRefresh(YESTERDAY_QUERY, 1)
        }
    }

    @SuppressLint("SetTextI18n")
    fun sevenDaysUI(isReFresh: Boolean = true) {
        binding.tvDate.text = "Last 7 days"
        if (isReFresh) {
            (requireActivity() as RecentAppActivity).changeTimeRefresh(SEVEN_DAYS_QUERY, 1)
        }
    }

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

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

}