package com.sheep.jiuyan.samllsheep.utils;

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

import com.sheep.jiuyan.samllsheep.SheepApp;

/**
 * Created by kemllor on 2017/12/21.
 */

public class SpUtils {

    public static String SKIP_LOGIN_FLAG = "skip_login";

    public static String getToken(Context context) {
        SharedPreferences user = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        return user.getString("wx_openId", "");
    }

    public static void saveToken(Context context, String openId) {
        SharedPreferences user = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = user.edit();
        editor.putString("wx_openId", openId);
        editor.apply();
    }

    /**
     * 存入一些小型的用户数据
     *
     * @param key   ,
     * @param value ,
     */
    public static void saveOrder(String key, int value) {
        SharedPreferences user = SheepApp.getInstance().getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = user.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    /**
     * 获取user表的用户数据
     *
     * @param key ，
     * @return value
     * ，
     */
    public static boolean getFirst(String key) {
        SharedPreferences user = SheepApp.getInstance().getSharedPreferences("user", Context.MODE_PRIVATE);
        return user.getBoolean(key, false);
    }
    /**
     * 存入一些小型的用户数据
     *
     * @param key   ,
     * @param value ,
     */
    public static void saveFirst(String key, boolean value) {
        SharedPreferences user = SheepApp.getInstance().getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = user.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * 获取user表的用户数据
     *
     * @param key ，
     * @return value
     * ，
     */
    public static int getOrder(String key) {
        SharedPreferences user = SheepApp.getInstance().getSharedPreferences("user", Context.MODE_PRIVATE);
        return user.getInt(key, 0);
    }


    /**
     * 点击事件 记录,是否在提示
     *
     * @param context
     * @param vaule   0：提示 1：这一次不提示 2：永远不提示
     */
    public static void saveDownNotice(Context context, int vaule) {
        SharedPreferences user = context.getSharedPreferences("btn_download_notice", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = user.edit();
        editor.putInt("notice_down", vaule);
        editor.apply();
    }


    public static int getDownNotice(Context context) {
        try {//之所以加try catch 是因为2.5.3以前这个地方是保存的boolean型，2.5.3又尝试读取int型，导致获取失败
            SharedPreferences user = context.getSharedPreferences("btn_download_notice", Context.MODE_PRIVATE);
            return user.getInt("notice_down", 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 储存设备id
     *
     * @param context ，
     * @param vaule   ，
     */
    public static void saveDeviceIds(Context context, String vaule) {
        SharedPreferences user = context.getSharedPreferences("save_deviced_id", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = user.edit();
        editor.putString("device_id", vaule);
        editor.apply();
    }


    public static String getDeviceIds(Context context) {
        SharedPreferences user = context.getSharedPreferences("save_deviced_id", Context.MODE_PRIVATE);
        return user.getString("device_id", "");
    }


    /**
     * 保存要忽略的版本的md5值，只能忽略一个
     *
     * @param vaule
     */
    public static void saveIgnoreMd5(String vaule) {
        SharedPreferences user = SheepApp.getInstance().getSharedPreferences("save_data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = user.edit();
        editor.putString("ignore_md5", vaule);
        editor.apply();
    }

    public static String getIgnoreMd5() {
        SharedPreferences user = SheepApp.getInstance().getSharedPreferences("save_data", Context.MODE_PRIVATE);
        return user.getString("ignore_md5", "");
    }
}
