package kr.co.icontrols.iotplatform.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPrefUtil {
    private static SharedPreferences mPreferences;
    private final static String PREF_NAME = "kr.co.icontrols.iotplatform";


    public final static String USER_ID = "user_id";
    public final static String USER_PASS = "user_pass";
    public final static String AUTO_LOGIN = "auto_login";
    public final static String NOTI_ALL = "noti_all";
    public final static String NOTI_ADD_DEVICE = "noti_add_device";
    public final static String NOTI_DELETE_DEVICE = "noti_delete_member";
    public final static String NOTI_ADD_MEMBER = "noti_add_member";
    public final static String NOTI_REMOVE_MEMBER = "noti_remove_member";
    public final static String SHOW_SMART_GUIDE = "shwo_smart_guide"; //최초진입 가이드 표시
    public final static String PUSH_TOKEN = "push_token";
    public final static String LOGIN_TIME = "login_time";
    public final static String BACKGROUND_WAIT_TIME = "background_wait_time"; //앱 백그라운드 대기시간

    public final static String DEFAULT_USER_ID = "";
    public final static String DEFAULT_USER_PASS = "";
    public final static boolean DEFAULT_AUTO_LOGIN = false;
    public final static boolean DEFAULT_NOTI_ALL = false;
    public final static boolean DEFAULT_NOTI_ADD_DEVICE = false;
    public final static boolean DEFAULT_NOTI_DELETE_DEVICE = false;
    public final static boolean DEFAULT_NOTI_ADD_MEMBER = false;
    public final static boolean DEFAULT_NOTI_REMOVE_MEMBER = false;
    public final static boolean DEFAULT_SHOW_SMART_GUIDE = true;
    public final static String DEFAULT_PUSH_TOKEN = "";
    public final static long DEFAULT_LOGIN_TIME = 0;
    public final static long DEFAULT_BACKGROUND_WAIT_TIME = 0;

    public static void init(Context context) {
        if (mPreferences == null) {
            mPreferences = context.getSharedPreferences(PREF_NAME, Activity.MODE_PRIVATE);
        }
    }

    public static void reset() {
        if (mPreferences == null) return;
        putString(USER_ID,DEFAULT_USER_ID);
        putString(USER_PASS,DEFAULT_USER_PASS);
        putBoolean(AUTO_LOGIN, DEFAULT_AUTO_LOGIN);
        putBoolean(NOTI_ALL, DEFAULT_NOTI_ALL);
        putBoolean(NOTI_ADD_DEVICE, DEFAULT_NOTI_ADD_DEVICE);
        putBoolean(NOTI_DELETE_DEVICE, DEFAULT_NOTI_DELETE_DEVICE);
        putBoolean(NOTI_ADD_MEMBER, DEFAULT_NOTI_ADD_MEMBER);
        putBoolean(NOTI_REMOVE_MEMBER, DEFAULT_NOTI_REMOVE_MEMBER);
        putBoolean(SHOW_SMART_GUIDE, DEFAULT_SHOW_SMART_GUIDE);
        putString(PUSH_TOKEN,DEFAULT_PUSH_TOKEN);
        putLong(LOGIN_TIME, DEFAULT_LOGIN_TIME);
        putLong(BACKGROUND_WAIT_TIME, DEFAULT_BACKGROUND_WAIT_TIME);
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        if (mPreferences == null) return defaultValue;
        return mPreferences.getBoolean(key, defaultValue);
    }

    public static void putBoolean(String key, boolean Value) {
        if (mPreferences == null) return;
        SharedPreferences.Editor mEditer = mPreferences.edit();
        mEditer.putBoolean(key, Value);
        mEditer.commit();
    }

    public static String getString(String key, String defaultValue) {
        if (mPreferences == null) return defaultValue;
        return mPreferences.getString(key, defaultValue);
    }

    public static void putString(String key, String value) {
        if (mPreferences == null) return;

        SharedPreferences.Editor mEditer = mPreferences.edit();
        mEditer.putString(key, value);
        mEditer.commit();
    }

    public static int getInt(String key, int defaultValue) {
        if (mPreferences == null) return defaultValue;
        return mPreferences.getInt(key, defaultValue);
    }

    public static void putInt(String key, int value) {
        if (mPreferences == null) return;
        SharedPreferences.Editor mEditer = mPreferences.edit();
        mEditer.putInt(key, value);
        mEditer.commit();
    }

    public static long getLong(String key, long defaultValue) {
        if (mPreferences == null) return defaultValue;
        return mPreferences.getLong(key, defaultValue);
    }

    public static void putLong(String key, long value) {
        if (mPreferences == null) return;
        SharedPreferences.Editor mEditer = mPreferences.edit();
        mEditer.putLong(key, value);
        mEditer.commit();
    }

    public static float getFloat(String key, float defaultValue) {
        if (mPreferences == null) return defaultValue;
        return mPreferences.getFloat(key, defaultValue);
    }

    public static void putFloat(String key, float value) {
        if (mPreferences == null) return;
        SharedPreferences.Editor mEditer = mPreferences.edit();
        mEditer.putFloat(key, value);
        mEditer.commit();
    }
}
