package com.swago.room.user

import android.view.View
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import com.swago.baseswago.PersonDataDFragment
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.dialog.BenefitBagDialog
import com.swago.baseswago.dialog.ReportDialog
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.NoDoubleClickListener
import com.swago.baseswago.util.UserInfo
import com.swago.room.R
import com.swago.room.audio.UserByAnchorInviteDialog
import com.swago.room.base.BaseComFragment
import com.swago.room.bean.UserRoomModel
import com.swago.room.databinding.FragmentBaseComBinding
import com.swago.room.dialog.AudioNoticeUserDialog
import com.swago.room.dialog.ShareDialog
import com.swago.room.enum.RoomType
import com.swago.room.game.GameListDialog
import com.swago.room.gift.GiftDialog
import com.swago.room.hongbao.RedDetailListDialog
import com.swago.room.hongbao.RedEnvelopBroadcastDialog
import com.swago.room.hongbao.RedEnvelopeDialog
import com.swago.room.inter.IFooter
import com.swago.room.inter.IHeader
import com.swago.room.lianmai.UserLianMaiDialog
import com.swago.room.vm.RoomOtherVm
import com.swago.room.widget.ComHeaderView
import com.swago.room.widget.UserFooterView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 *@date 2021/10/11 14:24
 *description:
 */
class UserRoomFragment : BaseComFragment<FragmentBaseComBinding>() {

    override fun initOther() {
        super.initOther()
//        binding.ivBenefit.setOnClickListener(object : NoDoubleClickListener() {
//            override fun onClick() {
//                val dialog = BenefitBagDialog.newInstance()
//                dialog.benefitIconDismissFun = {
//                    UserInfo.getUserInfo()?.let {
//                        if (it.is_benefit == 0) {
//                            binding.ivBenefit.visibility = View.VISIBLE
//                        } else {
//                            binding.ivBenefit.visibility = View.GONE
//                        }
//                    }
//                }
//                dialog.show(childFragmentManager, "BenefitBagDialog")
//            }
//        })

        binding.ivReport.visibility = ConstraintLayout.VISIBLE
        binding.ivReport.setOnClickListener(object : NoDoubleClickListener() {
            override fun onClick() {
                SwagoRoomManager.iRoomInfo?.let {
                    ReportDialog.newInstance(it.getAnchorCode())
                        .show(childFragmentManager, "ReportDialog")
                }
            }
        })
    }


    override fun initLiveData() {
        super.initLiveData()

        val footerView = iFooter as UserFooterView
        footerView.openSendMsgDialog = {
            if (isForbid) {
                Toast.makeText(
                    AppContext.getContext(), AppContext.getContext().resources.getString(
                        R.string.you_are_been_forbid
                    ), Toast.LENGTH_SHORT
                ).show()
            } else {
                openSendMessageDialog()
            }
        }

        footerView.openMessageFun = {
            openMessageListDialog()
        }

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

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

        footerView.openShareFun = {
            openShareDialog()
        }

        footerView.openLianMaiFun = {
            openLianMaiListDialog()
        }

        msgVm.forbidSpeakOrCancel = {
            isForbid = it
        }

        msgVm.openRedEnvelopPortal = {
            binding.ivRedEnvelopeBroadcastView.setRedEnvelopeBroadcastData(it)
            binding.ivRedEnvelopeBroadcastView.openRedEnvelopeBroadcastDialog = {
                RedEnvelopBroadcastDialog.newInstance(it.senderName,it.senderAvatar,it.anchorId,it.anchorName).apply {
                    this.gotoTargetRoom = {
                        goToTargetRoom(it)
                    }
                }.show(childFragmentManager,"RedEnvelopBroadcastDialog")
            }
        }

        userVm.followStateLiveData.observe(this) {
            (iHeader as ComHeaderView).setFollow()
        }

        //用户在直播间是否被禁言权限
        roomVm.roomAuthLiveData.observe(this) {
            isForbid = it.is_forbid == 1 || UserInfo.getUserInfo()?.broadcast_msg_status==1
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (iRoomInfo is UserRoomModel){
                    iRoomInfo.userInRoomIsAdmin = it.is_room_admin
                }
            }
        }

        msgVm.setOrDeleteRoomAdmin = {
            SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                if (iRoomInfo is UserRoomModel){
                    iRoomInfo.userInRoomIsAdmin = it
                }
            }
        }

        msgVm.openInviteByAnchorFun = {
            UserByAnchorInviteDialog.newInstance(it.inviteName,it.inviteAvatar,it.maiIndex,it.applyId).show(childFragmentManager,"UserByAnchorInviteDialog")
        }

        (iHeader as ComHeaderView).showAudioUserNoticeFun = {
            AudioNoticeUserDialog.newInstance().show(childFragmentManager,"AudioNoticeUserDialog")
        }

        (iHeader as ComHeaderView).showUserInfo = {
            PersonDataDFragment.newInstance(it, isAnchor = false, inRoom = true,SwagoRoomManager.iRoomInfo?.getRoomId()?:"")
                .apply {
                    this.openGiftDialog = { nickName, userId ->
                        GiftDialog.newInstance(
                            nickName,
                            userId
                        ).apply {
                            this.openSendRedEnvelopeFun = {
                                RedEnvelopeDialog.newInstance(roomConfig).show(this@UserRoomFragment.childFragmentManager,"RedEnvelopeDialog")
                            }
                        }.show(parentFragmentManager, "GiftDialog")
                    }
                    this.sendMsgInRoom = {
                        if (isForbid) {
                            Toast.makeText(
                                AppContext.getContext(), AppContext.getContext().resources.getString(
                                    R.string.you_are_been_forbid
                                ), Toast.LENGTH_SHORT
                            ).show()
                        } else {
                            openSendMessageDialog("@${it} ")
                        }
                    }
                }.show(childFragmentManager, "PersonDataDFragment")
        }

    }


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

    private fun openShareDialog() {
        ShareDialog.newInstance().show(childFragmentManager,"ShareDialog")
    }

    fun dismissDialog() {
        childFragmentManager.findFragmentByTag("GiftDialog")?.let {
            if (it is GiftDialog) it.dismissAllowingStateLoss()
        }
        childFragmentManager.findFragmentByTag("BenefitBagDialog")?.let {
            if (it is BenefitBagDialog) it.dismissAllowingStateLoss()
        }
    }

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

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

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
        super.joinedRoom(iRoomInfo)
        SwagoRoomManager.iRoomInfo?.let {
            if (it.getAudioNotice().isNotEmpty()){
                AudioNoticeUserDialog.newInstance().show(childFragmentManager,"AudioNoticeUserDialog")
            }
        }
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

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

    /*********************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 val iHeader: IHeader by lazy {
        ComHeaderView(requireContext())
    }
    override val iFooter: IFooter by lazy {
        UserFooterView(requireContext())
    }
}