package com.xyzshell.andinfo.libs

import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager

data class BatteryDetails(
    val percentage: Int, // 0-100 电池百分比
    val isCharging: Boolean, // 是否正在充电
    val currentNow: Int?, // in mA 当前电流
    val powerUsage: Double?, // in W 功率消耗
    val temperature: Double, // in Celsius 温度 摄氏度
    val health: String, // 健康状态
    val voltage: Int, // in mV 电压
    val maxInput: Int?, // in mA 最大输入电流
    val capacity: Double?, // in mAh 电池容量
    val chargeCounter: Int?, // in mAh 电量计量
    val chargeCycles: Int?, // estimated cycles 充电周期 (估计值)
    val plugType: String, // 充电类型 (USB, AC, WIRELESS, NONE)
    val status: String // 电池状态 (Charging, Discharging, Full, Not Charging, Unknown)
)

class BatteryInfo(private val context: Context) {

    fun getBatteryDetails(): BatteryDetails {
        val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        val batteryStatus = context.registerReceiver(null, intentFilter)
        val batteryManager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager

        // 电池百分比
        val level = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
        val scale = batteryStatus?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1
        val percentage = if (level >= 0 && scale > 0) (level * 100 / scale) else -1

        // 是否充电
        val status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
        val isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL

        // 当前电流 (mA)
        val currentNow = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)?.let {
            it / 1000 // 转换为 mA
        }

        // 电压 (mV)
        val voltage = batteryStatus?.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1) ?: -1

        // 温度 (摄氏度)
        val temperature = (batteryStatus?.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1) ?: -1) / 10.0

        // 健康状态
        val health = when (batteryStatus?.getIntExtra(BatteryManager.EXTRA_HEALTH, -1) ?: -1) {
            BatteryManager.BATTERY_HEALTH_GOOD -> "Good"
            BatteryManager.BATTERY_HEALTH_OVERHEAT -> "Overheat"
            BatteryManager.BATTERY_HEALTH_DEAD -> "Dead"
            BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE -> "Over Voltage"
            BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE -> "Failure"
            BatteryManager.BATTERY_HEALTH_COLD -> "Cold"
            else -> "Unknown"
        }

        // 最大输入电流 (mA)
        val maxInput = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)?.let {
            it / 1000 // 转换为 mA
        }

        // 电池容量 (mAh)
        val capacity = batteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)?.toDouble()

        // 电量计量 (mAh)
        val chargeCounter = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)?.let {
            it / 1000 // 转换为 mAh
        }

        // 耗电量 (W)
        val powerUsage = if (currentNow != null && voltage > 0) {
            (currentNow * voltage) / 1_000_000.0 // 转换为 W
        } else {
            null
        }

        // 充电周期 (估计值)
        val chargeCycles = capacity?.let { cap ->
            chargeCounter?.let { counter ->
                (counter / cap).toInt()
            }
        }

        // 充电类型
        val plugType = when (batteryStatus?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) ?: -1) {
            BatteryManager.BATTERY_PLUGGED_USB -> "USB"
            BatteryManager.BATTERY_PLUGGED_AC -> "AC"
            BatteryManager.BATTERY_PLUGGED_WIRELESS -> "WIRELESS"
            else -> "NONE"
        }

        // 电池状态
        val statusText = when (status) {
            BatteryManager.BATTERY_STATUS_CHARGING -> "Charging"
            BatteryManager.BATTERY_STATUS_DISCHARGING -> "Discharging"
            BatteryManager.BATTERY_STATUS_FULL -> "Full"
            BatteryManager.BATTERY_STATUS_NOT_CHARGING -> "Not Charging"
            else -> "Unknown"
        }

        return BatteryDetails(
            percentage = percentage,
            isCharging = isCharging,
            currentNow = currentNow,
            powerUsage = powerUsage,
            temperature = temperature,
            health = health,
            voltage = voltage,
            maxInput = maxInput,
            capacity = capacity,
            chargeCounter = chargeCounter,
            chargeCycles = chargeCycles,
            plugType = plugType,
            status = statusText
        )
    }

    /**
     * 检测是否支持无线充电
     */
    fun isWirelessChargingSupported(): Boolean {
        return try {
            context.packageManager.hasSystemFeature("android.hardware.wireless_charging")
        } catch (_: Exception) {
            false
        }
    }
}