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.model.repository.LocalDataSource
import com.renrenliao.im.model.repository.RemoteDataSource
import com.renrenliao.im.utils.LogUtils
import com.renrenliao.im.utils.MapUtils
import com.renrenliao.im.widget.dialog.ExplainDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

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

    val addFriendLiveData = MutableLiveData<Boolean>()

    fun addFriend(friendUid:String,desc:String,buddyRemark:String){
        var map = HashMap<String,String>()
        map["friendUid"] = friendUid
        map["desc"] = desc
        map["buddyRemark"] = buddyRemark
        map = MapUtils.getComParam(map)

        launchUI({
            val result = withContext(Dispatchers.IO){
                remoteDataSource.addFriend(map)
            }
            result.checkSuccess {
                LogUtils.error(it)
                addFriendLiveData.value = true
            }
            result.checkError {
                ExplainDialog.Builder()
                    .setTitle("提示")
                    .setDesc(it.errorMsg)
                    .setNo("确定")
                    .create()
                    .show()
            }
        }, isShowDiaLoading = true)

    }

}