package com.secspace.lib.common.utils;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.secspace.lib.common.env.SSettingConfig;
import com.secspace.mdmengine.api.annotation.WhichFactory;
import com.secspace.mdmengine.api.manager.MdmFactoryManager;

import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;

public class DisableCompat {
    private static final String TAG = "DisableCompat";
    public static final String PACKAGENAME_WORKSPACE = "com.safe.workspace";
    public static final String CCOMPONENT_WORKSPACE_ADMIN_CLASS = "ch.deletescape.lawnchair.DeviceAdminReceiver";

    public static void disableComponent(Context context, ComponentName componentName) {
        try {
            PackageManager packageManager = context.getPackageManager();
            packageManager.setComponentEnabledSetting(componentName, COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        } catch (Exception e) {
            Log.e(TAG, "disableComponent Error: " + e.toString());
        }
    }

    public static void enableComponent(Context context, ComponentName componentName) {
        try {
            Log.i(TAG, "DisableCompat-enableComponent: " + componentName.toShortString());
            PackageManager packageManager = context.getPackageManager();
            packageManager.setComponentEnabledSetting(componentName, COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        } catch (Exception e) {
            Log.e(TAG, "enableComponent Error: " + e.toString());
        }
    }

    /**
     * 启用应用
     */
    public static boolean enablePackage(Context context, String packageName, boolean enable) {
        Log.d(TAG, "enable package " + packageName + ", enable " + enable);
        try {
            DevicePolicyManager manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
            if (manager != null) {
                ComponentName componentName = new ComponentName(PACKAGENAME_WORKSPACE, CCOMPONENT_WORKSPACE_ADMIN_CLASS);
                boolean isHidden = manager.isApplicationHidden(componentName, packageName);
                if (enable && !isHidden || !enable && isHidden) {
                    Log.d(TAG, "enable = " + enable + ",isHidden ? " + isHidden);
                    return false;
                }
                return manager.setApplicationHidden(componentName, packageName, !enable);
            }
        } catch (Exception e) {
            Log.e(TAG, "enable package " + packageName + " failed, e = " + Log.getStackTraceString(e));
        }
        return false;
    }

    public static void enableApplication(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return;
        }
        boolean result;
        if (SSettingConfig.isUseDeviceOwnerToDisableApp()) {
            result = DpmHelper.getInstance(context).hideApplication(packageName, false);
        } else {
            result = enableApplicationAOSP(context, packageName);
        }
        Log.i(TAG, "enable: " + packageName + " ,    result= " + result);
    }


    public static void disableApplication(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return;
        }
        boolean result;
        if (SSettingConfig.isUseDeviceOwnerToDisableApp()) {
            result = DpmHelper.getInstance(context).hideApplication(packageName, true);
        } else {
            result = disableApplicationAOSP(context, packageName);
        }
        Log.d(TAG, "disable: " + packageName + " ,      result=" + result);
    }

    private static boolean disableApplicationAOSP(Context context, String packageName) {

        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        MdmFactoryManager.getInstance().switchFactory(WhichFactory.AOSP_SYSTEM);
        MdmFactoryManager.getInstance().getDeviceApplicationManager()
                .setApplicationEnabledSetting(packageName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
        MdmFactoryManager.getInstance().switchFactory(WhichFactory.HUAWEI);
        return true;
    }

    private static boolean enableApplicationAOSP(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            return false;
        }
        MdmFactoryManager.getInstance().switchFactory(WhichFactory.AOSP_SYSTEM);
        MdmFactoryManager.getInstance().getDeviceApplicationManager()
                .setApplicationEnabledSetting(packageName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
        MdmFactoryManager.getInstance().switchFactory(WhichFactory.HUAWEI);
        return true;
    }
}
