package com.android.launcher3.mdm;

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.lib.common.utils.DpmHelper;
import com.secspace.mdmengine.api.annotation.WhichFactory;
import com.secspace.mdmengine.api.manager.MdmFactoryManager;

import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;

public class DisableCompat {
    private static final String TAG = "DisableCompat";

    public static boolean isApplicationEnable(Context context, String packageName) {
        try {
            PackageManager pm = context.getPackageManager();
            int mode = pm.getApplicationEnabledSetting(packageName);
            return mode == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT || mode == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
        } catch (Exception e) {
            Log.e(TAG, "isApplicationEnable Exception: " + e.toString());
            e.printStackTrace();
        }
        return true;
    }

    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;
    }



    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) {

            }

    }

    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) {
        }
    }

    public static boolean isComponentEnable(Context context, ComponentName componentName) {
        try {
            PackageManager packageManager = context.getPackageManager();
            switch (packageManager.getComponentEnabledSetting(componentName)) {
                case COMPONENT_ENABLED_STATE_DEFAULT:
                    packageManager.setComponentEnabledSetting(componentName, COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                    return true;
                case COMPONENT_ENABLED_STATE_ENABLED:
                    return true;
                case COMPONENT_ENABLED_STATE_DISABLED:
                    return false;
                case COMPONENT_ENABLED_STATE_DISABLED_USER:
                    return false;
                case COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED:
                    return true;
                default:
                    packageManager.setComponentEnabledSetting(componentName, COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                    return true;
            }
        } catch (Exception e) {
            Log.e(TAG, "enableComponent Error: " + e.toString());
        }
        return false;
    }
}
