package com.oo.seex.oo_statistics_lib.utils;

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

/**
 * Created by efort on 15/8/24.
 * 用来做本地存储的一个工具类
 */
public class SharePreferenceUtils {
    private SharedPreferences spf;
    private static SharePreferenceUtils spUtils = null;

    private SharePreferenceUtils(Context context) {
        spf = context.getSharedPreferences("STATISTIC", Context.MODE_PRIVATE);
    }

    /**
     * 获取工具类对象
     *
     * @param context
     * @return
     */
    public static SharePreferenceUtils getInstance(Context context) {
        if (spUtils == null) {
            spUtils = new SharePreferenceUtils(context);
        }
        return spUtils;
    }

    /**
     * 存储String类型的数据
     *
     * @param code 存储标记
     * @param info 存储信息
     */
    public void saveString(String code, String info) {
        spf.edit().putString(code, info).commit();
    }

    /**
     * 取String数据
     *
     * @param code
     * @param defult
     * @return
     */
    public String getString(String code, String defult) {
        return spf.getString(code, defult);
    }

    /**
     * 存储Boolean类型的数据
     *
     * @param code 存储标记
     * @param info 存储信息
     */
    public void saveBoolean(String code, boolean info) {
        spf.edit().putBoolean(code, info).commit();
    }

    /**
     * 取Boolean数据
     *
     * @param code
     * @param defult
     * @return
     */
    public boolean getBoolean(String code, boolean defult) {
        return spf.getBoolean(code, defult);
    }

    /**
     * 存储int类型的数据
     *
     * @param code 存储标记
     * @param info 存储信息
     */
    public void saveInteger(String code, int info) {
        spf.edit().putInt(code, info).commit();
    }

    /**
     * 取int数据
     *
     * @param code
     * @param defult
     * @return
     */
    public int getInteger(String code, int defult) {
        return spf.getInt(code, defult);
    }

    public void setDeviceId(String id) {
        saveString("DEVICE_ID", id);
    }

    public String getDeviceId() {
        return getString("DEVICE_ID", "");
    }

    public void setLoginState() {
        saveBoolean("ISFIRSTLOGIN", false);
    }

    public boolean isFirstLogin() {
        return getBoolean("ISFIRSTLOGIN", true);
    }

    public void saveLong(String code, long l) {
        spf.edit().putLong(code, l).commit();
    }

    public long getLong(String code, long l) {
        return spf.getLong(code, l);
    }
}
