package hf.lskj.com.carstockcount.utils;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created on 2016/8/2.
 * Author:crs
 * Description:首选项工具类
 */
public class PreferencesUtils {
    //存储数据需要editor对象，取数据直接首选项对象就好了。
    private static final String SHARE_PREFER_NAME = "config";
    private static SharedPreferences mSharedPreferences;


    //-----------------------------put开始-------------------------
    public static void putBoolean(Context context, String key, boolean value) {
        putBoolean(context, key, value, SHARE_PREFER_NAME);
    }

    private static void putBoolean(Context context, String key, boolean value, String fileName) {
        if (mSharedPreferences == null) {
            //私有模式
            mSharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static void putString(Context context, String key, String value) {
        putString(context, key, value, SHARE_PREFER_NAME);
    }

    private static void putString(Context context, String key, String value, String fileName) {
            mSharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = mSharedPreferences.edit();
        edit.putString(key, value);
        edit.apply();
    }

    //-----------------------------以下是Get--------------------
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        return getBoolean(context, key, defaultValue, SHARE_PREFER_NAME);
    }

    private static boolean getBoolean(Context context, String key, boolean defaultValue, String fileName) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        }
        //如果获取不到就显示默认值
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    public static String getString(Context context, String key) {
        return getString(context, key, "");
    }

    public static String getString(Context context, String key, String defaultValue) {
        return getString(context, key, defaultValue, SHARE_PREFER_NAME);
    }

    private static String getString(Context context, String key, String defaultValue, String fileName) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        }
        //如果获取不到就显示默认值
        return mSharedPreferences.getString(key, defaultValue);
    }

    public static void clearDefault(Context context) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(SHARE_PREFER_NAME, Context.MODE_PRIVATE);
        }
        final SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.clear();
        editor.apply();
    }

}