package com.secspace.lib.common.utils;

import android.content.Context;

import java.util.List;

import static com.secspace.lib.common.env.SharedPreferences.Default.INIT_MDM_WHEN_ENTER;
import static com.secspace.lib.common.env.SharedPreferences.Default.REINFORCE;
import static com.secspace.lib.common.env.SharedPreferences.Default.SP_FILE_SECSPACE_COMMON;
import static com.secspace.lib.common.env.SharedPreferences.Default.SP_SECSPACE_HIDE_LAUNCHER_PACKAGES;


public class Prefs {

    private static final String APP_INFO = "app_info";
    public static final String SHOW_SYSTEM_APP_PKGS = "show_system_app_pkgs";
    public static final String SHOW_USER_APP_PKGS = "show_user_app_pkgs";

    public static final String AUTO_START_SELF = "auto_start_self";
    public static final String IN_SPACE = "in_space";
    public static final String ALLOW_NEW_APP = "allow_new_app";
    public static final String WIFI_WHITE_LIST = "wifi_white_list";
    public static final String IS_FIRST_INIT = "is_first_init";
    public static final String LOGIN_PWD = "login_pwd";
    public static final String PWD_PROTECTION_ANSWER_1 = "pwd_protection_answer_1";
    public static final String PWD_PROTECTION_ANSWER_2 = "pwd_protection_answer_2";
    public static final String PWD_PROTECTION_QUESTION_1 = "pwd_protection_question_1";
    public static final String PWD_PROTECTION_QUESTION_2 = "pwd_protection_question_2";
    public static final String OPEN_SILENCE_INSTALL_TOOL = "open_silence_install_tool";
    public static final String IMEI_CAN_USE = "imei_can_sue";
    public static final String LOCK_TIME = "lock_time";
    public static final String LOCK_COUNT = "lock_count";
    public static final String COVER_SECOND_INSTALL = "cover_second_install";
    public static final String SP_PRIVACYPOLICYSTATE = "privacyPolicyState";
    public static final String ENCRYPTED = "encrypted";

    public static void setCoverSecondInstall(Context context, boolean isCoverInstall) {
        putBoolean(context, COVER_SECOND_INSTALL, isCoverInstall);
    }

    public static boolean isCoverSecondInstall(Context context) {
        return getBoolean(context, COVER_SECOND_INSTALL, true);
    }

    public static void setLockCount(Context context, int count) {
        putInt(context, LOCK_COUNT, count);
    }

    public static int getLockCount(Context context) {
        return getInt(context, LOCK_COUNT, 0);
    }

    public static void setLockTime(Context context, long time) {
        putLong(context, LOCK_TIME, time);
    }

    public static long getLockTime(Context context) {
        return getLong(context, LOCK_TIME, 0L);
    }


    /**
     * 设置imei是否可用
     *
     * @param context
     * @param imeiCanUse
     */
    public static void setImeiCanUse(Context context, boolean imeiCanUse) {
        putBoolean(context, IMEI_CAN_USE, imeiCanUse);
    }

    /**
     * 获取imei是否可用
     *
     * @param context
     * @return
     */
    public static boolean isImeiCanUse(Context context) {
        return getBoolean(context, IMEI_CAN_USE, false);
    }

    /**
     * 设置是否打开静默安装卸载工具
     *
     * @param context
     * @param isOpen
     */
    public static void setOpenSilenceInstallTool(Context context, boolean isOpen) {
        putBoolean(context, OPEN_SILENCE_INSTALL_TOOL, isOpen);
    }

    /**
     * 获取是否打开静默安装卸载工具
     *
     * @param context
     * @return
     */
    public static boolean isOpenSilenceInstallTool(Context context) {
        return getBoolean(context, OPEN_SILENCE_INSTALL_TOOL, false);
    }

    /**
     * 设置密保一问题
     *
     * @param context
     * @param question1
     */
    public static void setPwdProtectionQuestion1(Context context, String question1) {
        putString(context, PWD_PROTECTION_QUESTION_1, question1);
    }

    /**
     * 获取密保1问题
     *
     * @param context
     * @return
     */
    public static String getPwdProtectionQuestion1(Context context) {
        return getString(context, PWD_PROTECTION_QUESTION_1, "厂商提供的初始演示激活码是多少?");
    }


