package it.rainbowbreeze.directwire.data;

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

import it.rainbowbreeze.directwire.common.Bag;
import it.rainbowbreeze.directwire.common.LogFacility;

/**
 * Manages app preferences
 * Created by alfredomorresi on 13/03/16.
 */
public class PreferencesDao {
    private static final String TAG = "PreferencesDao";
    private static final String PREF_FILE_NAME = "app_global_preferences";
    private static final String PREFKEY_BOTKEY = "BotKey";
    private static final String PREFKEY_USERNAME = "Username";
    private static final String PREFKEY_CHAT_ROOM = "ChatRoom";  // Room for the moments
    private static final String PREFKEY_SENT_TOKEY_TO_SERVER = "SentToketToServer";

    final Context mAppContext;
    final LogFacility mLog;
    private final SharedPreferences mAppPreferences;

    public PreferencesDao(Context appContext, LogFacility logFacility) {
        mAppContext = appContext;
        mLog = logFacility;

        /**
         * What's the difference between PreferenceManager.getDefaultSharedPreferences(...) and
         *  ApplicationContext.getSharedPreferences(mPrefsFileName, ...);
         *
         *  http://stackoverflow.com/questions/5946135/difference-between-getdefaultsharedpreferences-and-getsharedpreferences
         *
         *  and why getSharedPreferences is better
         *  http://stackoverflow.com/questions/10786172/android-getdefaultsharedpreferences
         */

        mAppPreferences = appContext.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    }

    public String getBotKey() {
        return mAppPreferences.getString(PREFKEY_BOTKEY, null);
    }
    public PreferencesDao setBotKey(String newValue) {
        mAppPreferences.edit().putString(PREFKEY_BOTKEY, newValue).apply();
        return this;
    }

    public String getUsername() {
        return mAppPreferences.getString(PREFKEY_USERNAME, "Unnamed User");
    }
    public PreferencesDao setUsername(String newValue) {
        mAppPreferences.edit().putString(PREFKEY_USERNAME, newValue).apply();
        return this;
    }

    public String getChatRoom() {
        return mAppPreferences.getString(PREFKEY_CHAT_ROOM, Bag.DEFAULT_ROOM_NAME);
    }
    public PreferencesDao setChatRoom(String newValue) {
        mAppPreferences.edit().putString(PREFKEY_CHAT_ROOM, newValue).apply();
        return this;
    }

    public boolean getSentTokenToServer() {
        return mAppPreferences.getBoolean(PREFKEY_SENT_TOKEY_TO_SERVER, false);
    }
    public PreferencesDao setSentTokenToServer(boolean newValue) {
        mAppPreferences.edit().putBoolean(PREFKEY_SENT_TOKEY_TO_SERVER, newValue).apply();
        return this;
    }

}
