package com.swago.room.vm

import android.app.Application
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.RoleType
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.baseroom.viewmodel.AbsMsgVm
import com.swago.baseswago.im.IRoomChat
import com.swago.baseswago.im.ImConstant.anchor_close_room
import com.swago.baseswago.im.ImConstant.cancel_forbid_speak
import com.swago.baseswago.im.ImConstant.follow_anchor_success
import com.swago.baseswago.im.ImConstant.forbid_speak
import com.swago.baseswago.im.ImConstant.force_close_live_room
import com.swago.baseswago.im.ImConstant.game_win_big_prize
import com.swago.baseswago.im.ImConstant.lucky_gift
import com.swago.baseswago.im.ImConstant.red_envelope
import com.swago.baseswago.im.ImConstant.red_envelope_broadcast
import com.swago.baseswago.im.ImConstant.remove_room
import com.swago.baseswago.im.ImConstant.room_chat_text
import com.swago.baseswago.im.ImConstant.svg_gift
import com.swago.baseswago.im.ImConstant.update_audience
import com.swago.baseswago.im.ImConstant.user_exit_room
import com.swago.baseswago.im.ImConstant.user_join_room
import com.swago.baseswago.model.RedEnvelope
import com.swago.baseswago.model.im.*
import com.swago.baseswago.model.live.ForceCloseModel
import com.swago.baseswago.model.live.ReceiveModel
import com.swago.baseswago.model.im.GamePrize
import com.swago.baseswago.model.live.RoomUserChangeModel
import com.swago.baseswago.model.live.RoomUserModel
import com.swago.baseswago.model.live.gift.IMGiftModel
import com.swago.baseswago.util.UserInfo

/**
 *@date 2021/10/10 10:00
 *description:
 */
class MsgVm(application: Application) : AbsMsgVm(application) {

    var isReady = false

    var newChatMsgFun: ((iRoomChat: IRoomChat?) -> Unit)? = null

    var anchorRoomClosed: ((data: AnchorRoomClosedBean?) -> Unit)? = null

    //强制关播
    var forceCloseAnchorRoom: ((closedReason: String) -> Unit)? = null

    var inComeChange: ((income: String) -> Unit)? = null

    var updateHotValue: ((hotValue: String) -> Unit)? = null

    var kickFromRoom: (() -> Unit)? = null

    var forbidSpeakOrCancel: ((cancel: Boolean) -> Unit)? = null

    var updateUserRoom: ((data: ArrayList<RoomUserModel.ListBean>, count: Int) -> Unit)? = null

    var newRedEnvelopeCome: ((redEnvelope: RedEnvelope) -> Unit)? = null

    //红包传送门
    var openRedEnvelopPortal: ((redEnvelope: RedEnvelope) -> Unit)? = null

    /**
     * 播放svg动画
     */
    var playSvgUrl: ((data: IMGiftModel) -> Unit)? = null

    /**
     * 礼物飘条
     */
    var waftFun: ((data: IPiaoTiao) -> Unit)? = null

    /**
     * 礼物弹道展示
     */
    var showGiftDanDao: ((data: IMGiftModel) -> Unit)? = null