    /**
     * 设置密保2问题
     *
     * @param context
     * @param question2
     */
    public static void setPwdProtectionQuestion2(Context context, String question2) {
        putString(context, PWD_PROTECTION_QUESTION_2, question2);
    }

    /**
     * 获取密保2问题
     *
     * @param context
     * @return
     */
    public static String getPwdProtectionQuestion2(Context context) {
        return getString(context, PWD_PROTECTION_QUESTION_2, "厂商所处城区是在哪里?");
    }

    /**
     * 设置密保一答案
     *
     * @param context
     * @param answer1
     */
    public static void setPwdProtectionAnswer1(Context context, String answer1) {
        putString(context, PWD_PROTECTION_ANSWER_1, answer1);
    }

    /**
     * 获取密保1答案
     *
     * @param context
     * @return
     */
    public static String getPwdProtectionAnswer1(Context context) {
        return getString(context, PWD_PROTECTION_ANSWER_1, "1908129SH9");
    }

    /**
     * 获取密保2答案
     *
     * @param context
     * @return
     */
    public static String getPwdProtectionAnswer2(Context context) {
        return getString(context, PWD_PROTECTION_ANSWER_2, "天津市南开区");
    }

    /**
     * 设置密保一答案
     *
     * @param context
     * @param answer2
     */
    public static void setPwdProtectionAnswer2(Context context, String answer2) {
        putString(context, PWD_PROTECTION_ANSWER_2, answer2);
    }


    /**
     * 获取密码
     *
     * @param context
     */
    public static String getLoginPwd(Context context) {
        return getString(context, LOGIN_PWD, "");
    }

    /**
     * 设置密码
     *
     * @param context
     * @param pwd
     */
    public static void setLoginPwd(Context context, String pwd) {
        putString(context, LOGIN_PWD, pwd);
    }

    /**
     * 设置是否首次进入
     *
     * @param context
     * @param isFirstInit
     */
    public static void setIsFirstInit(Context context, boolean isFirstInit) {
        putBoolean(context, IS_FIRST_INIT, isFirstInit);
    }

    /**
     * 获取是否首次进入
     *
     * @param context
     */
    public static boolean isFirstInit(Context context) {
        return getBoolean(context, IS_FIRST_INIT, true);
    }

    /**
     * 设置wifi白名单列表
     *
     * @param context
     * @param stringList
     */
    public static void setWifiWhiteList(Context context, String stringList) {
        putString(context, WIFI_WHITE_LIST, stringList);
    }

    public static String getWifiWhiteList(Context context) {
        return getString(context, WIFI_WHITE_LIST, "");
    }

    /**
     * 获取是否允许本身是否自启
     *
     * @param context
     * @return
     */
    public static boolean isAutoStartSelf(Context context) {
        return getBoolean(context, AUTO_START_SELF, true);
    }

    /**
     * 设置是否允许本身是否自启
     *
     * @param context
     * @param isAutoStartSelf
     */
    public static void setAutoStartSelf(Context context, boolean isAutoStartSelf) {
        putBoolean(context, AUTO_START_SELF, isAutoStartSelf);
    }

    /**
     * 是否允许添加新应用  默认不允许
     *
     * @param context
     * @return
     */
    public static boolean isAllowNewApp(Context context) {
        return getBoolean(context, ALLOW_NEW_APP, false);
    }

    /**
     * 设置是否允许添加新应用
     *
     * @param context
     * @param isAutoStartSelf
     */
    public static void setAllowNewApp(Context context, boolean isAutoStartSelf) {
        putBoolean(context, ALLOW_NEW_APP, isAutoStartSelf);
    }

    /**
     * 获取当前状态
     *
     * @param context
     * @return
     */
    public static boolean isInSpace(Context context) {
        return getBoolean(context, IN_SPACE, true);
    }

    /**
     * 当前状态
     *
     * @param context
     */
    public static void setInSpace(Context context, boolean isInSpace) {
        putBoolean(context, IN_SPACE, isInSpace);
    }

