package com.pixelnx.localhelp.utils.sharedpref;

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


import androidx.preference.PreferenceManager;

import com.google.gson.Gson;
import com.pixelnx.localhelp.model.ModelUserData;



public class SharedPref {

   static SharedPreferences myPrefs;
   static SharedPreferences.Editor prefsEditor;
  static SharedPref myObj;

    private SharedPref() {
    }





    public boolean getBooleanValue(String tag) {

        return myPrefs.getBoolean(tag, false);
    }


    public void setBooleanValue(String tag, boolean token) {
        prefsEditor.putBoolean(tag, token);
        prefsEditor.commit();
    }











    public static void clearAllPreferences() {
        prefsEditor = myPrefs.edit();
        prefsEditor.clear();
        prefsEditor.apply();
    }


    public static SharedPref getInstance(Context ctx) {
        if (myObj == null) {
            myObj = new SharedPref();
            myPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            prefsEditor = myPrefs.edit();
            prefsEditor.apply();
        }
        return myObj;
    }


    public void clearPreferences(String key) {
        prefsEditor.remove(key);

        prefsEditor.commit();

    }

    public ModelUserData getUser(String key) {
        Gson gson = new Gson();
        String json = myPrefs.getString("" + key, "");

        return gson.fromJson(json, ModelUserData.class);
    }

    public static  void setUser(String schoolName, ModelUserData modelLogin) {
       prefsEditor = myPrefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(modelLogin);
        prefsEditor.putString(schoolName, json);

        prefsEditor.apply();


    }


}
