package com.dumpster.cleaner.ui.batteryinfo

import android.content.Intent
import android.content.IntentFilter
import androidx.recyclerview.widget.ConcatAdapter
import com.dumpster.cleaner.R
import com.dumpster.cleaner.base.BaseActivity
import com.dumpster.cleaner.base.LottieEnum
import com.dumpster.cleaner.databinding.ActivityBatteryInfo2Binding
import com.dumpster.cleaner.ui.adapter.AdAdapter
import com.dumpster.cleaner.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(R.drawable.icon_current_average, "37.5℃", "Temperature"),
        BatteryInfoBean(R.drawable.icon_voltage, "3.6V", "Voltage"),
        BatteryInfoBean(R.drawable.icon_technology, "Li-Poly", "Technology"),
        BatteryInfoBean(R.drawable.icon_battery, "0mAh", "Battery Capacity"),
        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 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(BatteryHead(1, 0))
        )

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

    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(
                BatteryHead(wTime / 60, wTime % 60)
            )
        )


        val temperature = Utils.divide(BatteryReceiver.temperature, 10)
        batteryInfoList[0].tvValue = "$temperature℃"

        val voltage = Utils.divide(BatteryReceiver.voltage, 1000)
        batteryInfoList[1].tvValue = "${voltage}V"

        batteryInfoList[3].tvValue = "${(BatteryReceiver.mAh * percent).toInt()}mAh"
        batteryInfoList[4].tvValue = "${BatteryReceiver.mA.toInt()}mA"
        batteryInfoList[5].tvValue = "${BatteryReceiver.avmA.toInt()}mA"

        batteryInfoAdapter.submitList(
            listOf(
                batteryInfoList
            )
        )
    }
}