package com.swago.room.vm

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.RoleType
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.baseroom.viewmodel.AbsRoomVm
import com.swago.baseswago.http.SwagoException
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.model.im.AnchorRoomClosedBean
import com.swago.baseswago.model.live.AnchorClosedModel
import com.swago.baseswago.model.live.RoomAuthModel
import com.swago.baseswago.model.live.RoomUserModel
import com.swago.baseswago.util.ActivityManagerUtil
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.SwagoLoading
import com.swago.room.bean.UserRoomModel

/**
 *@date 2021/10/10 09:59
 *description:
 */
class RoomVm(application: Application) : AbsRoomVm(application) {

    //当前选中的游戏
    var currentGameType = -1

    val roomUserDataLiveData by lazy {
        MutableLiveData<List<RoomUserModel.ListBean>>()
    }

    val roomClosedLiveData by lazy {
        MutableLiveData<AnchorRoomClosedBean>()
    }

    val anchorCloseLiveData by lazy {
        MutableLiveData<AnchorClosedModel>()
    }

    override fun afterRoomChange(
        roleType: Int,
        iRoomInfo: IRoomInfo,
        callback: (canJoinRoom: Boolean) -> Unit
    ) {
        if (roleType == RoleType.user) {
            doUserRoomChange(roleType, iRoomInfo, callback)
        } else if (roleType == RoleType.anchor) {
            doAnchorRoomChange(roleType, iRoomInfo, callback)
        }
    }

    private fun doUserRoomChange(
        roleType: Int,
        iRoomInfo: IRoomInfo,
        callback: (canJoinRoom: Boolean) -> Unit
    ) {
        requestData2 {
            requestData {
                val roomModel = ApiManager.roomApi.joinRoom(iRoomInfo.getRoomId())
                val roomInfo = iRoomInfo as UserRoomModel
                roomInfo.roomModel = roomModel
                callback.invoke(true)
            }

            requestError {
                if (it is SwagoException) {
                    if (it.code == 504) {
                        if (SwagoRoomManager.iRoomInfo == null){
                            SwagoRoomManager.closeRoom()
                        }else{
                            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                                if (iRoomInfo is UserRoomModel){
                                    val anchorRoomClosedBean = AnchorRoomClosedBean()
                                    anchorRoomClosedBean.senderName = iRoomInfo.momentModel?.user_name
                                    anchorRoomClosedBean.userAvatar = iRoomInfo.momentModel?.user_head_img_url
                                    anchorRoomClosedBean.userCode = iRoomInfo.momentModel?.user_account
                                    roomClosedLiveData.value = anchorRoomClosedBean
                                }
                            }
                        }
                    } else {
                        Toast.makeText(AppContext.getContext(), it.message, Toast.LENGTH_SHORT)
                            .show()
                        SwagoRoomManager.closeRoom()
                        callback.invoke(false)
                    }
                } else {
                    callback.invoke(false)
                    SwagoRoomManager.closeRoom()
                    Toast.makeText(AppContext.getContext(), it.message, Toast.LENGTH_SHORT).show()
                }

            }

        }
    }

    private fun doAnchorRoomChange(
        roleType: Int,
        iRoomInfo: IRoomInfo,
        callback: (canJoinRoom: Boolean) -> Unit
    ) {
        requestData2 {
            requestData {
                SwagoLoading.showLoadingDialog(ActivityManagerUtil.get().currentActivity())
                val roomInfo = iRoomInfo as UserRoomModel
                val roomModel = ApiManager.roomApi.startLive(currentGameType)
                roomInfo.roomModel = roomModel
                callback.invoke(true)
                SwagoLoading.cancelLoadingDialog()
            }

            requestError {
                if (it is SwagoException) {
                    Toast.makeText(AppContext.getContext(), it.message, Toast.LENGTH_SHORT).show()
                }
                SwagoRoomManager.closeRoom()
                callback.invoke(false)
            }

        }
    }

    fun loadRoomData(roomId: String) {
        requestData {
            val data = ApiManager.roomApi.getRoomUser(roomId)
            roomUserDataLiveData.value = data.list
        }
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        leaveCurrentRoom(iRoomInfo)
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {}

    override fun endRoom(iRoomInfo: IRoomInfo?) {

    }

    override fun closeRoomed() {
        when (SwagoRoomManager.roleType) {
            RoleType.anchor -> {
                if (SwagoRoomManager.iRoomInfo == null) {
                    ActivityManagerUtil.get().currentActivity().finish()
                } else {
                    requestData2(false) {
                        requestData {
                            SwagoLoading.showLoadingDialog(
                                ActivityManagerUtil.get().currentActivity()
                            )
                            val data = ApiManager.roomApi.closeLive()
                            SwagoLoading.cancelLoadingDialog()
                            anchorCloseLiveData.value = data
                        }

                        requestError {
                            ActivityManagerUtil.get().currentActivity().finish()
                        }
                    }
                }
            }

            RoleType.user -> {
                SwagoRoomManager.iRoomInfo?.let {
                    leaveCurrentRoom(it)
                }
                ActivityManagerUtil.get().finishTopActivity()
            }
        }
    }

    private fun leaveCurrentRoom(iRoomInfo: IRoomInfo) {
        requestData2(false) {
            requestData {
                ApiManager.roomApi.leaveRoom(iRoomInfo.getRoomId())
            }

            requestError {

            }
        }
    }

    val roomAuthLiveData by lazy {
        MutableLiveData<RoomAuthModel>()
    }

    fun getUserRoomAuth(userId: String, roomId: String) {
        requestData {
            roomAuthLiveData.value = ApiManager.roomApi.getUserRoomAuth(roomId,userId)
        }
    }
}