package com.example.mynotepad;

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

import com.example.mynotepad.MenuFeatures.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class SharedPreferencesClass {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;


    public void changeTheTheme(Activity activity) {
        sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        editor.putString(activity.getString(R.string.saved_high_score_key), Utils.sTheme).apply();
    }

    public void getThemeOnMainActivityCreate(Activity activity) {
        SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
        Utils.sTheme = sharedPref.getString(activity.getString(R.string.saved_high_score_key), "bright");
    }

    public void saveArrayFolders(Activity activity ,ArrayList<String> list, String key) {
        sharedPreferences = activity.getSharedPreferences(key ,Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        editor.remove(key);
        editor.putString(key, json);
        editor.apply();
    }

    public ArrayList<String> getArrayFolders(Activity activity ,String key) {
        sharedPreferences = activity.getSharedPreferences(key ,Context.MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString(key, null);
        System.out.println(json+"+++++++++++++++++++++++++++++++++++++++++++++++");
        Type type = new TypeToken<ArrayList<String>>() {}.getType();
        return gson.fromJson(json, type);
    }
}