    override fun receiveMsg() {
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<RoomChatMsgBean>>(room_chat_text) {
            if (it.getGroupId() == (SwagoRoomManager.iRoomInfo?.getRoomGroupId() ?: "")) {
                it.data?.let {
                    newChatMsgFun?.invoke(it)
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<UserJoinRoomBean>>(user_join_room) {
            if (isReady && it.getGroupId() == (SwagoRoomManager.iRoomInfo?.getRoomGroupId()
                    ?: "")
            ) {
                if (SwagoRoomManager.roleType == RoleType.user || it.data?.senderCode != UserInfo.getUserInfo()?.user_account) {
                    it.data?.let {
                        newChatMsgFun?.invoke(it)
                        updateHotValue?.invoke(it.hotValue)
                    }
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<UserJoinRoomBean>>(user_exit_room) {
            if (it.getGroupId() == (SwagoRoomManager.iRoomInfo?.getRoomGroupId() ?: "")) {
                if (SwagoRoomManager.roleType == RoleType.user || it.data?.senderCode != UserInfo.getUserInfo()?.user_account) {
                    it.data?.let {
                        updateHotValue?.invoke(it.hotValue)
                    }
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<RoomUserChangeModel>>(update_audience) {
            it.data?.let { model ->
                if (model.room_id == (SwagoRoomManager.iRoomInfo?.getRoomId() ?: "")) {
                    updateUserRoom?.invoke(model.users, model.count)
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<AnchorRoomClosedBean>>(anchor_close_room) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (iRoomInfo.getRoomId() == (it.data?.roomId ?: "")) {
                    anchorRoomClosed?.invoke(it.data)
                }
            }
        }

        //后台强制关播
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<ForceCloseModel>>(force_close_live_room) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (iRoomInfo.getRoomId() == (it.data?.roomId ?: "")) {
                    forceCloseAnchorRoom?.invoke(it.data?.closedReason ?: "")
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<IMGiftModel>>(svg_gift) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (isReady) {
                    it.data?.let {
                        if (iRoomInfo.getRoomId() == (it.roomId ?: "")) {
                            playSvgUrl?.invoke(it)
                            inComeChange?.invoke(it.incomeCount)
                        }
                        waftFun?.invoke(it)
                    }
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<IMGiftModel>>(lucky_gift) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (isReady) {
                    it.data?.let {
                        if (iRoomInfo.getRoomId() == (it.roomId ?: "")) {
                            UserInfo.getUserInfo()?.let {  userInfoModel ->
                                if (userInfoModel.id != it.senderId){
                                    showGiftDanDao?.invoke(it)
                                    inComeChange?.invoke(it.incomeCount)
                                    if (it.multiple >= 500) {
                                        waftFun?.invoke(it)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<FollowSuccessMsgBean>>(
            follow_anchor_success
        ) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (isReady) {
                    it.data?.let {
                        if (iRoomInfo.getRoomId() == (it.roomId ?: "")) {
                            newChatMsgFun?.invoke(it)
                        }
                    }
                }
            }
        }


        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<ReceiveModel>>(remove_room) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                UserInfo.getUserInfo()?.let { userInfo ->
                    it.data?.let {
                        if (it.roomId == iRoomInfo.getRoomId() && it.receiverId == userInfo.id) {
                            kickFromRoom?.invoke()
                        }
                    }
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<ReceiveModel>>(forbid_speak) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                UserInfo.getUserInfo()?.let { userInfo ->
                    it.data?.let {
                        if (it.roomId == iRoomInfo.getRoomId() && it.receiverId == userInfo.id) {
                            forbidSpeakOrCancel?.invoke(true)
                        }
                    }
                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<ReceiveModel>>(cancel_forbid_speak) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                UserInfo.getUserInfo()?.let { userInfo ->
                    it.data?.let {
                        if (it.roomId == iRoomInfo.getRoomId() && it.receiverId == userInfo.id) {
                            forbidSpeakOrCancel?.invoke(false)
                        }
                    }
                }
            }
        }

        //红包
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<RedEnvelope>>(red_envelope) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                it.data?.let {
                    if (it.roomId == iRoomInfo.getRoomId()) {
                        newRedEnvelopeCome?.invoke(it)
                        newChatMsgFun?.invoke(it)
                    }
                }
            }
        }

        //红包全局通知
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<RedEnvelope>>(red_envelope_broadcast) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                it.data?.let {
                    if (isReady && it.roomId != iRoomInfo.getRoomId() && SwagoRoomManager.roleType == RoleType.user) {
                        //其他直播间用户收到这个全局红包的通知触发弹窗
                        openRedEnvelopPortal?.invoke(it)
                    }
                }
            }
        }
        //游戏中奖飘条
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<GamePrize>>(game_win_big_prize) {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                it.data?.let {
                    if (isReady) {
                        //游戏飘条
                        waftFun?.invoke(it)
                    }
                }
            }
        }
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {

    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {

    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {

    }

    override fun closeRoomed() {

    }
}