package br.com.codetec.saudesabor.utils;

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

import com.google.gson.Gson;

import br.com.codetec.saudesabor.ui.login.LoginActivity;

/**
 * Created by SPEEDWAYTI BRASILIA on 28/03/17.
 */


public class SharedPreferenceUtil {

    public static String LOGIN_ULTIMO = "usuario_login_string";
    public static String SENHA_ULTIMO = "usuario_senha_string";

    private SharedPreferenceUtil() {
        /*Hider constructor.*/
    }

    //String Shared Preferences
    public static boolean setStringPreference(Context context, String key, String value) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences != null && !TextUtils.isEmpty(key)) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(key, value);
            return editor.commit();
        }
        return false;
    }

    public static String getStringPreference(Context context, String key) {
        String value = null;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences != null) {
            value = preferences.getString(key, null);
        }

        return value;
    }


    //Boolean Shared Preferences
    public static boolean getBooleanPreference(Context context, String key, boolean defaultValue) {
        boolean value = defaultValue;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences != null) {
            value = preferences.getBoolean(key, defaultValue);
        }

        return value;
    }

    public static boolean setBooleanPreference(Context context, String key, boolean value) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences != null) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean(key, value);
            return editor.commit();
        }

        return false;
    }

    //Int Shared Preferences
    public static int getInPreference(Context context, String key, int defaultValue) {
        int value = defaultValue;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences != null) {
            value = preferences.getInt(key, defaultValue);
        }
        return value;
    }

    public static int setIntPreference(Context context, String key, int value) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences != null) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt(key, value);
            editor.commit();
        }
        return 0;

    }


    public static boolean setObjectPreference(Context context, String key, Object obj){
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences != null && !TextUtils.isEmpty(key)) {
            SharedPreferences.Editor editor = preferences.edit();
            try {
                String objJson = new Gson().toJson(obj);
                editor.putString(key, objJson);
                return editor.commit();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }


    public static <T> T getObjectPreference(Context context, String key,  Class<T> classOfT) {
        String value = null;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences != null) {
            value = preferences.getString(key, null);
            if(value!=null)
                return (T) new Gson().fromJson(value,  classOfT);
        }
        return null;
    }

}
