package com.xhly.manageapp.utils;

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

import com.google.gson.Gson;

import java.lang.reflect.Type;

public class CurrentAppSharedPreferencesUtils {
    /**
     * 保存在手机里面的文件名
     */
    public static final String SP_FILE_NAME = "ManageAppData";
    private SharedPreferences preferences;
    private static CurrentAppSharedPreferencesUtils instance;


    private CurrentAppSharedPreferencesUtils(Context context) {
        preferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 单例模式的SharedPreferencesUtil
     */
    public static CurrentAppSharedPreferencesUtils getInstance(Context context) {
        if (instance == null) {
            synchronized (CurrentAppSharedPreferencesUtils.class) {
                if (instance == null) {
                    instance = new CurrentAppSharedPreferencesUtils(context);
                }
            }
        }
        return instance;
    }


    //    *
//     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
//     * @param context
//     * @param key
//     * @param object
//
    public boolean setParam(String key, Object object) {

        String type = object.getClass().getSimpleName();
        SharedPreferences.Editor editor = preferences.edit();

        if ("String".equals(type)) {
            editor.putString(key, (String) object);
        } else if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) object);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) object);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) object);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) object);
        }

        editor.apply();
        return false;
    }


    //   *
//     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
//     * @param context
//     * @param key
//     * @param defaultObject
//     * @return
    public Object getParam(String key, Object defaultObject) {
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = preferences;

        if ("String".equals(type)) {
            return sp.getString(key, (String) defaultObject);
        } else if ("Integer".equals(type)) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if ("Boolean".equals(type)) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if ("Float".equals(type)) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if ("Long".equals(type)) {
            return sp.getLong(key, (Long) defaultObject);
        }

        return null;
    }


    /**
     * 保存对象到json对象
     *
     * @param object 播放列表对象
     */
    public void saveJson(String key, Object object) {
        String strJson = "";
        Gson gson = new Gson();
        strJson = gson.toJson(object);
        SharedPreferences.Editor edit = preferences.edit();
        if (edit != null) {
            edit.putString(key, strJson);
            edit.apply();
        }
    }

    public void clearJson(String key) {
        SharedPreferences.Editor edit = preferences.edit();
        if (edit != null) {
            edit.putString(key, "");
            edit.apply();
        }
    }

    /**
     * 读取object对象
     *
     * @return
     */
    public <T> Object getFromJson(String key, Class<T> cls) {
        try {
            String json = preferences.getString(key, "");
            if (TextUtils.isEmpty(json)) {
                Log.d("转换失败", "数据为空");
                return null;
            }
            Gson gson = new Gson();
            return gson.fromJson(json, cls);
        } catch (Exception e) {
            Log.d("转换失败", e.toString());
            return null;
        }
    }

    public <T> Object getFromJsonType(String key, Class<T> cls, Type type) {
        try {
            String json = preferences.getString(key, "");
            if (TextUtils.isEmpty(json)) {
                Log.d("转换失败", "数据为空");
                return null;
            }
            Gson gson = new Gson();
            return gson.fromJson(json, type);
        } catch (Exception e) {
            Log.d("转换失败", e.toString());
            return null;
        }
    }

    public String get(String key) {
        return preferences.getString(key, "");
    }

}