    /**
     * 设置系统显示App包名
     *
     * @param context
     * @param pkgs
     */
    public static void setSystemShowAppPkgs(Context context, List<String> pkgs) {
        putString(context, SHOW_SYSTEM_APP_PKGS, JsonUtil.toJson(pkgs));
    }

    /**
     * 获取系统显示App包名
     *
     * @param context
     */
    public static List<String> getSystemShowAppPkgs(Context context) {
        String json = getString(context, SHOW_SYSTEM_APP_PKGS, "");
        return JsonUtil.json2StringList(json);
    }

    public static void setUserShowAppPkgs(Context context, List<String> pkgs) {
        putString(context, SHOW_USER_APP_PKGS, JsonUtil.toJson(pkgs));
    }

    public static List<String> getUserShowAppPkgs(Context context) {
        String json = getString(context, SHOW_USER_APP_PKGS, "");
        return JsonUtil.json2StringList(json);
    }

    private static void putString(Context context, String key, String value) {
        context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE)
                .edit().putString(key, value).apply();
    }

    private static String getString(Context context, String key, String defValue) {
        String value = context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE).getString(key, defValue);
        return value;
    }

    private static void putBoolean(Context context, String key, boolean value) {
        context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE)
                .edit().putBoolean(key, value).apply();
    }

    private static boolean getBoolean(Context context, String key, boolean defValue) {
        return context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE).getBoolean(key, defValue);
    }

    private static void putLong(Context context, String key, long value) {
        context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE)
                .edit().putLong(key, value).apply();
    }

    private static long getLong(Context context, String key, long defValue) {
        return context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE).getLong(key, defValue);
    }

    private static void putInt(Context context, String key, int value) {
        context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE)
                .edit().putInt(key, value).apply();
    }

    private static int getInt(Context context, String key, int defValue) {
        return context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE).getInt(key, defValue);
    }

    private static void clear(Context context, String key) {
        context.getSharedPreferences(APP_INFO, Context.MODE_PRIVATE).edit().remove(key).apply();
    }

    public static void setHideLauncherPackages(Context ctx, List<String> packages) {
        SPUtil.setParam(ctx, SP_SECSPACE_HIDE_LAUNCHER_PACKAGES, JsonUtil.toJson(packages), SP_FILE_SECSPACE_COMMON);
    }

    public static List<String> getHideLauncherPackages(Context ctx) {
        String json = (String) SPUtil.getParam(ctx, SP_SECSPACE_HIDE_LAUNCHER_PACKAGES, "", SP_FILE_SECSPACE_COMMON);
        return JsonUtil.json2StringList(json);
    }

    public static void setMdmInitializedWhenEnter(Context ctx, boolean init) {
        SPUtil.setParam(ctx, INIT_MDM_WHEN_ENTER, init, SP_FILE_SECSPACE_COMMON);
    }

    public static boolean isMdmInitializedWhenEnter(Context ctx) {
        return (boolean) SPUtil.getParam(ctx, INIT_MDM_WHEN_ENTER, false, SP_FILE_SECSPACE_COMMON);
    }

    public static void setReinforce(Context ctx, boolean init) {
        SPUtil.setParam(ctx, REINFORCE, init, SP_FILE_SECSPACE_COMMON);
    }

    public static boolean isReinforce(Context ctx) {
        return (boolean) SPUtil.getParam(ctx, REINFORCE, false, SP_FILE_SECSPACE_COMMON);
    }

    public static void setPrivacyPolicyState(Context ctx, boolean isAccept) {
        SPUtil.setParam(ctx, SP_PRIVACYPOLICYSTATE, isAccept, SP_FILE_SECSPACE_COMMON);
    }

    public static boolean isPrivacyPolicyAccept(Context ctx) {
        return (boolean) SPUtil.getParam(ctx, SP_PRIVACYPOLICYSTATE, false, SP_FILE_SECSPACE_COMMON);
    }

    public static void setEncrypted(Context ctx, String encrypted) {
        SPUtil.setParam(ctx, ENCRYPTED, encrypted, SP_FILE_SECSPACE_COMMON);
    }

    public static String getEncrypted(Context ctx) {
        return (String) SPUtil.getParam(ctx, ENCRYPTED, "", SP_FILE_SECSPACE_COMMON);
    }
}
