package com.adealink.weparty.room.chatroom.page.dispatchcenter

import androidx.lifecycle.MutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.module.room.chat.data.ClearMicNotificationContent
import com.adealink.weparty.module.room.chat.data.Notification
import com.adealink.weparty.module.room.chat.data.SendGiftNotificationContent
import com.adealink.weparty.module.room.data.MIC_ASSIGN_BY_REMOTE
import com.adealink.weparty.room.applymic.ApplyMicStore
import com.adealink.weparty.room.data.applyMicMemberCount
import com.adealink.weparty.room.data.hostList
import com.adealink.weparty.room.micseat.dispatcenter.MIC_GUEST
import com.adealink.weparty.room.sdk.service.roomService
import io.trtc.tuikit.atomicxcore.api.live.LiveInfo
import io.trtc.tuikit.atomicxcore.api.live.LiveListStore
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update

class DispatchRoomEventViewModel : BaseViewModel() {

    var applyMicNumber: Int = 0
    val applyMicNumberLD = MutableLiveData<Int>()


    private val _hostListFlow = MutableStateFlow<List<String>>(emptyList())
    val hostListFlow: StateFlow<List<String>> = _hostListFlow.asStateFlow()

    val clearGuestMicApplyLD = MutableLiveData<Unit>()
    val clearPlaymateMicLD = MutableLiveData<Unit>()

    init {
        LiveListStore.shared().liveState.currentLive.onEach { liveInfo ->
            if (liveInfo.liveID == roomService.joinController.getJoinedRoomId()) {
                onLiveInfoChanged(liveInfo)
            }
        }.launchIn(viewModelScope)

        roomService.messageController.notificationsFlow.map { notifications ->
            notifications.mapNotNull {
                if (it.content is SendGiftNotificationContent) {
                    it
                } else {
                    null
                }
            }
        }.distinctUntilChanged().onEach { notifications ->
            notifyMicApplyClear(notifications)
        }.launchIn(viewModelScope)
    }

    private fun onLiveInfoChanged(liveInfo: LiveInfo) {
        //申请连麦人数
        run {
            val number = liveInfo.applyMicMemberCount()
            notifyApplyMicNumberChanged(number)
        }

        //管理员
        run {
            val adminList = liveInfo.hostList()
            notifyAdminListChanged(adminList)
        }

    }

    private fun notifyApplyMicNumberChanged(number: Int) {
        if (applyMicNumber != number) {
            applyMicNumber = number
            applyMicNumberLD.send(number)
        }
    }

    private fun notifyAdminListChanged(adminList: List<String>) {
        _hostListFlow.update { adminList }
    }

    private fun notifyMicApplyClear(notifications: List<Notification>) {
        var clearGuestApply = false
        var clearPlaymateApply = false
        val roomId = roomService.joinController.getJoinedRoomId() ?: return
        notifications.mapNotNull {
            if (it.barrage.liveID == roomId) {
                it.content as? ClearMicNotificationContent
            } else {
                null
            }
        }.forEach {
            if (it.isClearGuest()) {
                clearGuestApply = true
            } else if (it.isClearPlaymate()) {
                clearPlaymateApply = true
            }
        }
        if (clearGuestApply) {
            clearGuestMicApplyLD.send(Unit)
            ApplyMicStore.removeRecord(roomId, MIC_GUEST)
        }
        if (clearPlaymateApply) {
            clearPlaymateMicLD.send(Unit)
            ApplyMicStore.removeRecord(roomId, MIC_ASSIGN_BY_REMOTE)
        }
    }


}