package com.renrenliao.im.viewmodel

import androidx.lifecycle.MutableLiveData
import com.renrenliao.im.base.BaseViewModel
import com.renrenliao.im.http.checkError
import com.renrenliao.im.http.checkSuccess
import com.renrenliao.im.im.AlarmType
import com.renrenliao.im.im.MsgType
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.withContext

/**
 **@ClassName ProcessAddFriendViewModel
 **@Description
 **@Author gan
 **@Date 2022/6/20 15:42
 **/
class ProcessAddFriendViewModel(private val remoteDataSource: RemoteDataSource,private val localDataSource: LocalDataSource) :BaseViewModel(){

    val rejectLiveData = MutableLiveData<Boolean>()

    fun reject(gid:String,friendUid:String,userUid:String){
        launchUI({
            var map = HashMap<String,String>()
            map["desc"] = ""
            map["friendUid"] = friendUid
            map["userUid"] = userUid
            map = MapUtils.getComParam(map)
            val result = withContext(Dispatchers.IO){
                remoteDataSource.rejectAddFriend(map)
            }
            result.checkSuccess {
                //  LogUtils.error(it)
                //拒绝好友添加 删除本地的这条数据
                withContext(Dispatchers.IO){
                    localDataSource.deleteAlarm(DataCacheUtils.getUid(),gid,AlarmType.ADDFRIENDREQUEST.value)
                }
                rejectLiveData.value = true
            }
            result.checkError {
                LogUtils.error(it.errorMsg)
            }

        })
    }

}