package com.e16din.lightutils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;

import com.google.gson.Gson;

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

public final class DataManager {

    private Context context;
    private Gson gson = new Gson();

    private static class Holder {
        public static final DataManager HOLDER_INSTANCE = new DataManager();
    }

    private DataManager() {
    }

    public static void init(final Context context) {
        if (Holder.HOLDER_INSTANCE.context == null) {
            Holder.HOLDER_INSTANCE.context = context.getApplicationContext();
        }
    }

    public static DataManager getInstance() {
        return Holder.HOLDER_INSTANCE;
    }

    public SharedPreferences getDefaultSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }

    public <T> void save(final String key, final T data) {
        final SharedPreferences.Editor editor = getDefaultSharedPreferences().edit();
        editor.putString(key, gson.toJson(data));
        editor.commit();
    }

    public void save(final String key, final boolean data) {
        final SharedPreferences.Editor editor = getDefaultSharedPreferences().edit();
        editor.putBoolean(key, data);
        editor.commit();
    }

    public void save(final String key, final String data) {
        final SharedPreferences.Editor editor = getDefaultSharedPreferences().edit();
        editor.putString(key, data);
        editor.commit();
    }

    public void save(final String key, final int data) {
        final SharedPreferences.Editor editor = getDefaultSharedPreferences().edit();
        editor.putInt(key, (int) data);
        editor.commit();
    }

    public void save(final String key, final long data) {
        final SharedPreferences.Editor editor = getDefaultSharedPreferences().edit();
        editor.putLong(key, (long) data);
        editor.commit();
    }

    public void save(final String key, final float data) {
        final SharedPreferences.Editor editor = getDefaultSharedPreferences().edit();
        editor.putFloat(key, (float) data);
        editor.commit();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void save(final String key, final Set<String> data) {
        final SharedPreferences.Editor editor = getDefaultSharedPreferences().edit();
        editor.putStringSet(key, data);
        editor.commit();
    }

    public <T> T load(final String key, final Type type) {
        final String string = getDefaultSharedPreferences().getString(key, null);
        if (string == null) {
            return null;
        }
        return gson.fromJson(string, type);
    }

    public String loadString(final String key) {
        return getDefaultSharedPreferences().getString(key, null);
    }

    public boolean loadBool(final String key) {
        return getDefaultSharedPreferences().getBoolean(key, false);
    }

    public int loadInt(final String key) {
        return getDefaultSharedPreferences().getInt(key, -1);
    }

    public long loadLong(final String key) {
        return getDefaultSharedPreferences().getLong(key, -1l);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public Set<String> loadStringSet(final String key) {
        return getDefaultSharedPreferences().getStringSet(key, null);
    }

    public float loadFloat(final String key) {
        return getDefaultSharedPreferences().getFloat(key, -1f);
    }

    public void clear() {
        final SharedPreferences.Editor editor = getDefaultSharedPreferences().edit();
        editor.clear();
        editor.commit();
    }
}

@@ -212,16 +212,60 @@
 anager;%0D
+%0Aimport android.support.annotation.NonNull;%0D
 %0A%0D%0Aimpor
@@ -641,16 +641,25 @@
 id init(
+@NonNull 
 final Co
@@ -1061,32 +1061,160 @@
 ext);%0D%0A    %7D%0D%0A%0D%0A
+    public boolean contains(@NonNull final String key) %7B%0D%0A        return getDefaultSharedPreferences().contains(key);%0D%0A    %7D%0D%0A%0D%0A
     public %3CT%3E v
@@ -1214,32 +1214,41 @@
 c %3CT%3E void save(
+@NonNull 
 final String key
@@ -1452,32 +1452,41 @@
 ublic void save(
+@NonNull 
 final String key
@@ -1684,32 +1684,41 @@
 ublic void save(
+@NonNull 
 final String key
@@ -1914,32 +1914,41 @@
 ublic void save(
+@NonNull 
 final String key
@@ -2144,32 +2144,41 @@
 ublic void save(
+@NonNull 
 final String key
@@ -2377,32 +2377,41 @@
 ublic void save(
+@NonNull 
 final String key
@@ -2668,16 +2668,25 @@
 id save(
+@NonNull 
 final St
@@ -2907,16 +2907,25 @@
  T load(
+@NonNull 
 final St
@@ -3188,16 +3188,25 @@
 dString(
+@NonNull 
 final St
@@ -3323,16 +3323,25 @@
 oadBool(
+@NonNull 
 final St
@@ -3455,16 +3455,25 @@
 loadInt(
+@NonNull 
 final St
@@ -3582,16 +3582,25 @@
 oadLong(
+@NonNull 
 final St
@@ -3770,16 +3770,25 @@
 ringSet(
+@NonNull 
 final St
@@ -3872,32 +3872,32 @@
 ull);%0D%0A    %7D%0D%0A%0D%0A
-
     public float
@@ -3907,16 +3907,25 @@
 adFloat(
+@NonNull 
 final St

add method contains, add @NonNull annotations to key parameters