package com.adealink.weparty.im.session.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.im.manager.session.sessionManager
import com.tencent.qcloud.tuikit.tuichat.bean.ChatInfo
import kotlinx.coroutines.launch

class SessionViewModel : BaseViewModel() {

    private var chatInfo: ChatInfo? = null

    fun loadChatInfo(chatInfo: ChatInfo?) {
        this@SessionViewModel.chatInfo = chatInfo
        viewModelScope.launch {
            isSessionMute()

        }
    }

    val isSessionMuteLD = MutableLiveData<Boolean>()

    //会话免打扰
    fun isSessionMute(): LiveData<Boolean> {
        val liveData = OnceMutableLiveData<Boolean>()
        viewModelScope.launch {
            val chatInfo = chatInfo ?: return@launch
            val uid = chatInfo.id
            val rlt = sessionManager.isSessionMute(listOf(uid), chatInfo.type)
            when (rlt) {
                is Rlt.Failed -> {
                }

                is Rlt.Success -> {
                    isSessionMuteLD.send(rlt.data[uid] ?: false)
                    liveData.send(rlt.data[uid] ?: false)
                }
            }
        }
        return liveData
    }

    fun setSessionMute(mute: Boolean): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val chatInfo = chatInfo ?: return@launch
            val uid = chatInfo.id
            val rlt = sessionManager.setSessionMute(listOf(uid), chatInfo.type, mute)
            when (rlt) {
                is Rlt.Failed -> {
                    //Ntd.
                }

                is Rlt.Success -> {
                    isSessionMuteLD.send(mute)
                }
            }
            liveData.send(rlt)
        }
        return liveData
    }


}