package com.swago.room.audio.widget

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

class AudioGiftAnimManager : IRoomActiveListener {

    val audioGiftViewList = ArrayList<AudioContainGiftView>()
    private val audioSendGiftModelList = LinkedList<AudioSendGiftModel>()

    private var loopJob: Job? = null

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

    override fun changeRoom(iRoomInfo: IRoomInfo) {

    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        audioSendGiftModelList.clear()
        audioGiftViewList.forEach {
            it.clearAnimation()
        }
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {

    }

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

    override fun closeRoomed() {
        audioSendGiftModelList.clear()
        audioGiftViewList.forEach {
            it.clearAnimation()
        }
        loopJob?.cancel()
        loopJob = null
        SwagoRoomManager.removeListener(this)
    }


    fun addNewMessage(audioSendGiftModel: AudioSendGiftModel){
        audioSendGiftModelList.add(audioSendGiftModel)
        if (loopJob==null){
            initQueue()
            loopJob?.start()
        }

    }

    private fun initQueue(){
        loopJob = GlobalScope.launch(Dispatchers.Main,start = CoroutineStart.LAZY) {
            var isLoop = true
            //用来在都忙的时候往
            while (isLoop){
                val imGiftModel  = audioSendGiftModelList.peek()
                LogUtil.d("giftDanDaoViewList","peek")
                if (imGiftModel!=null){
                    var isSetData = false
                    if(!isSetData){
                        for (index in 0 until audioGiftViewList.size){
                            if (!audioGiftViewList[index].isBusy){
                                //有空闲轨道
                                LogUtil.d("giftDanDaoViewList","ddddd-$index")
                                audioGiftViewList[index].playGiftAnim((imGiftModel))
                                isSetData = true
                                break
                            }
                        }
                    }

                    if (isSetData){
                        audioSendGiftModelList.poll()
                        if (audioSendGiftModelList.isEmpty()){
                            isLoop = false
                            loopJob= null
                        }
                    }else{
                        delay(200)
                    }
                }else{
                    isLoop = false
                    loopJob= null
                }
            }
        }
    }
}