package com.renrenliao.im.viewmodel

import androidx.lifecycle.MutableLiveData
import com.renrenliao.im.base.BaseViewModel
import com.renrenliao.im.bean.UserInfo
import com.renrenliao.im.http.checkError
import com.renrenliao.im.http.checkSuccess
import com.renrenliao.im.model.repository.RemoteDataSource
import com.renrenliao.im.provider.ClarityPotion
//import com.renrenliao.im.utils.DialogExplainUtils
import com.renrenliao.im.utils.LogUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext

/**
 **@ClassName FriendSearchViewModel
 **@Description
 **@Author gan
 **@Date 2022/6/13 16:30
 **/
class FriendSearchViewModel(private val remoteDataSource: RemoteDataSource) : BaseViewModel() {

    val searchLiveData = MutableLiveData<UserInfo>()

    fun searchFriend(key: String) {
        launchUI({
            val map = HashMap<String, String>()
            map["account"] = key
            map["type"] = "mobile"

            val result = withContext(Dispatchers.IO) {
             //   delay(2000)
                remoteDataSource.friendFind(map)
            }

            result.checkSuccess {
                //LogUtils.error(it)
                searchLiveData.value = it
            }
            result.checkError {
                LogUtils.error(it.errorMsg)
                //展示错误信息
//                DialogExplainUtils.getInstance().showCustomDialog(
//                    ClarityPotion.currentActivity(), DialogExplainUtils.getInstance().getMap(
//                        "提示", it.errorMsg, "确定", null
//                    )
//                )
            }

        }, isShowDiaLoading = true)
    }
}