package com.swiftcleaner.chovey.model.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;

public class BatteryReceiver extends BroadcastReceiver {
    private Runnable block;
    private static final String BATTERY_NOTIFICATION_STATUS_KEY = "batteryNotificationStatus";
    private static final String BATTERY_NOTIFICATION_INTERVAL_KEY = "batteryNotificationInterval";
    private static final String BATTERY_NOTIFICATION_COUNT_KEY = "batteryNotificationCount";
    private static final String BATTERY_NOTIFICATION_CURRENT_KEY = "batteryNotificationCurrent";
    private static final String BATTERY_NOTIFICATION_LAST_TIME_KEY = "batteryNotificationLastTime";
    public int level = 0;
    public int scale = 0;
    public int voltage = 0;
    public int temperature = 0;
    public String technology = "";
    public int health = 0;
    public int status = 0;
    public float mAh = 0f;
    public boolean isCharging = false;
    public int current = 0; // 电流值，单位为微安培（μA）
    public int currentAverage = 0;
    public int hours;
    public int minutes;
    public BatteryReceiver(Runnable block) {
        this.block = block;
    }

    public static void register(Context context) {
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            context.registerReceiver(new BatteryReceiver(null), intentFilter, Context.RECEIVER_EXPORTED);
        } else {
            context.registerReceiver(new BatteryReceiver(null), intentFilter);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (!Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) return;

        BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
        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);
        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;
        int chargeCounter = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
        int propertyCapacity = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        if (chargeCounter != Integer.MIN_VALUE && propertyCapacity != Integer.MIN_VALUE) {
            mAh = (chargeCounter / (propertyCapacity / 100f)) / 1000f;
        }
        current = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
        // 获取平均电流值，单位为微安培（μA）
        currentAverage = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);

        float averagePowerConsumption = 1000; // 假设平均功耗为1000mAh/小时
        long remainingTimeHours = (long) (mAh / averagePowerConsumption);
        hours = (int) remainingTimeHours;
        minutes = (int) ((Math.round((remainingTimeHours - hours) * 60)));

        if (block != null) {
            block.run();
        }
    }
}