package com.atmob.flutter_tool_android.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.text.TextUtils;

public class BackgroundPermissionUtil {
    public static void requestBackgroundPermission(Context context) {
        try {
            int brand = BrandUtil.getBrand();
            switch (brand) {
                case BrandUtil.HuaWei:
                    handleHuaWei(context);
                    break;
                case BrandUtil.XiaoMi:
                    handleXiaoMi(context);
                    break;
                case BrandUtil.Vivo:
                    handleVivo(context);
                    break;
                case BrandUtil.Oppo:
                    handleOppo(context);
                    break;
                case BrandUtil.Meizu:
                    handleMeizu(context);
                    break;
                case BrandUtil.Samsung:
                    handleSamsung(context);
                    break;
                case BrandUtil.Smartisan:
                    handleSmartisan(context);
                    break;
                case BrandUtil.LEPHONE:
                    handleLePhone(context);
                    break;
                default:
                    throw new Exception("handle brand failed. brand = " + brand);
            }
        } catch (Exception e) {
            e.printStackTrace();
            openSetting(context);
        }
    }

    private static void handleLePhone(Context activity) {
        openTargetApp(activity, "com.letv.android.letvsafe",
                "com.letv.android.letvsafe.AutobootManageActivity");
    }

    private static void handleSmartisan(Context context) {
        openTargetApp(context, "com.smartisanos.security", null);
    }

    private static void handleSamsung(Context context) throws Exception {
        try {
            openTargetApp(context, "com.samsung.android.sm_cn", "com.samsung.android.sm.autorun.ui.AutoRunActivity");
            return;
        } catch (Exception e){
            e.printStackTrace();
        }
        try {
            openTargetApp(context, "com.samsung.android.sm_cn", null);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            openTargetApp(context, "com.samsung.android.sm", null);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new Exception("handle samsung failed.");
    }

    private static void handleMeizu(Context context) {
        openTargetApp(context, "com.meizu.safe", "com.meizu.safe.permission.SmartBGActivity");
    }

    private static void handleOppo(Context context) throws Exception {
        try {
            openTargetApp(context, "com.coloros.phonemanager", null);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            openTargetApp(context, "com.oppo.safe", null);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            openTargetApp(context, "com.coloros.oppoguardelf", null);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            openTargetApp(context, "com.coloros.safecenter", null);
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new Exception("handle oppo failed.");
    }

    private static void handleVivo(Context context) throws Exception {
        openTargetApp(context, "com.iqoo.secure", null);
    }

    private static void handleXiaoMi(Context context) {
        openTargetApp(context, "com.miui.securitycenter",
                "com.miui.permcenter.autostart.AutoStartManagementActivity");
    }

    private static void handleHuaWei(Context context) throws Exception {
        try {
            openTargetApp(context, "com.hihonor.systemmanager", "com.hihonor.systemmanager.appcontrol.activity.StartupAppControlActivity");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            openTargetApp(context, "com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            openTargetApp(context, "com.huawei.systemmanager",
                    "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            openTargetApp(context, "com.huawei.systemmanager",
                    "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new Exception("handle huawei failed.");
    }

    public static void openSetting(Context context) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        context.startActivity(intent);
    }

    public static void openTargetApp(Context context, String packageName, String activityName) {
        Intent intent;
        if (TextUtils.isEmpty(activityName)) {
            intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        } else {
            intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.setComponent(new ComponentName(packageName, activityName));
        }
        context.startActivity(intent);
    }
}