package com.swago.room.vm

import android.app.Application
import android.util.Log
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.forbid_speak
import com.swago.baseswago.im.ImConstant.lucky_gift
import com.swago.baseswago.im.ImConstant.remove_room
import com.swago.baseswago.model.im.CusNewMsgBean
import com.swago.baseswago.model.im.RoomChatMsgBean
import com.swago.baseswago.im.ImConstant.room_chat_text
import com.swago.baseswago.im.ImConstant.svg_gift
import com.swago.baseswago.im.ImConstant.user_exit_room
import com.swago.baseswago.im.ImConstant.user_join_room
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.model.im.AnchorRoomClosedBean
import com.swago.baseswago.model.im.UserJoinRoomBean
import com.swago.baseswago.model.live.ReceiveModel
import com.swago.baseswago.model.live.gift.IMGiftModel
import com.swago.baseswago.model.mine.RoomBlack
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 inComeChange:((income:String)->Unit)? = null

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

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

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

    var updateUserRoom:((userJoinRoomBean:UserJoinRoomBean,isJoinOrExit:Boolean)->Unit)? = null

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

    /**
     * 礼物飘条
     */
    var waftFun:((data:IMGiftModel)->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 (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)
                        updateUserRoom?.invoke(it,true)
                    }

                }
            }
        }

        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)
                        updateUserRoom?.invoke(it,false)
                    }

                }
            }
        }

        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<AnchorRoomClosedBean>>(anchor_close_room){
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (iRoomInfo.getRoomId()== it.data?.roomId?:""){
                    anchorRoomClosed?.invoke(it.data)
                }
            }
        }
        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?:""){
                            showGiftDanDao?.invoke(it)
                            inComeChange?.invoke(it.incomeCount)
                        }
                    }
                }
            }
        }


        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)
                        }
                    }
                }
            }
        }
    }

    override fun changeRoom(iRoomInfo: IRoomInfo) {

    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {

    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {

    }

    override fun closeRoomed() {

    }
}