package com.duben.microtribe.utils;

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

import java.util.Map;
import java.util.Set;

/**
 * 本地SharedPreferences工具(不加密公用数据)
 * <p>
 * mengcuiguang
 */
public class SPUtil {

    private static SharedPreferences mPreferences;
    private static Context mContext;
    private static String mShareName = "cashpie_config";

    private SPUtil() {
    }

    private static SPUtil spUtil;

    public static SPUtil getInstance(Context context) {
        if (spUtil == null) {
            mContext = context;
            spUtil = new SPUtil();
            mPreferences = mContext.getSharedPreferences(mShareName, Context.MODE_PRIVATE);
        }
        return spUtil;
    }

    /**
     * 批量保存
     */
    public void saveInfo(Map<String, String> info) {
        SharedPreferences.Editor editor = mPreferences.edit();
        Set<String> keys = info.keySet();
        for (String str : keys) {
            editor.putString(str, info.get(str));
        }
        editor.commit();
    }

    /**
     * 保存string类型数据
     */
    public void putString(String key, String value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 根据key读取参数
     */
    public String getString(String key) {
        return mPreferences.getString(key, "");
    }

    /**
     * 保存long类型数据
     */
    public void putLong(String key, long value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    /**
     * 保存long类型数据
     */
    public long getLong(String key) {
        return mPreferences.getLong(key, -1);
    }

    /**
     * 保存int类型数据
     *
     * @param
     * @param value
     */
    public void putInt(String key, int value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 获取int类型数据
     *
     * @param key
     * @return
     */
    public int getInt(String key) {
        return mPreferences.getInt(key, -1);
    }

    /**
     * 读取状态
     */
    public boolean getBoolean(String key, boolean def) {
        return mPreferences.getBoolean(key, def);
    }

    /**
     * 读取状态
     */
    public void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 根据Key删除某个数据
     */
    public void removeByKey(String key) {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.remove(key);
        editor.commit();
    }

    /**
     * 删除所有数据
     */
    public void removeAll() {
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.clear();
        editor.commit();
    }


    /**
     * 获取本地图片流
     */
    public byte[] getIdcardImg(String key) {
        String idcardImg = mPreferences.getString(key, "");

        byte[] bitmapArray = null;
        try {
            bitmapArray = android.util.Base64.decode(idcardImg, android.util.Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmapArray;

    }
}
