package com.secspace.lib.common.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.secspace.lib.common.base.BaseActivity;
import com.secspace.lib.common.compat.BuildCompat;
import com.secspace.lib.common.env.Intents;
import com.secspace.lib.common.env.Packages;
import com.secspace.lib.common.function.ExitService;

import static com.secspace.lib.common.env.Intents.EXTRA_FORM_WHRER;
import static com.secspace.lib.common.env.Intents.Launcher.COMPONENT_LAUNCHER_CLASS;
import static com.secspace.lib.common.env.Intents.Pwd.COMPONENT_LOCK_CLASS;
import static com.secspace.lib.common.env.Packages.CompHuaWei.COMPONENTNAME_HW_WIFI_SETTING;
import static com.secspace.lib.common.env.Packages.PkgSystem.PACKAGENAME_SYSTEM_SETTINGS;
import static com.secspace.lib.common.utils.Package.PACKAGENAME_MIDDLEWARE;

/**
 * Created by SKR-CYW on 2017/9/20.
 */

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

    public static void startSystemLauncher(Context context) {
        Intent home = new Intent();
        if (BuildCompat.isEMUI()) {
            home.setPackage(Packages.PkgHuaWei.PACKAGENAME_HW_LAUNCHER);
        }
        home.setAction(Intent.ACTION_MAIN);
        home.addCategory(Intent.CATEGORY_HOME);
        home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        safeStartActivity(context, home);
    }

    public static void startHomeIntent(Context context) {
        Intent home = new Intent();
        home.setAction(Intent.ACTION_MAIN);
        home.addCategory(Intent.CATEGORY_HOME);
        home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        safeStartActivity(context, home);
    }

    public static void startEduLauncher(Context context) {
        Intent intent = new Intent();
        intent.setAction("android.start_launcher");
        intent.setPackage(context.getPackageName());
        safeStartActivity(context, intent);
    }

    public static void startSecSpaceLauncher(Context context, boolean fromManual) {
        Intent launcher = new Intent();
        launcher.setPackage(context.getPackageName());
        launcher.setComponent(new ComponentName(context.getPackageName(), COMPONENT_LAUNCHER_CLASS));
        launcher.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        launcher.putExtra(BaseActivity.INTENT_FROM_MANUAL, fromManual);
        safeStartActivity(context, launcher);
    }

    public static void activateMdmServiceDeviceOwner(Context context) {
        Intent intent = new Intent("com.mdm.action.IGNITE_MIDDLEWARE");
        intent.setPackage(PACKAGENAME_MIDDLEWARE);
        intent.putExtra("cmd", "setDeviceOwnerApp");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        safeStartActivity(context, intent);
    }

    public static void startExitService(Context context) {
        context.startService(new Intent(context, ExitService.class));
    }

    public static void startWifiSetting(Context context) {
        Intent launcher = new Intent();
        launcher.setPackage(PACKAGENAME_SYSTEM_SETTINGS);
        launcher.setComponent(COMPONENTNAME_HW_WIFI_SETTING);
        launcher.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        safeStartActivity(context, launcher);
    }

    private static void safeStartActivity(Context context, Intent intent){
        try{
            context.startActivity(intent);
        }catch (Exception e){

        }
    }
}
