package com.easy.clean.ui.batteryinfo

import android.content.Intent
import android.content.IntentFilter
import androidx.recyclerview.widget.ConcatAdapter
import com.easy.clean.R
import com.easy.clean.base.BaseActivity
import com.easy.clean.base.LottieEnum
import com.easy.clean.bean.config.AdConfigBean
import com.easy.clean.databinding.ActivityBatteryInfo2Binding
import com.easy.clean.ui.adapter.AdAdapter
import com.easy.clean.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(""))
        }
        binding.llTitle.tvTitle.text = getString(R.string.battery_info)
        batteryReceiver = BatteryReceiver { updateBatteryInfo() }
        registerReceiver(batteryReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))

        binding.rv.adapter = concatAdapter
        concatAdapter.addAdapter(batteryHeadAdapter)
        batteryHeadAdapter.submitList(
            listOf(BatteryHead(1))
        )

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

    override fun initListener() {
        super.initListener()
        binding.llTitle.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
            )
        )
    }
}