package com.sjkj.base_lib.net.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

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

public class PrefrenceTool {

    public PrefrenceTool() {
    }

    public static int getValue(String cfgFile, String cfgName, Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        return preference.getInt(cfgName, -1);
    }

    public static boolean getBooleanValue(String cfgFile, String cfgName, Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        return preference.getBoolean(cfgName, false);
    }

    public static String getStringValue(String cfgFile, String cfgName,
                                        Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        return preference.getString(cfgName, "");
    }

    public static Set<String> getStringSet(String cfgFile, String cfgName,
                                           Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        return preference.getStringSet(cfgName, new HashSet<String>());
    }

    public static long getLongValue(String cfgFile, String cfgName,
                                    Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        return preference.getLong(cfgName, -1);
    }

    public static <T> T getObjectValue(String cfgFile, String cfgName, Class<T> tClass,
                                       Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        String jsonString = preference.getString(cfgName, "");

        if (TextUtils.isEmpty(jsonString))
            return null;
        else
            return (new JsonSerializer()).deserialize(jsonString, tClass);
    }

    public static <T> T getObjectValue(String cfgFile, String cfgName, Type type,
                                       Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        String jsonString = preference.getString(cfgName, "");

        if (TextUtils.isEmpty(jsonString))
            return null;
        else
            return (new JsonSerializer()).deserialize(jsonString, type);
    }

    public static void saveValue(String cfgFile, String cfgName, int cfgValue,
                                 Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putInt(cfgName, cfgValue);
        editor.commit();
    }

    public static void saveValue(String cfgFile, String cfgName, long cfgValue,
                                 Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putLong(cfgName, cfgValue);
        editor.commit();
    }

    public static void saveValue(String cfgFile, String cfgName,
                                 boolean cfgValue, Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putBoolean(cfgName, cfgValue);
        editor.commit();
    }

    public static void saveValue(String cfgFile, String cfgName,
                                 float cfgValue, Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putFloat(cfgName, cfgValue);
        editor.commit();
    }

    public static void saveValue(String cfgFile, String cfgName,
                                 String cfgValue, Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putString(cfgName, cfgValue);
        editor.commit();
    }

    public static void saveValue(String cfgFile, String cfgName,
                                 Set<String> cfgValue, Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putStringSet(cfgName, cfgValue);
        editor.commit();
    }

    public static void saveValue(String cfgFile, String cfgName,
                                 Object object, Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();

        JsonSerializer jsonSerializer = new JsonSerializer();
        String cfgValue = jsonSerializer.serialize(object);

        editor.putString(cfgName, cfgValue);
        editor.commit();
    }

    public static void removeValue(String cfgFile, String cfgName,
                                   Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.remove(cfgName);
        editor.commit();
    }

    public static void removeValues(String cfgFile, String[] cfgNames,
                                    Context context) {
        SharedPreferences preference = context.getSharedPreferences(cfgFile,
                Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        for (String name : cfgNames) {
            editor.remove(name);
        }
        editor.commit();
    }


}
