package com.swago.room.lianmai

import android.app.Application
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import com.google.protobuf.Api
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.ImConstant
import com.swago.baseswago.inter.ApiManager
import com.swago.baseswago.model.im.CusNewMsgBean
import com.swago.baseswago.model.live.mic.ApplyLianMaiResultModel
import com.swago.baseswago.model.live.mic.LianMaiIMModel
import com.swago.baseswago.model.live.mic.LianMaiUserModel
import com.swago.baseswago.model.live.pk.StartPKModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.LogUtil
import com.swago.baseswago.util.UserInfo
import com.swago.room.R
import com.swago.room.pk.PKStateManager

class LianMaiVm(application: Application) : AbsMsgVm(application) {


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


    val applySuccessLiveData by lazy {
        MutableLiveData<ApplyLianMaiResultModel>()
    }

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

    fun applyLianMai(){
        requestData {
            SwagoRoomManager.iRoomInfo?.let {
                val data = ApiManager.roomApi.applyLianMai(it.getRoomId(),it.getBroadcastId())
                applySuccessLiveData.value = data
                LianMaiManager.lianMaiId = data.lianmai_id
                when(LianMaiManager.lianMaiState){
                    0 -> {
                        LianMaiManager.lianMaiState = 3
                    }
                    2 -> {
                        LianMaiManager.lianMaiState = 4
                    }
                    else -> {
                        LianMaiManager.lianMaiState = 3
                    }
                }
                Toast.makeText(AppContext.getContext(), AppContext.getContext().resources.getString(R.string.apply_lian_mai_success), Toast.LENGTH_SHORT).show()
            }
        }
    }

    val cancelSuccessLiveData by lazy {
        MutableLiveData<String>()
    }
    fun cancelApplyLianMai(lianMaiId:String,callback: (() -> Unit)?=null){
        requestData {
            SwagoRoomManager.iRoomInfo?.let {
                ApiManager.roomApi.refuseOrCancelLianMai(it.getRoomId(),it.getBroadcastId(),lianMaiId)
                if (SwagoRoomManager.roleType == RoleType.anchor){
                    Toast.makeText(
                        AppContext.getContext(), AppContext.getContext().resources.getString(R.string.you_have_refuse_lian_mai), Toast.LENGTH_SHORT).show()
                }else{
                    LianMaiManager.lianMaiId = ""
                    when(LianMaiManager.lianMaiState){
                        3 -> {
                            LianMaiManager.lianMaiState = 0
                        }
                        4 -> {
                            LianMaiManager.lianMaiState = 2
                        }
                        else -> {
                            LianMaiManager.lianMaiState = 0
                        }
                    }
                    Toast.makeText(
                        AppContext.getContext(), AppContext.getContext().resources.getString(
                            R.string.cancel_lian_mai_success), Toast.LENGTH_SHORT).show()
                    callback?.invoke()
                }
                cancelSuccessLiveData.value = lianMaiId
            }
        }
    }

    val acceptSuccessLiveData by lazy {
        MutableLiveData<Any>()
    }
    var acceptCount = 0
    fun acceptLianMai(){
        requestData {
            SwagoRoomManager.iRoomInfo?.let {
                ApiManager.roomApi.acceptLianMai(it.getRoomId(),it.getBroadcastId(),LianMaiManager.lianMaiId)
                LianMaiManager.lianMaiState = 1
                acceptCount++
                acceptSuccessLiveData.value = acceptCount
            }
        }
    }

    fun closeLianMai(callback:(()->Unit)? = null){
        requestData {
            SwagoRoomManager.iRoomInfo?.let {
                ApiManager.roomApi.closeLianMai(it.getRoomId(),it.getBroadcastId(),LianMaiManager.lianMaiId)
                LianMaiManager.lianMaiState = 0
                LianMaiManager.lianMaiId = ""
                callback?.invoke()
            }
        }
    }

    fun lianMaiCallback(){
        requestData {
            SwagoRoomManager.iRoomInfo?.let {
                ApiManager.roomApi.lianMaiSuccess(it.getRoomId(),it.getBroadcastId(),LianMaiManager.lianMaiId)
            }
        }
    }

    val lianMaiListLiveData by lazy {
        MutableLiveData<List<LianMaiUserModel>?>()
    }
    fun getLianMaiList(){
        requestData2 {
            requestData {
                SwagoRoomManager.iRoomInfo?.let {
                    val data = ApiManager.roomApi.getLianMaiList(it.getRoomId(),it.getBroadcastId())
                    lianMaiListLiveData.value = data.list
                }
            }
            requestError {
                lianMaiListLiveData.value = null
            }
        }
    }

