package com.ritoq.quickphone.ui.batteryinfo

import android.annotation.SuppressLint
import android.content.Intent
import android.content.IntentFilter
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.ritoq.quickphone.R
import com.ritoq.quickphone.base.BaseActivity
import com.ritoq.quickphone.base.LottieEnum
import com.ritoq.quickphone.bean.FeatureBean.Companion.BATTERY_INFO
import com.ritoq.quickphone.bean.FeatureBean.Companion.setFunctionTodayUsed
import com.ritoq.quickphone.databinding.ActivityBatteryInfoBinding
import com.ritoq.quickphone.utils.Utils

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

    private var batteryReceiver: BatteryReceiver? = null

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

    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)
            insets
        }
    }

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

        setFunctionTodayUsed(BATTERY_INFO)
    }

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

    //    @SuppressLint("SetTextI18n")
    @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.tvCapacity1.text = "${(BatteryReceiver.mAh * percent).toInt()}mAh"
        binding.tvCapacity2.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)
    }


}