package com.base.appzxhy.ui.batteryinfo

import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import androidx.recyclerview.widget.ConcatAdapter
import com.base.appzxhy.R
import com.base.appzxhy.base.BaseActivity
import com.base.appzxhy.base.LottieEnum
import com.base.appzxhy.databinding.ActivityBatteryInfo2Binding
import com.base.appzxhy.ui.adapter.AdAdapter
import com.base.appzxhy.utils.Utils

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

    private val concatAdapter = ConcatAdapter()
    private val batteryHeadAdapter = BatteryHeadAdapter()
    private val batteryInfoAdapter = BatteryInfoAdapter()
    private val adAdapter = AdAdapter(R.layout.item_ad)

    private val batteryInfoList = listOf(
        BatteryInfoBean("Good", "Health"),
        BatteryInfoBean("Normal", "Battery Status"),
        BatteryInfoBean("Li-Poly", "Battery Type"),
        BatteryInfoBean("0mAh", "Battery Capacity"),
    )

    private var batteryReceiver: BatteryReceiver? = null

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

    override fun initView() {
        super.initView()
        setStatusBarColor(getColor(R.color.colorPrimary))
        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(BatteryHead(1))
        )


        concatAdapter.addAdapter(adAdapter)
        concatAdapter.addAdapter(batteryInfoAdapter)
        batteryInfoAdapter.submitList(listOf(batteryInfoList))
    }

    override fun initListener() {
        super.initListener()
        binding.ivBack.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
        binding.tvBtn.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()
        val temperature = Utils.divide(BatteryReceiver.temperature, 10)
        val voltage = Utils.divide(BatteryReceiver.voltage, 1000)

        batteryHeadAdapter.submitList(
            listOf(
                BatteryHead(
                    min = wTime,
                    p = percent,
                    temper = "$temperature℃",
                    v = "${voltage}V",
                    power = "${(BatteryReceiver.mAh * percent).toInt()} mAh"
                )
            )
        )

        batteryInfoList[0].tvValue = when (80) {
            in 0..20 -> {
                "Bad"
            }

            in 21..50 -> {
                "Not Bad"
            }

            else -> {
                "Good"
            }
        }
        batteryInfoList[3].tvValue = "${BatteryReceiver.mAh.toInt()}mAh"


        batteryInfoAdapter.submitList(
            listOf(
                batteryInfoList
            )
        )
    }
}