package com.swago.room.giftdandao

import com.swago.baseswago.baseroom.IRoomActiveListener
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.model.live.gift.IMGiftModel
import com.swago.baseswago.util.LogUtil
import kotlinx.coroutines.*
import java.util.*
import kotlin.collections.ArrayList

/**
 *@date 2022/1/22 17:12
 *description:
 */
class GiftDanDaoManager  : IRoomActiveListener {

    val giftDanDaoViewList = ArrayList<GiftDanDaoView>()
    private val giftModelList = LinkedList<IMGiftModel>()

    private var loopJob: Job? = null

    //用来标记往哪个GiftDanDaoView添加数据
    private var giftDanDaoViewCount = 0

    fun init(){
        SwagoRoomManager.addListener(this)
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {

    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        giftModelList.clear()
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {

    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
        giftModelList.clear()
    }

    override fun closeRoomed() {
        giftModelList.clear()
        giftDanDaoViewList.forEach {
            it.clear()
        }
        SwagoRoomManager.removeListener(this)
    }


    fun addNewMessage(imGiftModel: IMGiftModel){
        giftModelList.add(imGiftModel)
        if (loopJob==null){
            initQueue()
            loopJob?.start()
        }

    }

    private fun initQueue(){
        loopJob = GlobalScope.launch(Dispatchers.Main,start = CoroutineStart.LAZY) {
            var isLoop = true
            //用来在都忙的时候往
            while (isLoop){
                val imGiftModel  = giftModelList.peek()
                LogUtil.d("giftDanDaoViewList","peek")
                if (imGiftModel!=null){
                    //用来判断是否有未使用的飘条 把数据添加进去
                    var isSetData = false

                    for (index in 0 until giftDanDaoViewList.size){
                        if (giftDanDaoViewList[index].isBusy){
                            //判断当前正忙的礼物弹道跟新来的礼物是不是同一个
                            val imCurrentGiftModel = giftDanDaoViewList[index].imCurrentGiftModel
                            if (imCurrentGiftModel!=null){
                                if (imCurrentGiftModel.giftId==imGiftModel.giftId&&imCurrentGiftModel.senderCode==imGiftModel.senderCode){
                                    giftDanDaoViewList[index].addData(imGiftModel)
                                    isSetData = true
                                    break
                                }
                            }
                        }else{
                            //有空闲轨道
                            LogUtil.d("giftDanDaoViewList","ddddd-$index")
                            giftDanDaoViewList[index].addData(imGiftModel)
                            isSetData = true
                            break
                        }
                    }

                    if (!isSetData){
                        //表示此事所有飘条都在忙 那么按顺序添加
                        LogUtil.d("giftDanDaoViewList","${giftDanDaoViewCount%giftDanDaoViewList.size}----$giftDanDaoViewCount")
                        when(giftDanDaoViewCount%giftDanDaoViewList.size){
                            0 -> {
                                giftDanDaoViewCount++
                                giftDanDaoViewList[0].addData(imGiftModel)
                                LogUtil.d("giftDanDaoViewList","ddddd1")
                            }
                            1 -> {
                                giftDanDaoViewCount++
                                giftDanDaoViewList[1].addData(imGiftModel)
                                LogUtil.d("giftDanDaoViewList","ddddd2")
                            }
                            2 -> {
                                giftDanDaoViewCount++
                                giftDanDaoViewList[2].addData(imGiftModel)
                                LogUtil.d("giftDanDaoViewList","ddddd1")
                            }
                        }
                    }else{
                        giftDanDaoViewCount = 0
                    }

                    giftModelList.poll()
                    if (giftModelList.isEmpty()){
                        isLoop = false
                        loopJob= null
                    }
                }else{
                    isLoop = false
                    loopJob= null
                    giftDanDaoViewCount = 0
                }
            }
        }
    }
}