package com.kfzs.duanduan.utils.dlg;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.KFZSApp;

import java.util.List;

public class Configer {
    public final static String IMEI = "IMEI";
    private SharedPreferences mSharedPreferences;
    private Editor mEditor;

    public Configer() {
        mSharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(KFZSApp.context);
        mEditor = mSharedPreferences.edit();
    }

    public boolean contains(String key) {
        return mSharedPreferences.contains(key);
    }


    public Configer(String shareName) {
        mSharedPreferences = KFZSApp.context.getSharedPreferences(shareName, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }


    /**
     * @param key
     * @param value 任意类，如果为null,显示清除本key
     * @return
     */
    public Configer put(String key, Object value) {
        if (value == null)
            mEditor.remove(key);
        else
            mEditor.putString(key, value.toString());
        return this;
    }

    /**
     * 插入Json数据
     *
     * @param key
     * @param value 任意类，如果为null,显示清除本key
     * @return
     */
    public Configer putJson(String key, Object value) {
        if (value == null) {
            mEditor.remove(key);
        } else {
            mEditor.putString(key, JSONObject.toJSONString(value));
        }
        return this;
    }


    /**
     * 获取Json类，失败返回空
     *
     * @param key
     * @param clazz
     * @param <T>   不能是Array数组
     * @return 失败返回null
     */
    public <T> T getJson(String key, Class<T> clazz) {
        try {
            return JSON.parseObject(mSharedPreferences.getString(key, ""), clazz);
        } catch (JSONException e) {
            return null;
        }
    }

    /**
     * 获取Json类，失败返回空
     *
     * @param key
     * @param clazz
     * @param <T>   Array数组类
     * @return 失败返回null
     */
    public <T> List<T> getJsonArray(String key, Class<T> clazz) {
        try {
            return JSONArray.parseArray(mSharedPreferences.getString(key, ""), clazz);
        } catch (JSONException e) {
            return null;
        }
    }

    public int getint(String key, int defVaule) {
        return Integer.valueOf(mSharedPreferences.getString(key, String.valueOf(defVaule)));
    }

    public long getlong(String key, long defVaule) {
        return Long.valueOf(mSharedPreferences.getString(key, String.valueOf(defVaule)));
    }

    public double getdouble(String key, double defVaule) {
        return Double.valueOf(mSharedPreferences.getString(key, String.valueOf(defVaule)));
    }

    public float getfloat(String key, float defVaule) {
        return Float.valueOf(mSharedPreferences.getString(key, String.valueOf(defVaule)));
    }

    public String getString(String key, String defVaule) {
        return mSharedPreferences.getString(key, defVaule);
    }

    public boolean getboolean(String key, boolean defVaule) {
        return mSharedPreferences.getString(key, String.valueOf(defVaule)).equals("true");
    }

    public Configer commit() {
        mEditor.commit();
        return this;
    }
}
