package com.swago.room.gift.control

import android.content.Context
import com.opensource.svgaplayer.SVGAParser
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 java.util.*

/**
 *@date 2021/12/11 16:42
 *description:
 */
class SvgPlayerManager : IRoomActiveListener {

    private val svgListQueue = LinkedList<IMGiftModel>()
    private var xSvgPlayer: XSvgPlayer? = null
    private var svgDanDaoView: SvgDanDaoView? = null

    fun init(context: Context, xSvgPlayer: XSvgPlayer,svgDanDaoView: SvgDanDaoView) {
        SVGAParser.shareParser().init(context)
        this.xSvgPlayer = xSvgPlayer
        this.svgDanDaoView = svgDanDaoView
        SwagoRoomManager.addListener(this)

        this.xSvgPlayer?.nextSvgPlay = {
            svgListQueue.poll()
            val imGiftModel = svgListQueue.peek()
            LogUtil.d("SvgPlayerManager","${svgListQueue.size}")
            if (imGiftModel==null){
                LogUtil.d("SvgPlayerManager","clear svgListQueue")
                svgListQueue.clear()
            }else{
                if (this.xSvgPlayer==null){
                    svgListQueue.clear()
                    LogUtil.d("SvgPlayerManager","xSvgPlayer 为空")
                }else{
                    this.xSvgPlayer!!.playSvga(imGiftModel)
                }

            }
        }
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {
        svgListQueue.clear()
        xSvgPlayer?.clear()
        svgDanDaoView?.clear()
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        svgListQueue.clear()
        xSvgPlayer?.clear()
        svgDanDaoView?.clear()
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {

    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {

    }

    override fun closeRoomed() {
        svgListQueue.clear()
        xSvgPlayer?.clear()
        svgDanDaoView?.clear()
        SwagoRoomManager.removeListener(this)
    }


    fun addSvgDanDaoView(imGiftModel: IMGiftModel){
        svgDanDaoView?.addData(imGiftModel)
    }

    fun addSvgUrl(imGiftModel: IMGiftModel?) {
        imGiftModel?.let {
            if (svgListQueue.isEmpty()) {
                LogUtil.d("SvgPlayerManager","数据源没有数据 新增一条")
                svgListQueue.add(it)
                this.xSvgPlayer?.playSvga(it)
            } else {
                svgListQueue.add(it)
                /*this.xSvgPlayer?.apply {
                    if (this.isPlaying){
                        LogUtil.d("SvgPlayerManager","当前队列不为空  有正在播放的svga, 新增一条")
                        svgListQueue.add(it)
                    }else{
                        LogUtil.d("SvgPlayerManager","当前队列不为空 没有正在在播放的svga, 新增一条")
                        svgListQueue.clear()
                        svgListQueue.add(it)
                        this.playSvga(it)
                    }
                }*/
            }
        }
    }
}