package com.xty.common

import android.content.Context
import android.os.Parcelable
import android.text.TextUtils
import com.tencent.mmkv.MMKV

object MMkvHelper {

    val mmkv by lazy { MMKV.defaultMMKV() }

    /**
     * 初始化
     */
    fun initMmkv(context: Context) {
        if (context == null) return
        MMKV.initialize(context)
    }

    /**
     * 存储常规数据
     */
    fun put(key: String, content: Any?): Boolean {
        if (TextUtils.isEmpty(key) || content == null) return false
        try {
            when (content) {
                is String ->
                    mmkv!!.encode(key, content!!)
                is Double ->
                    mmkv!!.encode(key, content!!)
                is Float ->
                    mmkv!!.encode(key, content!!)
                is Int ->
                    mmkv!!.encode(key, content!!)
                is Parcelable ->
                    mmkv!!.encode(key, content!!)
                is ByteArray ->
                    mmkv!!.encode(key, content!!)
                is Long ->
                    mmkv!!.encode(key, content!!)
                is Boolean ->
                    mmkv!!.encode(key, content!!)
                else ->
                    return false
            }
        } catch (exception: Exception) {
            LogUtils.e("MMKV", "MMKV Put Exception:${exception.cause}")
            return false
        }
        return true
    }

    /**
     * 存储set集合
     */
    fun putSet(key: String, content: Set<String>): Boolean {
        if (TextUtils.isEmpty(key) || content == null) return false
        mmkv!!.encode(key, content!!)
        return true
    }

    /**
     * 获取String 数据
     */
    fun getString(key: String): String {
        return mmkv!!.decodeString(key) ?: ""
    }
    fun getLong(key: String): Long {
        return mmkv!!.decodeLong(key) ?: 0
    }
    fun getDouble(key: String): Double {
        return mmkv!!.decodeDouble(key)
    }

    fun getFloat(key: String): Float {
        return mmkv!!.decodeFloat(key)
    }

    fun getBoolean(key: String, default: Boolean = true): Boolean {
        return mmkv!!.decodeBool(key, default)
    }

    fun getInt(key: String): Int {
        return mmkv!!.decodeInt(key)
    }

    fun getByteArray(key: String): ByteArray? {
        return mmkv!!.decodeBytes(key) ?: null
    }

    fun getParcelable(key: String, clazz: Class<Parcelable>): Parcelable? {
        return mmkv!!.decodeParcelable(key, clazz) ?: null
    }

    fun getSet(key: String): Set<String>? {
        return mmkv!!.decodeStringSet(key) ?: null
    }


    fun contains(key: String): Boolean {
        return mmkv!!.containsKey(key)
    }

    //   删除数据
    fun remove(key: String) {
        mmkv!!.removeValueForKey(key)
    }

    // 删除多个数据
    fun remove(vararg key: String) {
        var keys = arrayOfNulls<String>(key.size)
        key.forEachIndexed { index, content ->
            keys.set(index, content)
        }
        mmkv!!.removeValuesForKeys(keys)
    }

    fun clearUserInfo() {
        //清除token信息
        mmkv!!.remove(Const.Auth_Token)
            .remove(Const.Auth_Id)
            .remove(Const.WELCOME_DESC)
            .remove(Const.WELCOME_IMG)
            .remove(Const.WELCOME_BG)
            .remove(Const.ORGAN_ID)
            .remove(Const.WX_PUSH)
            .remove(Const.QQ_PUSH)
            .remove(Const.PHONE_PUSH)
            .remove(Const.MSG_PUSH)
            .remove(Const.USER_ID)
    }

    fun clearWhatchInfo(){
        mmkv!!.remove(Const.WHATCH_HAND_SETTING)
            .remove(Const.WHATCH_WRIST_SCREEN_SETTING)
            .remove(Const.WHATCH_SCREEN_LIGHT_SETTING)
            .remove(Const.WHATCH_DONT_REMIND_MODE_SETTING)
            .remove(Const.WHATCH_THEME_SIZE)
            .remove(Const.WHATCH_THEME_SETTING)
    }

    // 清理所有数据
    fun clearAll() {
        mmkv!!.clearAll()
    }

    // 保存配置信息
    fun saveAccountConfig(context :Context,accountId: String, configKey: String, configValue: String) {
        val sharedPreferences = context.getSharedPreferences("account_$accountId", Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.putString(configKey, configValue)
        editor.apply()
    }

    // 读取配置信息
    fun readAccountConfig(context :Context,accountId: String, configKey: String, defaultValue: String): String {
        val sharedPreferences = context.getSharedPreferences("account_$accountId", Context.MODE_PRIVATE)
        return sharedPreferences.getString(configKey, defaultValue) ?: defaultValue
    }
    // 修改配置信息
    fun updateAccountConfig(context :Context,accountId: String, configKey: String, configValue: String) {
        val sharedPreferences = context.getSharedPreferences("account_$accountId", Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.putString(configKey, configValue)
        editor.apply()
    }

    // 删除配置信息
    fun deleteAccountConfig(context :Context,accountId: String, configKey: String) {
        val sharedPreferences = context.getSharedPreferences("account_$accountId", Context.MODE_PRIVATE)
        val editor = sharedPreferences.edit()
        editor.remove(configKey)
        editor.apply()
    }

}