package com.example.iot_controlhost.utils;


import android.os.Parcelable;

import com.blankj.utilcode.util.GsonUtils;
import com.example.iot_controlhost.MyApp;
import com.tencent.mmkv.MMKV;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * MMKV工具类
 */
public class MMKVUtil {
    private static MMKVUtil INSTANCE;
    private static MMKV mmkvInstance;

    private MMKVUtil() {
    }

    /**
     * 初始化MMKV，只能在Application中初始化一次
     */
    public static void init() {
        INSTANCE = new MMKVUtil();
        MMKV.initialize(MyApp.getAppContext());
        mmkvInstance = MMKV.defaultMMKV();
    }

    public static MMKVUtil get() {
        return INSTANCE;
    }

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     */
    public static <T> void put(String key, T object) {
        if (object instanceof String) {
            mmkvInstance.encode(key, (String) object);
        } else if (object instanceof Integer) {
            mmkvInstance.encode(key, (Integer) object);
        } else if (object instanceof Boolean) {
            mmkvInstance.encode(key, (Boolean) object);
        } else if (object instanceof Float) {
            mmkvInstance.encode(key, (Float) object);
        } else if (object instanceof Long) {
            mmkvInstance.encode(key, (Long) object);
        } else if (object instanceof Double) {
            mmkvInstance.encode(key, (Double) object);
        } else if (object instanceof Set) {
            mmkvInstance.encode(key, (Set<String>) object);
        } else if (object instanceof Parcelable) {
            mmkvInstance.encode(key, (Parcelable) object);
        } else {
            mmkvInstance.encode(key, object == null ? "" : object.toString());
        }
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     */
    public static <T> T get(String key, T defaultObject) {
        if (defaultObject instanceof String || defaultObject == null) {
            return (T) mmkvInstance.decodeString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return (T) Integer.valueOf(mmkvInstance.decodeInt(key, (Integer) defaultObject));
        } else if (defaultObject instanceof Boolean) {
            return (T) Boolean.valueOf(mmkvInstance.decodeBool(key, (Boolean) defaultObject));
        } else if (defaultObject instanceof Float) {
            return (T) Float.valueOf(mmkvInstance.decodeFloat(key, (Float) defaultObject));
        } else if (defaultObject instanceof Long) {
            return (T) Long.valueOf(mmkvInstance.decodeLong(key, (Long) defaultObject));
        } else if (defaultObject instanceof Double) {
            return (T) Double.valueOf(mmkvInstance.decodeDouble(key, (Double) defaultObject));
        } else if (defaultObject instanceof Parcelable) {
            Parcelable p = (Parcelable) defaultObject;
            return (T) mmkvInstance.decodeParcelable(key, p.getClass());
        } else {
            return null;
        }
    }

    /**
     * 获得字符串
     */
    public static String get(String key) {
        return mmkvInstance.decodeString(key, "");
    }

    /**
     * 移除某个key值已经对应的值
     */
    public static void remove(String key) {
        mmkvInstance.remove(key);
    }

    /**
     * 查询某个key是否已经存在
     */
    public boolean contains(String key) {
        return mmkvInstance.contains(key);
    }

    /**
     * 清除所有数据
     */
    public static void clear() {
        mmkvInstance.clear();
    }

    /**
     * 获取所有key-value的json字符串
     */
    public static String getAllKeyValues() {
        Map<String, Object> result = new HashMap<>();
        String[] keys = mmkvInstance.allKeys();
        if (keys == null || keys.length == 0) {
            return "";
        }
        for (String key : keys) {
            if (key.contains("HISTORY") || key.contains("user") || key.contains("password")) {
                continue;
            }
            //将所有的key-value转换成json字符串
            result.put(key, mmkvInstance.decodeString(key, ""));
            //解决boolean、int等基本类型无法转换成json字符串的问题
            if (result.get(key) == null) {
                result.put(key, mmkvInstance.decodeInt(key, 0));
            }
        }
        return GsonUtils.toJson(result);
    }

}