package com.renrenliao.im.utils

import com.renrenliao.im.bean.AppConfigEntity
import com.renrenliao.im.bean.LineEntity
import com.renrenliao.im.bean.UserInfo
import com.renrenliao.im.config.Const
import com.renrenliao.im.http.RetrofitClient
import com.renrenliao.im.model.api.ApiService
import com.renrenliao.im.model.repository.RemoteDataSource
import com.tencent.mmkv.MMKV
import net.openmob.mobileimsdk.android.conf.IMConfigEntity

object DataCacheUtils {

    val kv = MMKV.defaultMMKV()

    //用户相关缓存
    const val KEY_TOKEN = "key_token"
    const val KEY_UID = "key_uid"
    const val KEY_USERINFO = "key_userinfo"

    const val KEY_DEVICEID = "key_deviceid"
    const val KEY_DEVICETOKEN = "key_devicetoken" //友盟推送token

    //  val KEY_SECRET = "key_secret"
    const val KEY_AD = "key_ad"
    const val KEY_APPCONFIG = "key_appconfig"

    //线路
    const val KEY_LINE = "key_line"
    const val KEY_LINE_INDEX = "key_line_index"

    @JvmField
    var mToken = ""

    @JvmField
    var mUid = ""

    @JvmField
    var mUserInfo: UserInfo? = null

    @JvmField
    @Volatile
    var isLogin: Boolean = false

    fun getToken(): String {
        return if (isLogin) {
            mToken.ifBlank {
                kv.decodeString(KEY_TOKEN, "").toString()
            }
        } else {
            ""
        }
    }

//    fun getTokenIM(): String {
//        return mToken.ifBlank {
//            kv.decodeString(KEY_TOKEN, "").toString()
//        }
//    }

    fun setToken(token: String) {
        mToken = token
        kv.encode(KEY_TOKEN, token)
    }

    fun getUid(): String {
        return if (isLogin) {
            mUid.ifBlank {
                kv.decodeString(KEY_UID, "").toString()
            }
        } else {
            ""
        }
    }

//    fun getUidIM(): String {
//
//        return mUid.ifBlank {
//            kv.decodeString(KEY_UID, "").toString()
//        }
//    }

    fun setUid(uid: String) {
        mUid = uid
        kv.encode(KEY_UID, uid)
    }

    fun setUserInfo(userInfo: UserInfo) {
        mUserInfo = userInfo
        kv.encode(KEY_USERINFO, userInfo)
    }

    fun getUserInfo(): UserInfo? {

        return if (mUserInfo != null) {
            mUserInfo
        } else {
            kv.decodeParcelable(KEY_USERINFO, UserInfo::class.java)
        }


    }

    //清除用户缓存
    fun clearUserInfo() {
        mToken = ""
        mUid = ""
        mUserInfo = UserInfo()

        val keys = arrayOf(KEY_TOKEN, KEY_UID, KEY_USERINFO)
        kv.removeValuesForKeys(keys)
    }

    fun getDeviceId(): String {
        return if (kv.decodeString(KEY_DEVICEID, "").isNullOrEmpty()) {
            val deviceId = DeviceUtils.getDeviceId()
            kv.encode(KEY_DEVICEID, deviceId)
            deviceId
        } else {
            kv.decodeString(KEY_DEVICEID, "").toString()
        }
    }

    fun getDeviceToken(): String {
        return ""
//        return if (kv.decodeString(KEY_DEVICETOKEN,"").isNullOrEmpty()){
//            kv.decodeString(KEY_TOKEN,"").toString()
//        }else{
//            kv.decodeString(KEY_DEVICETOKEN,"").toString()
//        }

    }

    fun getSecret(time: String): String {
        val uid = getUid()
        val token = getToken()
        val str = Const.APICODE + time + uid + token
        return DigestUtils.md5(str).trim()
    }

    fun getAD(): MutableSet<String>? {
        return kv.decodeStringSet(KEY_AD, emptySet())
    }

    fun setAD(set: Set<String>) {
        kv.encode(KEY_AD, set)
    }

    //设置app配置文件 静态变量
    fun setAppConfig(appConfigEntity: AppConfigEntity) {
        if (!appConfigEntity.aliyunoss.isNullOrEmpty()) {
         //   Const.BASE_IMG_HEAD_URL = appConfigEntity.aliyunoss
            Const.COMMON_IMG_USER_FILE = Const.BASE_IMG_HEAD_URL + "user/"
            Const.COMMON_IMG_GROUP_FILE = Const.BASE_IMG_HEAD_URL + "group/"
        }
        Const.useMobileRegister = appConfigEntity.useMobileRegister.toInt()

        kv.encode(KEY_APPCONFIG, appConfigEntity)
    }

    fun getAppConfig(): AppConfigEntity? {
        return kv.decodeParcelable(KEY_APPCONFIG, AppConfigEntity::class.java)
    }

    //设置线路  先设置缓存 再设置静态变量
    fun setLines(lines: LineEntity, potion: Int) {
        kv.encode(KEY_LINE, lines)
        kv.encode(KEY_LINE_INDEX, potion)
        //val potion = 0
        val line = lines.server[potion]
        Const.BASE_URL = line.apiUrl + "/"
        Const.BASE_UP_URL = line.uploadUrl
        IMConfigEntity.serverIP = line.imAddr
        IMConfigEntity.serverPort = line.imPort.toInt()
        //apiService重新生成
        RemoteDataSource.apiService =
            RetrofitClient.createApi(ApiService::class.java, Const.BASE_URL)
    }

    fun getLines(): LineEntity? {
        val potion = kv.decodeInt(KEY_LINE_INDEX, 0)
        val lines = kv.decodeParcelable(KEY_LINE, LineEntity::class.java)
        if (lines != null) {
            val line = lines.server[potion]
            Const.BASE_URL = line.apiUrl + "/"
            Const.BASE_UP_URL = line.uploadUrl
            IMConfigEntity.serverIP = line.imAddr
            IMConfigEntity.serverPort = line.imPort.toInt()
            //apiService重新生成
            RemoteDataSource.apiService =
                RetrofitClient.createApi(ApiService::class.java, Const.BASE_URL)
        }
        return lines
    }

    //消息开关  默认打开
    fun getMsgSwitch(uid: String):Boolean{
        return kv.decodeBool("msg_switch_$uid",true)
    }
    //设置消息开关
    fun setMsgSwitch(uid: String,isOpen:Boolean){
        kv.encode("msg_switch_$uid",isOpen)
    }

}