package com.yidian.shenghuoquan.newscontent.utils;

import android.Manifest;
import android.app.Activity;
import android.app.AppOpsManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author sun on 2018/7/5.
 */
public class FloatWindowPermissionManager {
    public static final int REQUEST_PERMISSION_OVERLAY = 100;

    public static final String TAG = "FloatWindowParamManager";

    public static boolean checkPermission(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return Settings.canDrawOverlays(context);
        }
        return checkOps(context);
    }

    public static boolean checkOverlayPermission(Context context) {
        AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        try {
            if (appOpsMgr != null) {
                int mode = appOpsMgr.checkOpNoThrow("android:system_alert_window",
                        android.os.Process.myUid(), context.getPackageName());
                if (mode == 0) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    private static boolean checkOps(Context context) {
        try {
            Object object = context.getSystemService(Context.APP_OPS_SERVICE);
            if (object == null) {
                return false;
            }
            Class localClass = object.getClass();
            Class[] arrayOfClass = new Class[3];
            arrayOfClass[0] = Integer.TYPE;
            arrayOfClass[1] = Integer.TYPE;
            arrayOfClass[2] = String.class;
            Method method = localClass.getMethod("checkOp", arrayOfClass);
            if (method == null) {
                return false;
            }
            Object[] arrayOfObject1 = new Object[3];
            arrayOfObject1[0] = 24;
            arrayOfObject1[1] = Binder.getCallingUid();
            arrayOfObject1[2] = context.getPackageName();
            int m = (Integer) method.invoke(object, arrayOfObject1);
            return m == AppOpsManager.MODE_ALLOWED || !RomUtils.isDomesticSpecialRom();
        } catch (Exception ignore) {
        }
        return false;
    }

    public static boolean tryJumpToPermissionPage(Activity context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            switch (RomUtils.getRomName()) {
                case RomUtils.ROM_MIUI:
                    return applyMiuiPermission(context);
                case RomUtils.ROM_EMUI:
                    return applyHuaweiPermission(context);
                case RomUtils.ROM_VIVO:
                    return applyVivoPermission(context);
                case RomUtils.ROM_OPPO:
                    return applyOppoPermission(context);
                case RomUtils.ROM_QIKU:
                    return apply360Permission(context);
                case RomUtils.ROM_SMARTISAN:
                    return applySmartisanPermission(context);
                case RomUtils.ROM_COOLPAD:
                    return applyCoolpadPermission(context);
                case RomUtils.ROM_ZTE:
                    return applyZTEPermission(context);
                case RomUtils.ROM_LENOVO:
                    return applyLenovoPermission(context);
                case RomUtils.ROM_LETV:
                    return applyLetvPermission(context);
                default:
                    return true;
            }
        } else {
            if (RomUtils.isMeizuRom()) {
                return getAppDetailSettingIntent(context);
            } else if (RomUtils.isVivoRom()) {
                return applyVivoPermission(context);
            } else if (RomUtils.isMiuiRom()) {
                return applyMiuiPermission(context) || getAppDetailSettingIntent(context);
            } else {
                return applyCommonPermission(context);
            }
        }
    }

    private static boolean startActivitySafely(Intent intent, Activity context) {
        try {
            if (isIntentAvailable(intent, context)) {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivityForResult(intent,REQUEST_PERMISSION_OVERLAY);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            Log.e(TAG, "启动Activity失败！！！！！！");
            return false;
        }
    }

    public static boolean isIntentAvailable(Intent intent, Activity context) {
        return intent != null && context.getPackageManager().queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
    }

    private static boolean applyCommonPermission(Activity context) {
        try {
            Class clazz = Settings.class;
            Field field = clazz.getDeclaredField("ACTION_MANAGE_OVERLAY_PERMISSION");
            Intent intent = new Intent(field.get(null).toString());
            intent.setData(Uri.parse("package:" + context.getPackageName()));
            startActivitySafely(intent, context);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private static boolean applyCoolpadPermission(Activity context) {
        Intent intent = new Intent();
        intent.setClassName("com.yulong.android.seccenter",
                "com.yulong.android.seccenter.dataprotection.ui.AppListActivity");
        return startActivitySafely(intent, context);
    }

    private static boolean applyLenovoPermission(Activity context) {
        Intent intent = new Intent();
        intent.setClassName("com.lenovo.safecenter",
                "com.lenovo.safecenter.MainTab.LeSafeMainActivity");
        return startActivitySafely(intent, context);
    }

    private static boolean applyZTEPermission(Activity context) {
        Intent intent = new Intent();
        intent.setAction("com.zte.heartyservice.intent.action.startActivity.PERMISSION_SCANNER");
        return startActivitySafely(intent, context);
    }

    private static boolean applyLetvPermission(Activity context) {
        Intent intent = new Intent();
        intent.setClassName("com.letv.android.letvsafe",
                "com.letv.android.letvsafe.AppActivity");
        return startActivitySafely(intent, context);
    }

    private static boolean applyVivoPermission(Activity context) {
        Intent intent = new Intent();
        intent.putExtra("packagename", context.getPackageName());
        intent.setAction("com.vivo.permissionmanager");
        intent.setClassName("com.vivo.permissionmanager",
                "com.vivo.permissionmanager.activity.SoftPermissionDetailActivity");
        ComponentName componentName1 = intent.resolveActivity(context.getPackageManager());
        if (componentName1 != null) {
            return startActivitySafely(intent, context);
        }

        intent.setAction("com.iqoo.secure");
        intent.setClassName("com.iqoo.secure",
                "com.iqoo.secure.safeguard.SoftPermissionDetailActivity");
        ComponentName componentName2 = intent.resolveActivity(context.getPackageManager());
        if (componentName2 != null) {
            return startActivitySafely(intent, context);
        }

        intent.setAction("com.iqoo.secure");
        intent.setClassName("com.iqoo.secure", "com.iqoo.secure.MainActivity");
        ComponentName componentName3 = intent.resolveActivity(context.getPackageManager());
        if (componentName3 != null) {
            return startActivitySafely(intent, context);
        }

        return startActivitySafely(intent, context);
    }

    private static boolean applyOppoPermission(Activity context) {
        Intent intent = new Intent();
        intent.putExtra("packageName", context.getPackageName());
        intent.setAction("com.oppo.safe");
        intent.setClassName("com.oppo.safe",
                "com.oppo.safe.permission.PermissionAppListActivity");
        if (!startActivitySafely(intent, context)) {
            intent.setAction("com.color.safecenter");
            intent.setClassName("com.color.safecenter",
                    "com.color.safecenter.permission.floatwindow.FloatWindowListActivity");
            if (!startActivitySafely(intent, context)) {
                intent.setAction("com.coloros.safecenter");
                intent.setClassName("com.coloros.safecenter",
                        "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity");
                return startActivitySafely(intent, context);
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    private static boolean apply360Permission(Activity context) {
        Intent intent = new Intent();
        intent.setClassName("com.android.settings",
                "com.android.settings.Settings$OverlaySettingsActivity");
        if (!startActivitySafely(intent, context)) {
            intent.setClassName("com.qihoo360.mobilesafe",
                    "com.qihoo360.mobilesafe.ui.index.AppEnterActivity");
            return startActivitySafely(intent, context);
        } else {
            return true;
        }
    }

    private static boolean applyMiuiPermission(Activity context) {
        Intent intent = new Intent();
        intent.setAction("miui.intent.action.APP_PERM_EDITOR");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.putExtra("extra_pkgname", context.getPackageName());
        return startActivitySafely(intent, context);
    }

    public static boolean getAppDetailSettingIntent(Activity context) {
        Intent localIntent = new Intent();
        localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        return startActivitySafely(localIntent, context);
    }

    private static boolean applyMeizuPermission(Activity context) {
        Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
        intent.setClassName("com.meizu.safe",
                "com.meizu.safe.security.AppSecActivity");
        intent.putExtra("packageName", context.getPackageName());
        return startActivitySafely(intent, context);
    }

    private static boolean applyHuaweiPermission(Activity context) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager",
                    "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");
            intent.setComponent(comp);
            if (!startActivitySafely(intent, context)) {
                comp = new ComponentName("com.huawei.systemmanager",
                        "com.huawei.notificationmanager.ui.NotificationManagmentActivity");
                intent.setComponent(comp);
                context.startActivity(intent);
                return true;
            } else {
                return true;
            }
        } catch (SecurityException e) {
            try {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName comp = new ComponentName("com.huawei.systemmanager",
                        "com.huawei.permissionmanager.ui.MainActivity");
                intent.setComponent(comp);
                context.startActivity(intent);
                return true;
            } catch (Exception e1) {
                Log.e(TAG, "Huawei跳转失败1" + e1);
                return getAppDetailSettingIntent(context);
            }
        } catch (ActivityNotFoundException e) {
            try {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName comp = new ComponentName("com.Android.settings",
                        "com.android.settings.permission.TabItem");
                intent.setComponent(comp);
                context.startActivity(intent);
                return true;
            } catch (Exception e2) {
                Log.e(TAG, "Huawei跳转失败2" + e);
                return getAppDetailSettingIntent(context);
            }
        } catch (Exception e) {
            return getAppDetailSettingIntent(context);
        }
    }

    private static boolean applySmartisanPermission(Activity context) {
        Intent intent = new Intent("com.smartisanos.security.action.SWITCHED_PERMISSIONS_NEW");
        intent.setClassName("com.smartisanos.security",
                "com.smartisanos.security.SwitchedPermissions");
        intent.putExtra("index", 17); //有版本差异,不一定定位正确
        if (startActivitySafely(intent, context)) {
            return true;
        } else {
            intent = new Intent("com.smartisanos.security.action.SWITCHED_PERMISSIONS");
            intent.setClassName("com.smartisanos.security",
                    "com.smartisanos.security.SwitchedPermissions");
            intent.putExtra("permission", new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW});

            return startActivitySafely(intent, context);
        }
    }
}
