package com.adealink.weparty.room.invite.viewmodel

import androidx.lifecycle.LiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.frame.network.data.PageReq
import com.adealink.weparty.App
import com.adealink.weparty.module.room.data.MIC_ASSIGN_BY_REMOTE
import com.adealink.weparty.network.data.NoMoreDataError
import com.adealink.weparty.room.data.GetRoomMemberReq
import com.adealink.weparty.room.data.InviteMicReq
import com.adealink.weparty.room.datasource.remote.RoomHttpService
import com.adealink.weparty.room.invite.data.RoomMemberItemData
import com.adealink.weparty.room.sdk.service.roomService
import com.adealink.weparty.util.PageHandler
import kotlinx.coroutines.launch

class InviteViewModel : BaseViewModel() {

    private val roomHttpService by lazy {
        App.instance.networkService.getHttpService(RoomHttpService::class.java)
    }

    fun invite(uid: String, micIndex: Int?): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val roomId = roomId
            if (roomId.isNullOrEmpty()) {
                return@launch
            }
            liveData.send(
                roomHttpService.inviteOnMic(
                    InviteMicReq(
                        roomId, uid, micIndex ?: MIC_ASSIGN_BY_REMOTE
                    )
                )
            )
        }
        return liveData
    }


    private val memberListResultSet = mutableSetOf<String>()
    private var total = 0L
    val totalLD = ExtMutableLiveData<Long>()
    val memberListResultLD = ExtMutableLiveData<List<RoomMemberItemData>>()
    val memberListRltLD = ExtMutableLiveData<Rlt<Any>>()
    private val memberList = mutableListOf<RoomMemberItemData>()
    private val pageHandler = PageHandler()

    private var roomId: String? = null
    private var filterPlaymate: Boolean = false
    private var filterCategory: List<String>? = null

    fun pullMemberList(
        filterPlaymate: Boolean = false,
        filterCategory: List<String>? = null
    ) {
        this.filterPlaymate = filterPlaymate
        this.filterCategory = filterCategory
        this.roomId = roomService.joinController.getJoinedRoomId()

        pageHandler.reset()
        memberListResultSet.clear()
        memberList.clear()
        total = 0
        loadMore()
    }

    fun hasMoreData(): Boolean {
        return !pageHandler.isEnd
    }

    fun loadMore() {
        if (pageHandler.isEnd) {
            memberListRltLD.send(Rlt.Failed(NoMoreDataError()))
            return
        }
        viewModelScope.launch {
            val roomId = this@InviteViewModel.roomId
            if (roomId.isNullOrEmpty()) {
                memberListRltLD.send(Rlt.Failed(NoMoreDataError()))
                return@launch
            }
            val rlt = roomHttpService.getRoomMembers(
                GetRoomMemberReq(
                    roomId = roomId,
                    page = PageReq(size = pageHandler.pageSize, next = pageHandler.currentPage),

                    filterPlaymate = filterPlaymate || !filterCategory.isNullOrEmpty(),
                    skillCategories = filterCategory
                )
            )
            when (rlt) {
                is Rlt.Failed -> {
                    memberListRltLD.send(rlt)
                }

                is Rlt.Success -> {
                    pageHandler.nextPage(rlt.data.data?.next)
                    rlt.data.data?.total?.let {
                        total = it
                        totalLD.send(it)
                    }
                    val nextList = rlt.data.data?.list?.filter {
                        !memberListResultSet.contains(it.user.uid)
                    } ?: emptyList()

                    val nextItemList = nextList.map {
                        RoomMemberItemData(it.user)
                    }
                    memberListResultSet.addAll(nextList.map { it.user.uid })
                    memberList.addAll(nextItemList)
                    memberListRltLD.send(rlt)
                    memberListResultLD.send(memberList)
                }
            }
        }
    }

}