package com.swago.room.piaotiao

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 2021/12/16 19:31
 *description:
 */
class WaftManager : IRoomActiveListener {

    val waftViewList = ArrayList<WaftView>()
    private val giftModelList = LinkedList<IMGiftModel>()

    private var loopJob: Job? = null

    //用来标记往哪个waftView添加数据
    private var waftViewCount = 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()
        waftViewList.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("waftViewList","peek")
                if (imGiftModel!=null){
                    //用来判断是否有未使用的飘条 把数据添加进去
                    var isSetData = false

                    for (index in 0 until waftViewList.size){
                        if (!waftViewList[index].isBusy){
                            LogUtil.d("waftViewList","ddddd-$index")
                            waftViewList[index].addData(imGiftModel)
                            isSetData = true
                            break
                        }
                    }

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

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

}