package com.renrenliao.im.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.renrenliao.im.base.BaseViewModel
import com.renrenliao.im.http.checkError
import com.renrenliao.im.http.checkSuccess
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.LogUtils
import com.renrenliao.im.utils.MapUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 **@ClassName FriendInfoViewModel
 **@Description
 **@Author gan
 **@Date 2022/6/13 18:17
 **/
class FriendInfoViewModel(private val remoteDataSource: RemoteDataSource,private val localDataSource: LocalDataSource):BaseViewModel() {

    val isFriendLiveData = MutableLiveData<Boolean>()

    fun isFriend(uid:String){
        viewModelScope.launch {
            val result = withContext(Dispatchers.IO){
                localDataSource.isFriend(DataCacheUtils.getUid(),uid)
            }
            isFriendLiveData.value = result
        }
    }

    val deleteLiveData = MutableLiveData<Boolean>()
    fun deleteFriend(uid: String){
        var map = HashMap<String,String>()
        map["friendUid"] = uid
        map["type"] = "3"
        map = MapUtils.getComParam(map)
        launchUI({
            //先接口删除
            val result = withContext(Dispatchers.IO){
                remoteDataSource.deleteFriend(map)
            }
            //再删除本地数据
            result.checkSuccess {
             //   LogUtils.error(it)
                withContext(Dispatchers.IO){
                    localDataSource.deleteByUid(DataCacheUtils.getUid(),uid)
                }
                deleteLiveData.value = true
            }

        })
    }
}