package com.renrenliao.im.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bumptech.glide.Glide
import com.renrenliao.im.base.BaseViewModel
import com.renrenliao.im.http.checkSuccess
import com.renrenliao.im.model.repository.LocalDataSource
import com.renrenliao.im.model.repository.RemoteDataSource
import com.renrenliao.im.provider.ClarityPotion
import com.renrenliao.im.utils.*
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.openmob.mobileimsdk.android.core.AutoReLoginDaemon
import net.openmob.mobileimsdk.android.core.QoS4SendDaemon

/**
 **@ClassName MainViewModel
 **@Description
 **@Author gan
 **@Date 2022/5/25 19:57
 **/
class MainViewModel(private val remoteRepo: RemoteDataSource, private val localDataSource: LocalDataSource):BaseViewModel() {

    val userInfo = LocalDataSource.readOnlyUserFlow

    val unReadLiveData = localDataSource.countUnRead()

    //http get friends http get groups 银行卡 广告
    fun getData(){
        viewModelScope.launch{
            var map1 = HashMap<String,String>()
            map1["userUid"] = DataCacheUtils.getUid()
            map1 = MapUtils.getComParam(map1)

            var map2 = HashMap<String,String>()
            map2["accessToken"] = DataCacheUtils.getToken()
            map2["loginId"] = DataCacheUtils.getUid()
            map2["userUid"] = DataCacheUtils.getUid()
            map2 = MapUtils.getComParam(map2)

            val map3 = MapUtils.getComParam(HashMap())

            val friends = async(Dispatchers.IO){remoteRepo.getFriendList(map3)}
            val groups = async(Dispatchers.IO){remoteRepo.getGroupList(map3)}
            val bankCards = async(Dispatchers.IO) {remoteRepo.getBankCards(map1)  }
            val ads = async (Dispatchers.IO){remoteRepo.getAds(map2)  }


            friends.await().checkSuccess { friends ->
          //      LogUtils.error(it)
                launch(Dispatchers.IO){
                    localDataSource.insertFriends(friends)
                    //更新头像
                    friends.forEach {

                        localDataSource.updateAvatarVersion(DataCacheUtils.getUid(),it.userUid,StringUtils.getAvatarVersion(it.userAvatarFileName))
                    }
                }
            }
            groups.await().checkSuccess {
             //   LogUtils.error(it)
                launch(Dispatchers.IO){
                    localDataSource.insertGroups(it)
                }
            }
           // LogUtils.error("${bankCard.await()} ${ad.await()}")
            bankCards.await().checkSuccess {
                LogUtils.error(it)
            }
            ads.await().checkSuccess {
                //LogUtils.error(it)
                val list = listOf<String>("https://cdn.pixabay.com/photo/2016/12/23/12/40/night-1927265_960_720.jpg","https://img.iplaysoft.com/wp-content/uploads/2019/free-images/free_stock_photo.jpg")
                DataCacheUtils.setAD(list.toSet())
                list.forEach { url ->
                    withContext(Dispatchers.IO){
                        val file = Glide.with(ClarityPotion.healingSalve).asFile().load(url).submit().get()
                        val copyFile = FileUtils.getOpenAdFile(url)
                        FileUtils.copyFile(file,copyFile)
              //           LogUtils.error(copyFile.absolutePath)
             //           LogUtils.error(b)
                    }
                   // LogUtils.error(file.absolutePath)
                }

            }

        }
    }




}