package com.zhimai.websocket.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;


/***
 * 需要提前初始化
 * */
public class SpUtils {
    private static SpUtils spUtils;
    private static final String TAG = "SpUtils";
    private SharedPreferences.Editor editor;
    private SharedPreferences sharedPreferences;

    private final String timeKey = "time_today";

    public static SpUtils getInstance(Context context) {
        if (spUtils == null) {
            spUtils = new SpUtils(context);
        }
        return spUtils;
    }

    public SpUtils(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences("qmzs", Context.MODE_PRIVATE);
            editor = sharedPreferences.edit();
        }
    }

    public String getMyTime() {
        return sharedPreferences.getString(timeKey, "");
    }

    public void setMyTime(String time) {
        put(timeKey, time);
    }


    public boolean getBoolean(String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }


    public void clearAll() {

        editor.clear().commit();
    }

//    public void clearAllWithOutUsrPwd() {
//
//        String userName = getString(ParamsUtils.USERNAME, "");
//        String pwd = getString(ParamsUtils.PWD, "");
//        String deviceId = getString(ParamsUtils.DEVICEID, "");
//        editor.clear().commit();
//        put(ParamsUtils.USERNAME, userName);
//        put(ParamsUtils.PWD, pwd);
//        put(ParamsUtils.DEVICEID, deviceId);
//        put(ParamsUtils.IS_LOGIN, false);
//
//        put(ParamsUtils.MULTIID, 0);
//        put(ParamsUtils.ORG_ID, 0);
//    }


    private SpUtils() {

    }


    public String getString(String key, String defaultValue) {

        return sharedPreferences.getString(key, defaultValue);

    }

    public int getInt(String key, int defaultValue) {

        return sharedPreferences.getInt(key, defaultValue);


    }

    public Object get(String key, Object defaultValue) {


        if (defaultValue instanceof Integer) {


            return (int) sharedPreferences.getInt(key, (int) defaultValue);

        } else if (defaultValue instanceof String) {

            return (String) (sharedPreferences.getString(key, (String) defaultValue));

        } else if (defaultValue instanceof Float) {

            return (Float) sharedPreferences.getFloat(key, (Float) defaultValue);


        } else if (defaultValue instanceof Boolean) {

            return (Boolean) sharedPreferences.getBoolean(key, (Boolean) defaultValue);

        }


        return null;

    }

    public void put(String key, Object value) {

        if (value instanceof Integer) {

            editor.putInt(key, (Integer) value);

        } else if (value instanceof String) {

            editor.putString(key, (String) value);


        } else if (value instanceof Float) {

            editor.putFloat(key, (Float) value);

        } else if (value instanceof Boolean) {

            editor.putBoolean(key, (Boolean) value);

        }

        editor.commit();

    }


}
