package com.younki.maidmatching.services.SharedPreference;

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

public class Preferences {
    static final String KEY_EMAIL = "email",
                        KEY_USER_ID = "user_id",
                        KEY_PASSWORD = "pass",
                        KEY_USERNAME = "username",
                        KEY_ROLE = "role",
                        KEY_STATUS = "is_logged_in",
                        KEY_USER_STATUS = "user_status",
                        KEY_USER_SKILL = "user_skill",
                        KEY_USER_LOCATION = "user_location",
                        KEY_TOKEN = "******";

    private static SharedPreferences getSharedPreference(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public static void setEmail(Context context, String email) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putString(KEY_EMAIL, email);
        editor.apply();
    }

    public static String getEmail(Context context) {
        return getSharedPreference(context).getString(KEY_EMAIL, "");
    }

    public static void setUserId(Context context, int user_id) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putInt(KEY_USER_ID, user_id);
        editor.apply();
    }

    public static int getUserId(Context context) {
        return getSharedPreference(context).getInt(KEY_USER_ID, 0);
    }

    public static void setPassword(Context context, String password) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putString(KEY_PASSWORD, password);
        editor.apply();
    }

    public static String getPassword(Context context) {
        return getSharedPreference(context).getString(KEY_PASSWORD, "");
    }

    public static void setUsername(Context context, String username) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putString(KEY_USERNAME, username);
        editor.apply();
    }

    public static String getUsername(Context context) {
        return getSharedPreference(context).getString(KEY_USERNAME, "");
    }

    public static void setRole(Context context, int role) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putInt(KEY_ROLE, role);
        editor.apply();
    }

    public static int getRole(Context context) {
        return getSharedPreference(context).getInt(KEY_ROLE, 0);
    }

    public static void setStatus(Context context, boolean status) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putBoolean(KEY_STATUS, status);
        editor.apply();
    }

    public static Boolean getStatus(Context context) {
        return getSharedPreference(context).getBoolean(KEY_STATUS, false);
    }

    public static void setToken(Context context, String token) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putString(KEY_TOKEN, token);
        editor.apply();
    }

    public static String getToken(Context context) {
        return getSharedPreference(context).getString(KEY_TOKEN, "");
    }

    public static void setUserStatus(Context context, int status) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putInt(KEY_USER_STATUS, status);
        editor.apply();
    }

    public static int getUserStatus(Context context) {
        return getSharedPreference(context).getInt(KEY_USER_STATUS, 1);
    }

    public static void setUserSkills(Context context, int skill) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putInt(KEY_USER_SKILL, skill);
        editor.apply();
    }

    public static int getUserSkills(Context context) {
        return getSharedPreference(context).getInt(KEY_USER_SKILL, 0);
    }

    public static void setUserLocation(Context context, String location) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.putString(KEY_USER_LOCATION, location);
        editor.apply();
    }

    public static String getUserLocation(Context context) {
        return getSharedPreference(context).getString(KEY_USER_LOCATION, null);
    }

    public static void clearLoggedInUser(Context context) {
        SharedPreferences.Editor editor = getSharedPreference(context).edit();
        editor.remove(KEY_EMAIL);
        editor.remove(KEY_PASSWORD);
        editor.remove(KEY_STATUS);
        editor.remove(KEY_USERNAME);
        editor.remove(KEY_TOKEN);
        editor.remove(KEY_USER_STATUS);
        editor.remove(KEY_USER_SKILL);
        editor.remove(KEY_USER_LOCATION);
        editor.apply();
    }
}
