package com.swago.baseswago.baseroom.viewmodel

import android.app.Application
import com.swago.baseswago.baseroom.*
import com.swago.baseswago.util.BaseViewModel

/**
 *@date 2021/10/4 10:13
 *description:
 */
abstract class AbsRoomVm(application: Application) : BaseViewModel(application),IRoomActiveListener{

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

    override fun changeRoom(iRoomInfo: IRoomInfo) {
        afterRoomChange(SwagoRoomManager.roleType,iRoomInfo){
            if (it){
                when(SwagoRoomManager.roleType){
                    RoleType.user -> {
                        SwagoRoomManager.joinRoom(iRoomInfo)
                    }

                    RoleType.anchor -> {
                        SwagoRoomManager.createRoom(iRoomInfo)
                    }
                }
            }else{
                SwagoRoomManager.endRoom()
            }
        }
    }

    override fun onCleared() {
        super.onCleared()
        SwagoRoomManager.removeListener(this)
    }

    abstract fun afterRoomChange(roleType: Int,iRoomInfo: IRoomInfo,callback:(canJoinRoom:Boolean)->Unit)

}