package com.zxdemo.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.BatteryManager
import android.util.Log
import com.swiftcleaner.chovey.model.bean.Global.NOTIFICATION_WHERE_BATTERY
import com.swiftcleaner.chovey.model.bean.Global.NOTIFICATION_WHERE_INSTALL
import com.swiftcleaner.chovey.model.bean.Global.batteryNotificationStatus
import com.zxdemo.notity.NotificationUtils
import com.zxdemo.utils.SpUtils

class BatteryStatusReceiver() : BroadcastReceiver() {

    override fun onReceive(context: Context?, batteryIntent: Intent?) {
        val level = batteryIntent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
        val scale = batteryIntent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1
        val state = batteryIntent?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1

        val now =
            batteryIntent?.getIntExtra(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW.toString(), -1)
                ?: -1
        val health = batteryIntent?.getIntExtra(BatteryManager.EXTRA_HEALTH, -1) ?: -1
        val voltage = batteryIntent?.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1) ?: -1
        val temperature = batteryIntent?.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1) ?: -1
        val technology = batteryIntent?.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY) ?: "Unknown"
        val batteryPct = level * 100 / scale.toFloat()


        var batteryNotificationStatus = SpUtils.getInstance().getInt(batteryNotificationStatus, 0)

        if (batteryNotificationStatus == 1
            &&
            (level in 11..19)
            &&
            state == BatteryManager.BATTERY_STATUS_CHARGING
        ) {
            if (context != null) {
                NotificationUtils.sendNotification(context, NOTIFICATION_WHERE_BATTERY)
            }
        }
    }


}