package com.ez.ertc.meeting.utils;

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

import com.ez.ertc.meeting.ErtcEzmHelper;

/**
 *
 */
public class SPUtil {
    private final static String SETTING_PREFERENCES = "setting_preferences";

    /**
     * 调用此方法时注意要保存一些不能清空的数据
     */
    public static void cleanAllValues(){
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        editor.apply();
    }

    // 获取保存字符串类型信息
    public static String getStringValue(String key, String defaultValue) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        String value = pref.getString(key, defaultValue);
        return value;
    }

    public static void setStringValue(String key, String value) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(key, value);
        editor.apply();
    }


    // 获取保存布尔类型信息
    public static Boolean getBooleanValue(String key, boolean defaultValue) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        Boolean value = pref.getBoolean(key, defaultValue);
        return value;
    }

    public static void setBooleanValue(String key, Boolean value) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(key, value == null ? false : value);
        editor.apply();
    }


    // 获取保存整型类型信息
    public static int getIntValue(String key, int defaultValue) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        int value = pref.getInt(key, defaultValue);
        return value;
    }

    public static void setIntValue(String key, int value) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(key, value);
        editor.apply();
    }


    // 获取保存长整形类型信息
    public static long getLongValue(String key, long defaultValue) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        long value = pref.getLong(key, defaultValue);
        return value;
    }

    public static void setLongValue(String key, long value) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putLong(key, value);
        editor.apply();
    }


    // 获取保存浮点类型信息
    public static float getFloatValue(String key, float defaultValue) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        float value = pref.getFloat(key, defaultValue);
        return value;
    }

    public static void setFloatValue(String key, float value) {
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putFloat(key, value);
        editor.apply();
    }



    /**
     * 清空某些数据
     */
    public static void removeValues(String key){
        SharedPreferences pref = ErtcEzmHelper.getContext().getSharedPreferences(SETTING_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.remove(key);
        editor.apply();
    }

}
