package com.miraclegarden.smsmessage.util;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;

public class OEMBackgroundHelper {

    private static final String MIUI_SECURITY = "com.miui.securitycenter";
    private static final String MIUI_AUTOSTART = "com.miui.permcenter.autostart.AutoStartManagementActivity";

    private static final String COLOROS_SAFECENTER = "com.coloros.safecenter";
    private static final String COLOROS_STARTUP = "com.coloros.safecenter.permission.startup.StartupAppListActivity";

    private static final String OPPO_SAFE = "com.oppo.safe";
    private static final String OPPO_STARTUP = "com.oppo.safe.permission.startup.StartupAppListActivity";

    private static final String VIVO_PERMISSION = "com.vivo.permissionmanager";
    private static final String VIVO_BGSTARTUP = "com.vivo.permissionmanager.activity.BgStartUpManagerActivity";

    private static final String IQOO_SECURE = "com.iqoo.secure";
    private static final String IQOO_BGSTARTUP = "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager";

    private static final String HUAWEI_SYSTEM_MANAGER = "com.huawei.systemmanager";
    private static final String HUAWEI_STARTUP_CTRL = "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity";
    private static final String HUAWEI_POWER_MANAGER = "com.huawei.systemmanager.power.ui.HwPowerManagerActivity";

    public static void openAutoStartSettings(Context context) {
        String manufacturer = Build.MANUFACTURER.toLowerCase();

        Intent intent = new Intent();
        boolean success = false;

        try {
            if (manufacturer.contains("xiaomi") || manufacturer.contains("redmi")) {
                intent.setComponent(new ComponentName(MIUI_SECURITY, MIUI_AUTOSTART));
                success = true;
            } else if (manufacturer.contains("oppo")) {
                if (tryStartActivity(context, intent, COLOROS_SAFECENTER, COLOROS_STARTUP)) return;
                if (tryStartActivity(context, intent, OPPO_SAFE, OPPO_STARTUP)) return;
            } else if (manufacturer.contains("realme")) {
                if (tryStartActivity(context, intent, COLOROS_SAFECENTER, COLOROS_STARTUP)) return;
            } else if (manufacturer.contains("vivo")) {
                if (tryStartActivity(context, intent, VIVO_PERMISSION, VIVO_BGSTARTUP)) return;
                if (tryStartActivity(context, intent, IQOO_SECURE, IQOO_BGSTARTUP)) return;
            } else if (manufacturer.contains("huawei") || manufacturer.contains("honor")) {
                if (tryStartActivity(context, intent, HUAWEI_SYSTEM_MANAGER, HUAWEI_STARTUP_CTRL)) return;
                if (tryStartActivity(context, intent, HUAWEI_SYSTEM_MANAGER, HUAWEI_POWER_MANAGER)) return;
            } else if (manufacturer.contains("oneplus")) {
                openBatteryOptimizationSettings(context);
                return;
            } else if (manufacturer.contains("samsung")) {
                openAppBatterySettings(context);
                return;
            }

            if (success) {
                context.startActivity(intent);
            } else {
                openBatteryOptimizationSettings(context);
            }
        } catch (ActivityNotFoundException e) {
            openBatteryOptimizationSettings(context);
        }
    }

    public static void openBatteryOptimizationSettings(Context context) {
        try {
            Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            context.startActivity(intent);
        }
    }

    public static void requestBatteryOptimizationExemption(Context context) {
        if (!isIgnoringBatteryOptimizations(context)) {
            try {
                Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                openBatteryOptimizationSettings(context);
            }
        }
    }

    public static boolean isIgnoringBatteryOptimizations(Context context) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm.isIgnoringBatteryOptimizations(context.getPackageName());
    }

    public static String getManufacturer() {
        return Build.MANUFACTURER;
    }

    public static boolean needsAutoStartPermission() {
        String manufacturer = Build.MANUFACTURER.toLowerCase();
        return manufacturer.contains("xiaomi") || manufacturer.contains("redmi")
                || manufacturer.contains("oppo") || manufacturer.contains("realme")
                || manufacturer.contains("vivo") || manufacturer.contains("huawei")
                || manufacturer.contains("honor") || manufacturer.contains("oneplus");
    }

    public static String getAutoStartPermissionName() {
        String manufacturer = Build.MANUFACTURER.toLowerCase();
        if (manufacturer.contains("xiaomi") || manufacturer.contains("redmi")) {
            return "自启动";
        } else if (manufacturer.contains("oppo") || manufacturer.contains("realme")) {
            return "自动启动";
        } else if (manufacturer.contains("vivo")) {
            return "后台启动";
        } else if (manufacturer.contains("huawei") || manufacturer.contains("honor")) {
            return "自动启动管理";
        } else if (manufacturer.contains("oneplus")) {
            return "电池优化";
        }
        return "电池/自启动";
    }

    private static boolean tryStartActivity(Context context, Intent intent, String pkg, String cls) {
        try {
            intent.setComponent(new ComponentName(pkg, cls));
            context.startActivity(intent);
            return true;
        } catch (ActivityNotFoundException e) {
            return false;
        }
    }

    private static void openAppBatterySettings(Context context) {
        try {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            context.startActivity(intent);
        }
    }
}