package com.stm.asset.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.stm.asset.base.ConfigKey;

/**
 * 类名称：PropertyUtil
 * 类描述：配置工具类
 * 创建人：story
 * 创建时间：2021-08-07 09:32
 */
public class PropertyUtil {

    private static final String PREFERENCES_SYSTEM = "preferences_system";
    private static final String KEY_IS_FIRST_BEGIN = "key_is_first_begin"; // 首次进入
    private static final String KEY_REMEMBER_ACCOUNT = "key_remember_account"; // 上次登录用户名
    private static final String KEY_HTTP_ADDRESS = "key_http_address"; // Http Address
    private static final String KEY_HTTP_PORT = "key_http_port"; // Http Port
    private static final String KEY_HTTP_PLATFORM = "key_http_platform"; // Http Platform
    private static final String KEY_USE_HTTPS = "key_use_https"; // 是否启用Https
    private static final String KEY_ENCRYPT_PWD = "key_encrypt_pwd"; // 加密密钥
    private static final String KEY_ACCESS_TOKEN = "key_access_token"; // 加密token

    public static boolean isFirstBegin(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        return preferences.getBoolean(KEY_IS_FIRST_BEGIN, true);
    }

    public static void setFirstBegin(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        preferences.edit().putBoolean(KEY_IS_FIRST_BEGIN, false).apply();
    }

    public static String getRememberAccount(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        return preferences.getString(KEY_REMEMBER_ACCOUNT, "");
    }

    public static void setRememberAccount(Context context, String account) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_IS_FIRST_BEGIN, account).apply();
    }

    public static String getHttpAddress(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        String address = preferences.getString(KEY_HTTP_ADDRESS, "");
        if (address == null || "".equals(address)) {
            address = ConfigKey.GATEWAYIP;
        }
        return address;
    }

    public static void setHttpAddress(Context context, String address) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_HTTP_ADDRESS, address).apply();
    }

    public static String getHttpPort(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        String port = preferences.getString(KEY_HTTP_PORT, "");
        if (port == null || "".equals(port)) {
            port = ConfigKey.GATWAYPORT;
        }
        return port;
    }

    public static void setHttpPort(Context context, String port) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_HTTP_PORT, port).apply();
    }

    public static String getHttpPlatform(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        String platform = preferences.getString(KEY_HTTP_PLATFORM, "");
        if (platform == null || "".equals(platform)) {
            platform = ConfigKey.PLAYFORM;
        }
        return platform;
    }

    public static void setHttpPlatform(Context context, String platform) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_HTTP_PLATFORM, platform).apply();
    }

    public static String getEncryptPwd(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        return preferences.getString(KEY_ENCRYPT_PWD, "");
    }

    public static void setEncryptPwd(Context context, String sign) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_ENCRYPT_PWD, sign).apply();
    }
    public static boolean isUseHttps(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        String isUse = preferences.getString(KEY_USE_HTTPS, "");
        if ("aa".equals(isUse)) {
            return true;
        } else if ("bb".equals(isUse)) {
            return false;
        }
        return ConfigKey.ISOPENHTTPS;
    }

    public static void setUseHttps(Context context, boolean isUse) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_USE_HTTPS, isUse ? "aa" : "bb").apply();
    }

    public static String getAccessToken(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        return preferences.getString(KEY_ACCESS_TOKEN, "");
    }

    public static void setAccessToken(Context context, String accessToken) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_SYSTEM, Context.MODE_PRIVATE);
        preferences.edit().putString(KEY_ACCESS_TOKEN, accessToken).apply();
    }

}
