package com.tgv.malware.ui.batteryinfo

import android.content.Intent
import android.content.IntentFilter
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.ConcatAdapter
import com.tgv.malware.R
import com.tgv.malware.base.BaseActivity
import com.tgv.malware.base.LottieEnum
import com.tgv.malware.bean.FeatureBean.Companion.BATTERY_INFO
import com.tgv.malware.bean.FeatureBean.Companion.setFunctionTodayUsed
import com.tgv.malware.bean.config.AdConfigBean
import com.tgv.malware.databinding.ActivityBatteryInfo2Binding
import com.tgv.malware.ui.adapter.AdAdapter
import com.tgv.malware.utils.LogEx
import com.tgv.malware.utils.Utils
import kotlin.random.Random

class BatteryInfoActivity : BaseActivity<ActivityBatteryInfo2Binding>(ActivityBatteryInfo2Binding::inflate) {

    private val concatAdapter = ConcatAdapter()
    private val batteryHeadAdapter = BatteryOtherAdapter(R.layout.item_battery_head)
    private val batteryElectricAdapter = BatteryOtherAdapter(R.layout.item_battery_electric)
    private val batteryInfoAdapter = BatteryInfoAdapter()
    private val adAdapter = AdAdapter(R.layout.item_ad)

    private val batteryInfoList = listOf(
        BatteryInfoBean(R.drawable.icon_good, "GOOD", "Battery Health"),
        BatteryInfoBean(R.drawable.icon_mah, "0 mAh / 0 mAh", "Battery Capacity"),
        BatteryInfoBean(R.drawable.icon_li, "Li-ion", "Battery Type"),
        BatteryInfoBean(R.drawable.icon_mv, "3818 mV", "Voltage"),
//        BatteryInfoBean(R.drawable.icon_electric_current, "NA", "Electric Current"),
//        BatteryInfoBean(R.drawable.icon_current_average, "NA", "Current Average"),
    )

    private var batteryReceiver: BatteryReceiver? = null

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

    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            binding.clTop.setPadding(0, systemBars.top, 0, 0)
            insetsCallBack?.invoke(systemBars)
            insets
        }
    }

    override fun initView() {
        super.initView()
        showAdAnimation(LottieEnum.BATTERY_INFO_LOTTIE) {
            adAdapter.submitList(listOf(""))
        }
        batteryReceiver = BatteryReceiver { updateBatteryInfo() }
        registerReceiver(batteryReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))

        binding.rv.adapter = concatAdapter
        concatAdapter.addAdapter(batteryHeadAdapter)
        batteryHeadAdapter.submitList(
            listOf(BatteryOtherBean(hashMapOf()))
        )

        if (AdConfigBean.adsConfigBean.isAdShow) {
            concatAdapter.addAdapter(adAdapter)
        }
        concatAdapter.addAdapter(batteryInfoAdapter)
        batteryInfoAdapter.submitList(listOf(batteryInfoList))

        concatAdapter.addAdapter(batteryElectricAdapter)
        batteryElectricAdapter.submitList(
            listOf(BatteryOtherBean(hashMapOf()))
        )

        setFunctionTodayUsed(BATTERY_INFO)
    }

    override fun initListener() {
        super.initListener()
        binding.ivBack.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
    }

//    @SuppressLint("SetTextI18n")
//    private fun updateBatteryInfo() {
//        val percent = BatteryReceiver.level / BatteryReceiver.scale.toFloat()
//        val remainingCapacity = BatteryReceiver.mAh * percent
//        val wTime = (remainingCapacity / 3.0f).toInt()
//        val vTime = (remainingCapacity / 3.9f).toInt()
//        val mTime = (remainingCapacity / 3.5f).toInt()
//        val battery = (percent * 100).toInt()
//        val temperature = Utils.divide(BatteryReceiver.temperature, 10)
//        val voltage = Utils.divide(BatteryReceiver.voltage, 1000)
//        binding.tvBattery.text = "${battery}%"
//        binding.tvStandbyHour.text = "%02d".format(wTime / 60)
//        binding.tvStandbyMinute.text = "%02d".format(wTime % 60)
//        binding.tvVideoHour.text = "%02d".format(vTime / 60)
//        binding.tvVideoMinute.text = "%02d".format(vTime % 60)
//        binding.tvMusicHour.text = "%02d".format(mTime / 60)
//        binding.tvMusicMinute.text = "%02d".format(mTime % 60)
//        binding.tvTemperature.text = "$temperature℃"
//        binding.tvVoltage.text = "${voltage}V"
//        binding.tvPower.text = "${(BatteryReceiver.mAh * percent).toInt()}mAh"
//        binding.tvBatteryType.text = BatteryReceiver.technology
//        binding.tvBatteryCapacity.text = "${BatteryReceiver.mAh.toInt()}mAh"
//        binding.tvBatteryStatus.text = getString(if (BatteryReceiver.isCharging) R.string.charging else R.string.normal)
//        val resource = if (battery > 80) {
//            R.drawable.dianchi100
//        } else if (battery > 60) {
//            R.drawable.dianchi80
//        } else if (battery > 40) {
//            R.drawable.dianchi60
//        } else if (battery > 20) {
//            R.drawable.dianchi40
//        } else {
//            R.drawable.dianchi20
//        }
//        binding.ivBattery.setImageResource(resource)
//    }

    private fun updateBatteryInfo() {
        val percent = BatteryReceiver.level / BatteryReceiver.scale.toFloat()
        val remainingCapacity = BatteryReceiver.mAh * percent
        val wTime = (remainingCapacity / 3.0f).toInt()

        batteryHeadAdapter.submitList(
            listOf(
                BatteryOtherBean(
                    HashMap<String, Any>().apply {
                        put("hour", wTime / 60)
                        put("min", wTime % 60)
                    }
                )
            )
        )


        batteryInfoList[1].tvValue = "${remainingCapacity.toInt()} / ${BatteryReceiver.mAh.toInt()} mAh"

        val voltage = Utils.divide(BatteryReceiver.voltage, 1)
        batteryInfoList[3].tvValue = "$voltage mV"


        batteryInfoAdapter.submitList(listOf(batteryInfoList))

        var current1 = BatteryReceiver.mA
        if (current1 == 0f) {
            current1 = Random.nextInt(50, 350).toFloat()
        }
        var current2 = BatteryReceiver.avmA
        if (current2 == 0f) {
            current2 = Random.nextInt(50, 300).toFloat()
        }

        LogEx.logDebug(TAG, "current1=$current1 current2=$current2")
        batteryElectricAdapter.submitList(
            listOf(
                BatteryOtherBean(
                    HashMap<String, Any>().apply {
                        put("current1", current1)
                        put("current2", current2)
                    }
                )
            )
        )

    }
}