package com.leroymerlin.pandroid.persistence;

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

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * Created by adrienleroy on 05/01/15.
 */
public class SharedPrefConfigurationManager implements ConfigurationManager {

    private final SharedPreferences preferenceFile;
    private final Gson gsonParser;


    public SharedPrefConfigurationManager(Context context, String preferenceFileName) {
        preferenceFile = context.getSharedPreferences(preferenceFileName, Context.MODE_PRIVATE);
        gsonParser = new GsonBuilder().create();

    }

    public void remove(Config config) {
        preferenceFile.edit().remove(config.getName()).apply();
    }

    public <T> T getConfig(Config field) {
        if (field.getType() == Integer.class)
            return (T)(Object)preferenceFile.getInt(field.getName(), ((int) field.getDefaultValue()));
        else if (field.getType() == Long.class)
            return (T) (Object)preferenceFile.getLong(field.getName(), ((Long) field.getDefaultValue()));
        else if (field.getType() == Float.class)
            return (T) (Object)preferenceFile.getFloat(field.getName(), ((Float) field.getDefaultValue()));
        else if (field.getType() == String.class)
            return (T) (Object) preferenceFile.getString(field.getName(), ((String) field.getDefaultValue()));
        else if (field.getType() == Boolean.class)
            return (T) (Object) preferenceFile.getBoolean(field.getName(), ((Boolean) field.getDefaultValue()));
        else{
            if(preferenceFile.contains(field.getName())){
                return (T) parseString(preferenceFile.getString(field.getName(), null), field.getType());
            }else{
                return (T) field.getDefaultValue();
            }

        }
    }

    public void setConfig(Config field, Object object) {
        SharedPreferences.Editor editor = preferenceFile.edit();
        if (field.getType() == Integer.class)
            editor.putInt(field.getName(), (Integer) object);
        else if (field.getType() == Long.class)
            editor.putLong(field.getName(), (Long) object);
        else if (field.getType() == String.class)
            editor.putString(field.getName(), (String) object);
        else if (field.getType() == Boolean.class)
            editor.putBoolean(field.getName(), (Boolean) object);
        else if (field.getType() == Float.class)
            editor.putFloat(field.getName(), (Float) object);
        else {
            editor.putString(field.getName(), objectToString(object));
        }
        editor.apply();
    }

    public String objectToString(Object object) {
        if (object == null) {
            return null;
        } else {
            return gsonParser.toJson(object);
        }
    }

    public <T> T parseString(String value, Class<T> a) {
        if (value == null) {
            return null;
        } else {
            return gsonParser.fromJson(value, a);
        }
    }


}

@@ -40,16 +40,56 @@
 tence;%0A%0A
+import android.annotation.SuppressLint;%0A
 import a
@@ -112,16 +112,16 @@
 ontext;%0A
-
 import a
@@ -221,16 +221,39 @@
 ilder;%0A%0A
+import java.util.Map;%0A%0A
 /**%0A * C
@@ -1913,32 +1913,497 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Retrieve all values from the preferences.%0A     */%0A    public Map%3CString, ?%3E getAll() %7B%0A        return preferenceFile.getAll();%0A%0A    %7D%0A%0A    /**%0A     *  return size of sharedPref%0A     * @return%0A     */%0A    public int getSize() %7B%0A        return preferenceFile.getAll().size();%0A    %7D%0A%0A    /**%0A     *%0A     * Clear SharedPreference%0A     *%0A     */%0A    @SuppressLint(%22CommitPrefEdits%22)%0A    public void clear() %7B%0A        preferenceFile.edit().clear();%0A    %7D%0A%0A
     public void 
@@ -3198,21 +3198,22 @@
 %7D%0A%0A    p
-ublic
+rivate
  String 
@@ -3380,29 +3380,31 @@
     %7D%0A%0A    p
-ublic
+rivate 
  %3CT%3E T parse
@@ -3579,8 +3579,5 @@
   %7D%0A
-%0A%0A%7D%0A
+%7D

fix (SharedPrefConfigurationManager) add getAll, clear, getSize methods.