package com.renrenliao.im.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.renrenliao.im.base.BaseViewModel
import com.renrenliao.im.config.Const
import com.renrenliao.im.http.RetrofitClient
import com.renrenliao.im.http.checkError
import com.renrenliao.im.http.checkSuccess
import com.renrenliao.im.model.api.ApiService
import com.renrenliao.im.model.repository.LocalDataSource
import com.renrenliao.im.model.repository.RemoteDataSource
import com.renrenliao.im.utils.DataCacheUtils
import com.renrenliao.im.utils.DeviceUtils
import com.renrenliao.im.utils.MapUtils
import com.renrenliao.im.utils.RouteUtils
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.openmob.mobileimsdk.android.core.AutoReLoginDaemon
import net.openmob.mobileimsdk.android.core.QoS4SendDaemon
import org.koin.core.component.KoinComponent
import java.util.*
import kotlin.collections.HashMap

/**
 **@ClassName SpashViewModel
 **@Description
 **@Author ganzhe
 **@Date 2022/5/19 16:00
 **/
class SplashViewModel(
    private val remoteRepo: RemoteDataSource,
    private val localDataSource: LocalDataSource
) : BaseViewModel(), KoinComponent {

    val versionStatusLiveData = MutableLiveData<Int>()

    fun getAppConfig() {

        viewModelScope.launch {

            val lines = withContext(Dispatchers.IO) {
                remoteRepo.getLines()
            }
            //设置线路
            val localLine = DataCacheUtils.getLines()
            val position = (0 until lines.server.size).random()//Random().nextInt(lines.server.size)
            if ((localLine == null) || (localLine.version != lines.version)) {
                DataCacheUtils.setLines(lines, position)
            }

            //请求配置文件
            var configMap = HashMap<String, String>()
            configMap["type"] = "0"
            configMap["version"] = DeviceUtils.getVersionCode().toString()
            configMap = MapUtils.getComParam(configMap)
            val configData = withContext(Dispatchers.IO) {
                remoteRepo.getAppConfig(configMap)
            }

            //模拟登录
            var loginMap = HashMap<String, String>()
            val kv = MMKV.defaultMMKV()
            val uid = kv.decodeString(DataCacheUtils.KEY_UID, "").toString()
            val token = kv.decodeString(DataCacheUtils.KEY_TOKEN, "").toString()
            loginMap["userUid"] = uid
            loginMap["accessToken"] = token
            loginMap["loginType"] = "2" // loginType 2 token登陆
            loginMap["modleType"] = "android"
            loginMap["umDeviceToken"] = DataCacheUtils.getDeviceToken()

            val loginData = withContext(Dispatchers.IO) {
                remoteRepo.login(loginMap)
            }

            loginData.checkSuccess {
                val userInfo =
                    it.authed_info//GsonUtils.convertEntity(Des3Util.decode(it),AuthInfo::class.java).authed_info
                //保存用户信息
                DataCacheUtils.isLogin = true
                DataCacheUtils.setToken(userInfo.token.toString())
                DataCacheUtils.setUid(userInfo.userUid)
                localDataSource.updateUserInfo(userInfo)

                configData.checkSuccess { appConfigEntity ->
                    //保存app配置
                    DataCacheUtils.setAppConfig(appConfigEntity)
                    //版本livedata 变化
                    versionStatusLiveData.value = appConfigEntity.versionStatus
                }
                //跳转到登陆
                configData.checkError {
                    DataCacheUtils.clearUserInfo()
                    RouteUtils.exitAndLogin()
                }
            }
            loginData.checkError {
                DataCacheUtils.clearUserInfo()
                RouteUtils.exitAndLogin()
            }


        }
    }


}