package com.mints.goodmoney.utils;

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

import com.mints.goodmoney.common.Constant;
import com.mints.library.utils.ConstantUtil;

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

/**
 * 描述：数据存取类
 * 作者：孟崔广
 * 时间：2019/10/29 17:51
 * 邮箱：mengcga@163.com
 */
public class PreferencesService {
	private static SharedPreferences mPreferences;
	private static Context mContext;
	private static String mShareName = "mints_goodmoney";

	private PreferencesService() {
	}

	private static PreferencesService mPreferencesService;

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

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

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

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

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

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

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

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

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

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

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

	/**
	 * 删除所有数据(将第一次打开记录保存下来)
	 */
	public void removeAll() {
		boolean isFirstOpen;
		Editor editor = mPreferences.edit();
		String versionName = ConstantUtil.getVersionName(mContext);
		isFirstOpen = getBoolean(Constant.FIRST_USE_KEY + versionName, false);
		editor.clear();
		editor.commit();
		putBoolean(Constant.FIRST_USE_KEY + versionName, isFirstOpen);
	}

	/**
	 * 秘文加密
	 * @return
	 */
	private String secretEncode(String s){
		return Des3.encode(s,Constant.DEFAULT_CHANNEL_SECRET);
	}

	/**
	 * 秘文解密
	 * @return
	 */
	private String secretDecode(String s){
		return Des3.decode(s, Constant.DEFAULT_CHANNEL_SECRET);
	}

}
