package com.tjzhxx.union.public_store;

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

import java.util.Set;

/**
 * 使用方法：
 * SharedPreferences preferences = getSharedPreferences("user", Context.MODE_PRIVATE);
 * BasePreferences basePreferences = new BasePreferences(preferences);
 */
public class BasePreferences {

    public final static String SP_FILE_NAME = "app_user";
    public final static String APP_USER_OBJ = "app_user_obj";
    public final static String APP_USER_PWD = "app_user_pwd";
    public final static String APP_IS_REMEMBER = "app_remember";

    public final static String SP_FILE_MANAGER = "app_managers";
    public final static String KEY_FOOD_MANAGER = "FoodManagers";
    public final static String KEY_PROPERTY_MANAGER = "PropertyManagers";

    protected SharedPreferences mPreferences;

    public static BasePreferences getAppUser(Context context){
        SharedPreferences preferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
        BasePreferences basePreferences = new BasePreferences(preferences);
        return basePreferences;
    }

    public static BasePreferences getAppManager(Context context){
        SharedPreferences preferences = context.getSharedPreferences(SP_FILE_MANAGER, Context.MODE_PRIVATE);
        BasePreferences basePreferences = new BasePreferences(preferences);
        return basePreferences;
    }

    public BasePreferences(SharedPreferences sharedPreferences) {
        mPreferences = sharedPreferences;
    }

    public boolean putInt(String key, int value) {
        return mPreferences.edit().putInt(key, value).commit();
    }

    public void putApplyInt(String key, int value){
        mPreferences.edit().putInt(key, value).apply();
    }

    public boolean putLong(String key, long value) {
        return mPreferences.edit().putLong(key, value).commit();
    }

    public void putApplyLong(String key, long value) {
        mPreferences.edit().putLong(key, value).apply();
    }

    public boolean putFloat(String key, float value) {
        return mPreferences.edit().putFloat(key, value).commit();
    }

    public void putApplyFloat(String key, float value) {
        mPreferences.edit().putFloat(key, value).apply();
    }

    public boolean putString(String key, String value) {
        return mPreferences.edit().putString(key, value).commit();
    }

    public void putApplyString(String key, String value) {
        mPreferences.edit().putString(key, value).apply();
    }

    public boolean putSetString(String key, Set<String> value) {
        return mPreferences.edit().putStringSet(key, value).commit();
    }

    public void putApplySetString(String key, Set<String> value) {
        mPreferences.edit().putStringSet(key, value).apply();
    }

    public boolean putBoolean(String key, boolean value) {
        return mPreferences.edit().putBoolean(key, value).commit();
    }

    public void putApplyBoolean(String key, boolean value) {
        mPreferences.edit().putBoolean(key, value).apply();
    }

    public String getString(String key, String defValue) {
        return mPreferences.getString(key, defValue);
    }

    public boolean getBoolean(String key, boolean defValue) {
        return mPreferences.getBoolean(key, defValue);
    }

    public int getInt(String key, int defValue) {
        return mPreferences.getInt(key, defValue);
    }

    public long getLong(String key, long defValue) {
        return mPreferences.getLong(key, defValue);
    }

    public float getFloat(String key, float defValue){
        return mPreferences.getFloat(key, defValue);
    }

    public Set<String> getSetString(String key, Set<String> defValue){
        return mPreferences.getStringSet(key, defValue);
    }

    public boolean contains(String key){
        return mPreferences.contains(key);
    }

    public void registerOnChangeListener(OnSharedPreferenceChangeListener listener){
        if (listener!=null)
        mPreferences.registerOnSharedPreferenceChangeListener(listener);
    }

    public void unregisterOnChangeListener(OnSharedPreferenceChangeListener listener){
        if (listener!=null)
        mPreferences.unregisterOnSharedPreferenceChangeListener(listener);
    }


}