    fun getRoomLianMaiInfo(){
        requestData {
            SwagoRoomManager.iRoomInfo?.let {
                val data = ApiManager.roomApi.getLianMaiInfo(it.getRoomId(),it.getBroadcastId())
                if (data.room_state==1){
                    val lianMaiIMModel = LianMaiIMModel(data.userId,data.nickName,data.avatar)
                    LianMaiManager.lianMaiState = 2
                    LianMaiManager.dispatchStartLianMai(lianMaiIMModel)
                }
            }
        }
    }



    override fun receiveMsg() {
        //申请连麦
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<LianMaiIMModel>>(ImConstant.launch_mic) {
            it.data?.let {
                SwagoRoomManager.iRoomInfo?.apply {
                    if (this.getRoomId() == it.roomId) {
                        //主播显示小红点
                        showLianMaiDotFun?.invoke()
                    }
                }
            }
        }

        //拒绝连麦
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<LianMaiIMModel>>(ImConstant.anchor_refuse_mic) {
            it.data?.let {
                SwagoRoomManager.iRoomInfo?.apply {
                    if (this.getRoomId() == it.roomId) {
                        UserInfo.getUserInfo()?.let { userInfo->
                            if (userInfo.id == it.senderId){
                                anchorRefuseUserLianMai?.invoke()
                                Toast.makeText(AppContext.getContext(), AppContext.getContext().resources.getString(R.string.anchor_refuse_you_lian_mai), Toast.LENGTH_SHORT).show()
                                when(LianMaiManager.lianMaiState){
                                    3 -> {
                                        LianMaiManager.lianMaiState = 0
                                    }
                                    4 -> {
                                        LianMaiManager.lianMaiState = 2
                                    }
                                    else -> {
                                        LianMaiManager.lianMaiState = 0
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }

        //接受连麦
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<LianMaiIMModel>>(ImConstant.anchor_accept_mic) {
            it.data?.let {
                SwagoRoomManager.iRoomInfo?.apply {
                    if (this.getRoomId() == it.roomId) {
                        LianMaiManager.lianMaiIMModel = it
                        UserInfo.getUserInfo()?.let { userInfo ->
                            if (userInfo.id == it.senderId){
                                LianMaiManager.lianMaiState = 1
                            }else{
                                if (SwagoRoomManager.roleType == RoleType.anchor){
                                    LianMaiManager.lianMaiState = 1
                                }else{
                                    when(LianMaiManager.lianMaiState){
                                        3 -> {
                                            LianMaiManager.lianMaiState = 4
                                        }
                                        else -> {
                                            LianMaiManager.lianMaiState = 2
                                        }
                                    }
                                }
                            }
                            LianMaiManager.dispatchStartLianMai(it)
                        }

                    }
                }
            }

        }
        //关闭连麦
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<LianMaiIMModel>>(ImConstant.close_mic_chat) {
            it.data?.let {
                SwagoRoomManager.iRoomInfo?.apply {
                    if (this.getRoomId() == it.roomId) {
                        LianMaiManager.lianMaiIMModel = null
                        UserInfo.getUserInfo()?.let { userInfo ->
                            if (userInfo.id == it.senderId){
                                LianMaiManager.lianMaiState = 0
                            }else{
                                if (SwagoRoomManager.roleType == RoleType.anchor){
                                    LianMaiManager.lianMaiState = 0
                                    LianMaiManager.lianMaiId = ""
                                }else{
                                    when(LianMaiManager.lianMaiState){
                                        4 -> {
                                            LianMaiManager.lianMaiState = 3
                                        }
                                        else -> {
                                            LianMaiManager.lianMaiState = 0
                                        }
                                    }
                                }
                            }
                        }
                        LianMaiManager.dispatchStopLianMai(it)
                    }
                }
            }

        }

        //异常导致关闭连麦
        imGroupNewMsgListener.handleMsgType<CusNewMsgBean<LianMaiIMModel>>(ImConstant.error_to_close_mic) {
            it.data?.let {
                SwagoRoomManager.iRoomInfo?.apply {
                    if (this.getRoomId() == it.roomId) {
                        LianMaiManager.lianMaiIMModel = null
                        UserInfo.getUserInfo()?.let { userInfo ->
                            if (userInfo.id == it.senderId){
                                LianMaiManager.lianMaiState = 0
                            }else{
                                if (SwagoRoomManager.roleType == RoleType.anchor){
                                    LianMaiManager.lianMaiState = 0
                                }else{
                                    when(LianMaiManager.lianMaiState){
                                        4 -> {
                                            LianMaiManager.lianMaiState = 3
                                        }
                                        else -> {
                                            LianMaiManager.lianMaiState = 0
                                        }
                                    }
                                }
                            }
                        }
                        LianMaiManager.dispatchStopLianMai(it)
                    }
                }
            }
        }
    }



    override fun changeRoom(iRoomInfo: IRoomInfo) {
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {

    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

    override fun closeRoomed() {
    }
}