package com.dumpster.cleaner.ui.batteryinfo

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
import com.dumpster.cleaner.bean.FeatureBean.Companion.BATTERY_INFO
import com.dumpster.cleaner.bean.config.PopupConfigBean
import com.dumpster.cleaner.bean.push.NotificationSendBean
import com.dumpster.cleaner.bean.push.NotificationSendBean.Companion.POPUP_WHERE_BATTERY
import com.dumpster.cleaner.business.push.notification.MyNotificationManager
import com.dumpster.cleaner.utils.AppPreferences


//电池上次推送时间
var batteryLastPushTime = 0L
    get() {
        return AppPreferences.getInstance().getLong("batteryLastPushTime", field)
    }
    set(value) {
        field = value
        AppPreferences.getInstance().put("batteryLastPushTime", value, true)
    }

class BatteryReceiver(
    private val block: (() -> Unit?)? = null
) : BroadcastReceiver() {

    companion object {

        var level = 0
        var scale = 0
        var voltage = 0
        var temperature = 0
        var technology = ""
        var health = 0
        var status = 0
        var mAh = 0f
        var isCharging = false
        var mA = 0.0F//电流
        var avmA = 0.0F//平均电流

        fun registerBatteryReceiver(context: Context) {
            val intentFilter = IntentFilter().apply {
                addAction(Intent.ACTION_BATTERY_CHANGED)
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                context.registerReceiver(BatteryReceiver(), intentFilter, Context.RECEIVER_EXPORTED)
            } else {
                context.registerReceiver(BatteryReceiver(), intentFilter)
            }
        }
    }

    override fun onReceive(context: Context, intent: Intent) {
        if (Intent.ACTION_BATTERY_CHANGED != intent.action) return

        val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
        level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
        scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0)
        voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0)
        temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0)
        technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY).toString()
        health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0)
        status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN)
        isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL

        val chargeCounter = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)
        val propertyCapacity = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
        if (chargeCounter != Int.MIN_VALUE && propertyCapacity != Int.MIN_VALUE) {
            mAh = (chargeCounter / (propertyCapacity.toFloat() / 100f)) / 1000f
        }

        var currentMicroA = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
        if (currentMicroA != Int.Companion.MIN_VALUE) {
            mA = currentMicroA / 1000f // 转换为毫安
        }
        val avCurrentMicroA = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)
        if (avCurrentMicroA != Int.Companion.MIN_VALUE) {
            avmA = avCurrentMicroA / 1000f // 转换为毫安
        }

        block?.invoke()
        if (block == null) uiHandle(context)
    }

    private fun uiHandle(context: Context) {

        val batteryPct = level * 100 / scale.toFloat()
        if (batteryPct.toInt() > 21 && !isCharging) return

        if (!PopupConfigBean.popupConfigBean.batteryS) return

        val canSend = ((System.currentTimeMillis() - batteryLastPushTime) / 1000) >
                PopupConfigBean.popupConfigBean.batteryInterval

        MyNotificationManager.submitSendBean(
            NotificationSendBean(
                context,
                where = POPUP_WHERE_BATTERY,
                canSend = { canSend },
                sendSuccess = {
                    batteryLastPushTime = System.currentTimeMillis()
                },
            ).apply {
                actionId = BATTERY_INFO
            }
        )
    }
}