package com.secspace.lib.common.utils

import android.content.ComponentName
import android.content.Context
import com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MDMENHANCE
import com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MIDDLEWARE
import com.secspace.lib.common.utils.Package.COMPONENT_ADMIN_CLASS
import com.secspace.lib.common.utils.Package.ComponentNames.COMPONENT_NAME_HW_HWOUC
import com.secspace.lib.common.utils.Package.ComponentNames.huaweiLuancher
import com.secspace.lib.common.utils.Package.PACKAGENAME_HW_HWOUC
import com.secspace.mdmengine.api.annotation.WhichFactory
import com.secspace.mdmengine.api.manager.MdmFactoryManager

object UninstallUtil{

    fun removeForbidden(context: Context?) {
        DisableCompat.enableComponent(context, huaweiLuancher)
        MdmFactoryManager.getInstance().switchFactory(WhichFactory.HUAWEI)
        DisableCompat.enableComponent(context, COMPONENT_NAME_HW_HWOUC)
        MdmFactoryManager.getInstance().deviceSettingsManager.isSystemUpdateDisabled = false
        MdmFactoryManager.getInstance().deviceSettingsManager.isRestoreFactoryDisabled = false
        MdmFactoryManager.getInstance().deviceRestrictionManager.isStatusBarExpandPanelDisabled = false
        MdmFactoryManager.getInstance().googlePolicyManager?.setStatusBarDisabled(false)
        MdmFactoryManager.getInstance().googlePolicyManager?.screenCaptureDisabled = false
        MdmFactoryManager.getInstance().deviceSettingsManager.clearDefaultLauncher(context?.packageName)

        MdmFactoryManager.getInstance().deviceRestrictionManager.clearDeviceOwnerApp()
        val disabledDeactivateMdmPackageList = MdmFactoryManager.getInstance().deviceApplicationManager.disabledDeactivateMdmPackageList
        if (disabledDeactivateMdmPackageList != null && disabledDeactivateMdmPackageList.size > 0) {
            MdmFactoryManager.getInstance().deviceApplicationManager.removeDisabledDeactivateMdmPackages(disabledDeactivateMdmPackageList)
        }
        val disallowedUninstallPackageList = MdmFactoryManager.getInstance().deviceApplicationManager.disallowedUninstallPackageList
        if (disallowedUninstallPackageList != null && disallowedUninstallPackageList.size > 0) {
            MdmFactoryManager.getInstance().deviceApplicationManager.removeDisallowedUninstallPackages(disallowedUninstallPackageList)
        }
        // 取消激活
        MdmFactoryManager.getInstance().deviceSettingsManager.removeActiveAdmin(ComponentName(PACKAGENAME_MIDDLEWARE, COMPONENT_ADMIN_CLASS))
        MdmFactoryManager.getInstance().deviceSettingsManager.removeActiveAdmin(ComponentName(PACKAGENAME_MDMENHANCE, COMPONENT_ADMIN_CLASS))

        MdmFactoryManager.getInstance().deviceRestrictionManager.clearDeviceOwnerApp()
        DisableCompat.enableComponent(context, huaweiLuancher)
    }

    fun uninstallService(context: Context?) {
        if (PkgManagerHelper.getInstance(context).isAppInstalled(context?.packageName)) {
            PkgManagerHelper.getInstance(context).uninstallByInstaller(context?.packageName)
        }
        if (PkgManagerHelper.getInstance(context).isAppInstalled(PACKAGENAME_MIDDLEWARE)) {
            PkgManagerHelper.getInstance(context).uninstallByInstaller(PACKAGENAME_MIDDLEWARE)
        }
    }
}