package kr.co.hanwha.hifive.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by sanghoonkwon on 2018. 1. 10..
 */

public class HFSharedPreferences {
    public static final String NAME_HIFIVE = "name.hifive";
    public static final String KEY_DEVICE_UUID = "key.device_uuid";

    public static final String KEY_NEED_REFRESH_MAIN_HOME_ASSIGNED = "key.need_refresh_main_home_assigned";
    public static final String KEY_NEED_REFRESH_MAIN_HOME_REQUEST = "key.need_refresh_main_home_request";
    public static final String KEY_NEED_REFRESH_MAIN_PROJECT = "key.need_refresh_main_project";
    public static final String KEY_NEED_REFRESH_MAIN_DASHBOARD = "key.need_refresh_main_dashboard";
    public static final String KEY_NEED_REFRESH_ALARM_LIST = "key.need_refresh_alarm_list";

    public static final String KEY_LAST_MEMBER_LOGIN_ID = "key.last_member_login_id";
    public static final String KEY_MEMBER_LOGIN_ID = "key.member_login_id";
    public static final String KEY_LAST_NOTICE_NO = "key.last_notice_no";
    public static final String KEY_NOTICE_INFO_NO = "key.notice_info.no";
    public static final String KEY_NOTICE_INFO_URL = "key.notice_info.url";

    public static final String KEY_RECENT_KEYWORD_LIST = "key.recent_keyword.list";

    public static final String KEY_CHECK_AUTH_TIME = "key.check.auth.time";

    public static final String KEY_SELECTED_DEPARTMENT = "key.selected_department";

    public static final String KEY_NOTIFICATION_GROUP_ID = "key.notification.group_id";
    public static final String KEY_NOTIFICATION_TITLE = "key.notification.title";
    public static final String KEY_NOTIFICATION_MESSAGE = "key.notification.message";
    public static final String KEY_NOTIFICATION_TYPE = "key.notification.type";
    public static final String KEY_NOTIFICATION_VALUE_1 = "key.notification.value1";
    public static final String KEY_NOTIFICATION_VALUE_2 = "key.notification.value2";
    public static final String KEY_NOTIFICATION_VALUE_3 = "key.notification.value3";
    public static final String KEY_NOTIFICATION_DB_CODE = "key.notification.db_code";

    public static final String KEY_DEFAULT_SCREEN = "key.default.screen";

    public static void setInt(Context context, String key, int value) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static int getInt(Context context, String key) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        return pref.getInt(key, -1);
    }

    public static void setLong(Context context, String key, long value) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static long getLong(Context context, String key) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        return pref.getLong(key, -1);
    }


    public static void setBoolean(Context context, String key, boolean value) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    public static boolean getBoolean(Context context, String key, boolean def) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        return pref.getBoolean(key, def);
    }

    public static void setString(Context context, String key, String value) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static void addStringSet(Context context, String key, String value) {
        Set<String> set = getStringSet(context, key, value);
        set.add(value);
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putStringSet(key, set);
        editor.apply();
    }

    public static boolean hasStringSet(Context context, String key, String value) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        Set<String> set = pref.getStringSet(key, null);
        if (set != null && set.contains(value)) {
            return true;
        }
        return false;
    }

    public static Set<String> getStringSet(Context context, String name, String key) {
        SharedPreferences pref = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return pref.getStringSet(key, new HashSet<String>());
    }

    public static String getString(Context context, String key) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        return pref.getString(key, "");
    }

    public static void setStringArrayPref(Context context, String key, ArrayList<String> values) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, null);

        JSONArray a = new JSONArray();
        for (int i = 0; i < values.size(); i++) {
            a.put(values.get(i));
        }

        if (!values.isEmpty()) {
            editor.putString(key, a.toString());
        } else {
            editor.putString(key, null);
        }

        editor.apply();
    }

    public static ArrayList<String> getStringArrayPref(Context context, String key) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String json = prefs.getString(key, null);
        ArrayList<String> urls = new ArrayList<>();
        if (json != null) {
            try {
                JSONArray a = new JSONArray(json);
                for (int i = 0; i < a.length(); i++) {
                    String url = a.optString(i);
                    urls.add(url);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return urls;
    }


    public static void removeKey(Context context, String key) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.remove(key);
        editor.apply();
    }

    public static void clear(Context context) {
        SharedPreferences pref = context.getSharedPreferences(NAME_HIFIVE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        editor.commit();
    }
}
