package com.getbubblenow.android.util;

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

public class UserStore {
    private static UserStore instance;
    private 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 USERNAME_KEY = "com.wireguard.android.util.bubbleUSERNAMEResponse";
    private static final String PASSWORD_KEY = "com.wireguard.android.util.bubblePASSWORDResponse";
    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";
    public  static final String USER_TOKEN_DEFAULT_VALUE = "";
    public static final String DEVICE_DEFAULT_VALUE = "";
    public static final String DEVICE_ID_DEFAULT_VALUE = "";
    private static final String HOSTNAME_DEFAULT_VALUE = "";
    public static final String USERNAME_DEFAULT_VALUE = "";
    private static final String PASSWORD_DEFAULT_VALUE = "";
    private 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 = "";

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

        return instance;
    }

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

    public void setToken(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(String deviceName, 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(String hostName){
        sharedPreferences.edit().putString(HOSTNAME_KEY,hostName).apply();
    }

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

    public void setUserData(String username , String password){
        sharedPreferences.edit().putString(USERNAME_KEY, username).apply();
        sharedPreferences.edit().putString(PASSWORD_KEY,password).apply();
    }

    public String getUsername(){
        return sharedPreferences.getString(USERNAME_KEY,USERNAME_DEFAULT_VALUE);
    }

    public String getPassword(){
        return sharedPreferences.getString(PASSWORD_KEY,PASSWORD_DEFAULT_VALUE);
    }

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

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

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

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

    public void setSageHostname(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(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(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);
    }
}
