package pxds.etmo

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.graphics.Typeface
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.BarUtils
import com.test.basd.supercleanermax.R
import com.test.basd.supercleanermax.databinding.NjbrquBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import pxds.czbe.twvj.Ntwq
import pxds.czbe.twvj.Sdpo
import pxds.czbe.twvj.Xteh
import pxds.dlpf.buoz.fico.Shzt
import pxds.lmmd.zmwf.Bpfw
import pxds.lmmd.zmwf.Khko.setTrackedOnClickListener
import pxds.lmmd.zmwf.Khko.string

class Mzvb : Bpfw<NjbrquBinding>() {

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

    private lateinit var batteryReceiver: BatteryReceiver

    private val fragments by lazy {
        mutableListOf(homeFragment, batteryFragment, toolsFragment)
    }

    var level = 0

    private val toolsFragment by lazy {
        Sdpo()
    }

    private val batteryFragment by lazy {
        Ntwq()
    }

    private val homeFragment by lazy {
        Xteh()
    }

    override val isLightMode = true

    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)

        binding.xohiqvq.run {
            adapter = object : FragmentStateAdapter(this@Mzvb) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }
        binding.xohiqvq.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                setPage(position)
            }
        })
        binding.djdwnit.setTrackedOnClickListener {

            binding.xohiqvq.currentItem = 0
        }
        binding.trysyrz.setTrackedOnClickListener {
            binding.xohiqvq.currentItem = 1
        }
        binding.uijmpuw.setTrackedOnClickListener {
            binding.xohiqvq.currentItem = 2
        }

        batteryReceiver = BatteryReceiver()
        val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        registerReceiver(batteryReceiver, filter)
    }

    private inner class BatteryReceiver : BroadcastReceiver() {

        override fun onReceive(context: Context?, intent: Intent?) {
            val current = intent?.extras!!.getInt(R.string.ޥޝގޥށ_D.string())
            val total = intent.extras!!.getInt(R.string.ޚގތހޢ_D.string())
            level = current * 100 / total

        }
    }

    private fun setPage(p: Int) {
        binding.djdwnit.isSelected = false
        binding.trysyrz.isSelected = false
        binding.uijmpuw.isSelected = false
        binding.uhqbbjd.typeface = Typeface.DEFAULT
        binding.sestraa.typeface = Typeface.DEFAULT
        binding.ineoscg.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.djdwnit.isSelected = true
                binding.uhqbbjd.typeface = Typeface.DEFAULT_BOLD
            }

            1 -> {
                binding.trysyrz.isSelected = true
                binding.sestraa.typeface = Typeface.DEFAULT_BOLD
            }

            2 -> {
                binding.uijmpuw.isSelected = true
                binding.ineoscg.typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (AppUtils.isAppForeground()) {
            lifecycleScope.launch(Dispatchers.Main) {
                delay(1000)
                Shzt.show(this@Mzvb)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(batteryReceiver)
    }
}