package com.swago.room.anchor

import android.widget.Toast
import com.swago.baseswago.PersonDataDFragment
import com.swago.baseswago.agora.AgoraManager
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.dialog.ChoiceDialogFragment
import com.swago.baseswago.model.live.pk.ProcessPKModel
import com.swago.baseswago.model.live.pk.PunishPKModel
import com.swago.baseswago.model.live.pk.StartPKModel
import com.swago.baseswago.model.live.pk.StopPKModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.UserInfo
import com.swago.room.R
import com.swago.room.base.BaseComFragment
import com.swago.room.databinding.FragmentBaseComBinding
import com.swago.room.dialog.AudioNoticeDialog
import com.swago.room.game.GameListDialog
import com.swago.room.gift.GiftDialog
import com.swago.room.hongbao.RedEnvelopeDialog
import com.swago.room.inter.IFooter
import com.swago.room.inter.IHeader
import com.swago.room.lianmai.AnchorLianMaiDialog
import com.swago.room.pk.PKAcceptDialog
import com.swago.room.pk.PKInviteDialog
import com.swago.room.pk.PKSearchLisDialog
import com.swago.room.widget.AnchorFooterView
import com.swago.room.widget.ComHeaderView

/**
 *@date 2021/10/9 11:35
 *description:
 */
class AnchorRoomFragment : BaseComFragment<FragmentBaseComBinding>() {

    private var footerView : AnchorFooterView? = null
    private var pkAcceptDialog:PKAcceptDialog? = null
    private var pkInviteDialog:PKInviteDialog? = null

    override fun initOther() {
        super.initOther()
        //语音列表
        UserInfo.getUserInfo()?.let {
            roomVm.getAudioList(it.id,1)
        }

    }

    override fun initLiveData() {
        super.initLiveData()
        footerView = iFooter as AnchorFooterView
        footerView?.openSendMsgDialog = {
            openSendMessageDialog()
        }

        footerView?.openMessageFun = {
            openMessageListDialog()
        }

        footerView?.openGameFun = {
            if (gameList.size == 1){
                //直接弹出游戏弹窗
                setGameDefaultData(0)
            }else{
                //弹出游戏选择弹窗
                //弹出游戏选择弹窗
                GameListDialog.newInstance(gameList).apply {
                    this.openDialogFun = {
                        openGameDialog()
                    }
                }.show(childFragmentManager,"GameListDialog")
            }
        }

        footerView?.openPKFun = {
            openPKSearchDialog()
        }

        footerView?.openLianMaiFun = {
            openLianMaiListDialog()
        }

        footerView?.clearJiFen = {
            ChoiceDialogFragment.newInstance(AppContext.getContext().resources.getString(R.string.ji_fen_to_zero)).apply {
                this.sureFun = {
                    roomVm.clearJiFen()
                }
            }.show(childFragmentManager,"ChoiceDialogFragment")
        }
        footerView?.openAudioNoticeDialogFun = {
            AudioNoticeDialog.newInstance().show(childFragmentManager,"AudioNoticeDialog")
        }

        footerView?.openGiftPanelFun = {
            //打开礼物列表
            SwagoRoomManager.iRoomInfo?.let {
                if (it.getAnchorId().isNotEmpty()) {
                    GiftDialog.newInstance(
                        it.getAnchorName(),
                        it.getAnchorId()
                    ).apply {
                        this.openSendRedEnvelopeFun = {
                            RedEnvelopeDialog.newInstance(roomConfig).show(this@AnchorRoomFragment.childFragmentManager,"RedEnvelopeDialog")
                        }
                    }.show(childFragmentManager, "GiftDialog")
                }
            }
        }

        (iHeader as ComHeaderView).showUserInfo = {
            PersonDataDFragment.newInstance(it, isAnchor = true, inRoom = true,SwagoRoomManager.iRoomInfo?.getRoomId()?:"")
                .apply {
                    this.openGiftDialog = {nickName, userId ->
                        GiftDialog.newInstance(
                            nickName,
                            userId
                        ).apply {
                            this.openSendRedEnvelopeFun = {
                                RedEnvelopeDialog.newInstance(roomConfig).show(this@AnchorRoomFragment.childFragmentManager,"RedEnvelopeDialog")
                            }
                        }.show(parentFragmentManager, "GiftDialog")
                    }
                    this.sendMsgInRoom = {
                        openSendMessageDialog("@${it} ")
                    }
                }.show(childFragmentManager,"PersonDataDFragment")
        }

        pkVm.acceptFromSenderPKLiveData.observe(this){
            pkAcceptDialog = PKAcceptDialog.newInstance(it)
            pkAcceptDialog?.show(childFragmentManager,"PKAcceptDialog")
        }

        pkVm.acceptOrRefuseLiveData.observe(this){
            if (it ==2){
                pkAcceptDialog?.dismissAllowingStateLoss()
            }else if(it ==3||it ==4||it ==5){
                pkInviteDialog?.dismissAllowingStateLoss()
            }
        }

        lianMaiVm.showLianMaiDotFun = {
            (iFooter as AnchorFooterView).showLianMaiDot()
        }
    }

    private fun openLianMaiListDialog() {
        AnchorLianMaiDialog.newInstance().show(childFragmentManager,"AnchorLianMaiDialog")
    }

    private fun openPKSearchDialog() {
        PKSearchLisDialog.newInstance().apply {
            openPKInviteFun = {
                pkInviteDialog = PKInviteDialog.newInstance(it)
                pkInviteDialog?.show(parentFragmentManager,"PKInviteDialog")
            }
        }.show(childFragmentManager,"PKSearchLisDialog")
    }


    override fun changeRoom(iRoomInfo: IRoomInfo) {
        super.changeRoom(iRoomInfo)
    }

    override fun leaveRoom(iRoomInfo: IRoomInfo) {
        super.leaveRoom(iRoomInfo)
    }

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
        super.joinedRoom(iRoomInfo)
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }


    /*********************PK**************************/
    override fun startPK(startPKModel: StartPKModel, isGetDataFromIM: Boolean) {
    }

    override fun stopPK(stopPKModel: StopPKModel) {
    }

    override fun punishPK(punishPKModel: PunishPKModel, isGetDataFromIM: Boolean) {
    }

    override fun processChangePK(processPKModel: ProcessPKModel,isGetDataFromIM: Boolean) {
    }
    /*********************PK**************************/


    override fun openSeatMic() {
        super.openSeatMic()
        footerView?.let {
            it.isMute = !it.isMute
        }
    }

    override fun muteSeatMic() {
        super.muteSeatMic()
        footerView?.let {
            it.isMute = !it.isMute
        }
    }


    override fun closeRoomed() {
        super.closeRoomed()
    }

    override val iHeader: IHeader by lazy {
        ComHeaderView(requireContext())
    }
    override val iFooter: IFooter by lazy {
        AnchorFooterView(requireContext())
    }
}