package com.sec.tool;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.secspace.lib.common.utils.DeviceUtil;
import com.secspace.lib.common.utils.DpmHelper;
import com.secspace.lib.common.utils.ImeiCheckUtils;
import com.secspace.lib.common.utils.PkgManager;
import com.secspace.lib.common.utils.Prefs;
import com.secspace.mdmengine.api.manager.MdmFactoryManager;

import java.util.List;

import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MIDDLEWARE;

public class MainActivity extends AppCompatActivity {
    AlertDialog mTipDialog;
    public static final String TAG = "MainActivity";
    ComponentName componentName8 = new ComponentName("com.huawei.android.hwouc", "com.huawei.android.hwouc.ui.activities.MainEntranceActivity");
    public static ComponentName huaweiLuancher = new ComponentName("com.huawei.android.launcher", "com.huawei.android.launcher.unihome.UniHomeLauncher");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImeiCheckUtils.getInstance(this).initImeiList();
        startService(new Intent(this,MyService.class));
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: "+checkImei());
        if (!checkImei()) {
            showPwdDialog(getString(R.string.not__prmission), getString(R.string.work_sure), null, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mTipDialog = null;
                    dialog.dismiss();
                    finish();
                }
            }, null);
            return;
        }
        try {
            DpmHelper.getInstance(this).initMiddleWare();
            if (!DpmHelper.getInstance(this).isMiddlewareDPMActived()) {
                DpmHelper.getInstance(this).toActivateMiddlewareDpm();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean checkImei() {
        String imei = DeviceUtil.getIMEI(this);
        Log.i(TAG, "checkImei: " + imei);
        if (Prefs.isImeiCanUse(this) || ImeiCheckUtils.getInstance(this).getmImeiList().contains(imei)) {
            Prefs.setImeiCanUse(this, true);
            return true;
        }
        return false;
    }

    /**
     * dialog
     *
     * @param title
     * @param positiveText
     * @param negativeText
     * @param positiveClickListener
     * @param negativeClickListener
     */
    private void showPwdDialog(String title, String positiveText, String negativeText, DialogInterface.OnClickListener positiveClickListener, DialogInterface.OnClickListener negativeClickListener) {
        if (mTipDialog != null && mTipDialog.isShowing()) {
            mTipDialog.dismiss();
        }
        View view = LayoutInflater.from(this).inflate(R.layout.item_ll, null);
        LinearLayout ll = view.findViewById(R.id.item_ll);
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(title)
                .setCancelable(false)
                .setView(view);
        if (positiveClickListener != null) {
            builder.setPositiveButton(positiveText, positiveClickListener);
        }
        if (negativeClickListener != null) {
            builder.setNegativeButton(negativeText, negativeClickListener);
        }
        if (mTipDialog == null) {
            mTipDialog = builder.create();
        }
        if (!mTipDialog.isShowing()) {
            mTipDialog.show();
        }
    }

    public void remove_forbidden(View view) {
        DisableCompat.enableComponent(this, componentName8);
        MdmFactoryManager.getInstance().getDeviceSettingsManager().setRestoreFactoryDisabled(false);
        MdmFactoryManager.getInstance().getDeviceSettingsManager().clearDefaultLauncher(this.getPackageName());
        DisableCompat.enableComponent(this, huaweiLuancher);
    }

    public void uninstall_mobile_steward(View view) {
        MdmFactoryManager.getInstance().getDeviceRestrictionManager().clearDeviceOwnerApp();
        List<String> disabledDeactivateMdmPackageList = MdmFactoryManager.getInstance().getDeviceApplicationManager().getDisabledDeactivateMdmPackageList();
        if (disabledDeactivateMdmPackageList != null && disabledDeactivateMdmPackageList.size() > 0) {
            MdmFactoryManager.getInstance().getDeviceApplicationManager().removeDisabledDeactivateMdmPackages(disabledDeactivateMdmPackageList);
        }
        List<String> disallowedUninstallPackageList = MdmFactoryManager.getInstance().getDeviceApplicationManager().getDisallowedUninstallPackageList();
        if (disallowedUninstallPackageList != null && disallowedUninstallPackageList.size() > 0) {
            MdmFactoryManager.getInstance().getDeviceApplicationManager().removeDisallowedUninstallPackages(disallowedUninstallPackageList);
        }
        MdmFactoryManager.getInstance().getDeviceSettingsManager().removeActiveAdmin(new ComponentName(PACKAGENAME_MIDDLEWARE, "com.sec.middleware.receiver.DpmReceiver"));
        PkgManager.getInstance(this).uninstallByInstaller("com.secspace.mobilemanager");
        PkgManager.getInstance(this).uninstallByInstaller(PACKAGENAME_MIDDLEWARE);
    }
}
