package com.swago.baseswago.baseroom

import java.util.concurrent.CopyOnWriteArrayList

/**
 *@date 2021/10/3 11:17
 *description:
 */
open class RoomStateManager{

    val iRoomActiveListeners = CopyOnWriteArrayList<IRoomActiveListener>()

    var iRoomInfo:IRoomInfo? = null

    var roleType = RoleType.user

    fun addListener(iRoomActiveListener: IRoomActiveListener){
        iRoomActiveListeners.add(iRoomActiveListener)
    }

    fun removeListener(iRoomActiveListener: IRoomActiveListener){
        iRoomActiveListeners.add(iRoomActiveListener)
    }


    protected fun joinedRoom(iRoomInfo: IRoomInfo){
        roleType = RoleType.user
        this.iRoomInfo = iRoomInfo
        iRoomActiveListeners.forEach {
            it.joinedRoom(iRoomInfo)
        }
    }


    fun startLiveRoom(iRoomInfo: IRoomInfo){
        roleType = RoleType.anchor
        this.iRoomInfo = iRoomInfo
        iRoomActiveListeners.forEach {
            it.joinedRoom(iRoomInfo)
        }
    }


    fun closedRoom(){
        iRoomActiveListeners.forEach {
            it.closeRoomed()
        }
        this.iRoomInfo = null
    }


    fun changeRoom(iRoomInfo: IRoomInfo){
        if (this.iRoomInfo!=null){
            iRoomActiveListeners.forEach {
                it.leaveRoom(this.iRoomInfo!!)
            }
            this.iRoomInfo = null
        }
        iRoomActiveListeners.forEach {
            it.changeRoom(iRoomInfo)
        }
    }

    fun endRoom(){
        iRoomActiveListeners.forEach {
            it.endRoom(iRoomInfo)
        }
    }

    fun clearListener(){
        iRoomActiveListeners.clear()
    }
}