package com.getbubblenow.android.util;

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

public class UserStore {
    private static UserStore instance;
    private final SharedPreferences sharedPreferences;

    public static final String USER_SHARED_PREF = "com.wireguard.android.util.bubbleUserSharedPref";
    private static final String USER_DATA_KEY = "com.wireguard.android.util.bubbleUserResponse";
    private static final String DEVICE_DATA_KEY = "com.wireguard.android.util.bubbleDeviceResponse";
    private static final String DEVICE_ID_KEY = "com.wireguard.android.util.bubbleDeviceIDResponse";
    private static final String HOSTNAME_KEY = "com.wireguard.android.util.bubbleHostnameResponse";
    private static final String SAGE_TOKEN_KEY = "com.wireguard.android.util.bubbleSageTokenResponse";
    private static final String CONFIG_KEY = "com.wireguard.android.util.bubbleConfigResponse";
    private static final String SAGE_HOSTNAME_KEY = "com.wireguard.android.util.bubbleSageHostNameResponse";
    private static final String SAGE_KEY = "com.wireguard.android.util.bubbleSageResponse";
    private static final String HOSTNAME_FOR_ACCOUNT_KEY = "com.wireguard.android.util.bubbleHostNameForAccountResponse";
    private static final String MFA_TOKEN_KEY = "MFA_TOKEN_KEY";
    private static final String LOCALIZATION_PRE_KEY = "LOCALIZATION_PRE_KEY";
    private static final String LOCALIZATION_POST_KEY = "LOCALIZATION_POST_KEY";
    private static final String NETWORK_UUID_KEY = "NETWORK_UUID_KEY";

    public static final String USER_TOKEN_DEFAULT_VALUE = "";
    public static final String DEVICE_DEFAULT_VALUE = "";
    private static final String DEVICE_ID_DEFAULT_VALUE = "";
    private static final String HOSTNAME_DEFAULT_VALUE = "";
    public static final String SAGE_TOKEN_DEFAULT_VALUE = "";
    private static final String CONFIG_DEFAULT_VALUE = "";
    private static final String SAGE_HOSTNAME_DEFAULT_VALUE = "";
    private static final String SAGE_DEFAULT_VALUE = "";
    private static final String HOSTNAME_FOR_ACCOUNT_DEFAULT_VALUE = "";
    private static final String MFA_TOKEN_DEFAULT_VALUE = "";
    private static final String LOCALIZATION_PRE_DEFAULT_VALUE = "";
    private static final String LOCALIZATION_POST_DEFAULT_VALUE = "";
    private static final String NETWORK_UUID_DEFAULT_VALUE = "";

    public static UserStore getInstance(final Context context) {
        if (instance == null) {
            synchronized (UserStore.class) {
                if (instance == null) {
                    instance = new UserStore(context);
                }
            }
        }
        return instance;
    }

    private UserStore(final Context context) {
        sharedPreferences = context.getSharedPreferences(USER_SHARED_PREF, Context.MODE_PRIVATE);
    }

    public void setNetworkUuid(final String uuid) {
        sharedPreferences.edit().putString(NETWORK_UUID_KEY, uuid).apply();
    }

    public String getNetworkUuid() {
        return sharedPreferences.getString(NETWORK_UUID_KEY, NETWORK_UUID_DEFAULT_VALUE);
    }

    public void setToken(final String response) {
        sharedPreferences.edit().putString(USER_DATA_KEY, response).apply();
    }

    public String getToken() {
        return sharedPreferences.getString(USER_DATA_KEY, USER_TOKEN_DEFAULT_VALUE);
    }

    public void setDevice(final String deviceName, final String deviceID) {
        sharedPreferences.edit().putString(DEVICE_DATA_KEY, deviceName).apply();
        sharedPreferences.edit().putString(DEVICE_ID_KEY, deviceID).apply();
    }

    public String getDeviceName() {
        return sharedPreferences.getString(DEVICE_DATA_KEY, DEVICE_DEFAULT_VALUE);
    }

    public String getDeviceID() {
        return sharedPreferences.getString(DEVICE_ID_KEY, DEVICE_ID_DEFAULT_VALUE);
    }

    public void setHostname(final String hostName) {
        sharedPreferences.edit().putString(HOSTNAME_KEY, hostName).apply();
    }

    public String getHostname() {
        return sharedPreferences.getString(HOSTNAME_KEY, HOSTNAME_DEFAULT_VALUE);
    }

    public void setMfaToken(final String mfaToken) {
        sharedPreferences.edit().putString(MFA_TOKEN_KEY, mfaToken).apply();
    }

    public String getMfaTokenKey() {
        return sharedPreferences.getString(MFA_TOKEN_KEY, MFA_TOKEN_DEFAULT_VALUE);
    }

    public void setSageURL(final String sage){
        sharedPreferences.edit().putString(SAGE_KEY, sage).apply();
    }

    public String getSageURL() {
        return sharedPreferences.getString(SAGE_KEY, SAGE_DEFAULT_VALUE);
    }

    public void setConfig(final String config) {
        sharedPreferences.edit().putString(CONFIG_KEY, config).apply();
    }

    public String getConfig() {
        return sharedPreferences.getString(CONFIG_KEY, CONFIG_DEFAULT_VALUE);
    }

    public void setSageHostname(final String sageHostname) {
        sharedPreferences.edit().putString(SAGE_HOSTNAME_KEY, sageHostname).apply();
    }

    public String getSageHostname() {
        return sharedPreferences.getString(SAGE_HOSTNAME_KEY, SAGE_HOSTNAME_DEFAULT_VALUE);
    }

    public void setSageToken(final String sageToken) {
        sharedPreferences.edit().putString(SAGE_TOKEN_KEY, sageToken).apply();
    }

    public String getSageToken() {
        return sharedPreferences.getString(SAGE_TOKEN_KEY, SAGE_TOKEN_DEFAULT_VALUE);
    }

    public void setHostNameForAccount(final String hostNameForAccount) {
        sharedPreferences.edit().putString(HOSTNAME_FOR_ACCOUNT_KEY, hostNameForAccount).apply();
    }

    public String getHostNameForAccount() {
        return sharedPreferences.getString(HOSTNAME_FOR_ACCOUNT_KEY, HOSTNAME_FOR_ACCOUNT_DEFAULT_VALUE);
    }

    public void setLocalizationPre(final String data) {
        sharedPreferences.edit().putString(LOCALIZATION_PRE_KEY, data).apply();
    }

    public String getLocalizationPre() {
        return sharedPreferences.getString(LOCALIZATION_PRE_KEY, LOCALIZATION_PRE_DEFAULT_VALUE);
    }

    public void setLocalizationPost(final String data) {
        sharedPreferences.edit().putString(LOCALIZATION_POST_KEY, data).apply();
    }

    public String getLocalizationPost() {
        return sharedPreferences.getString(LOCALIZATION_POST_KEY, LOCALIZATION_POST_DEFAULT_VALUE);
    }
}
