package com.swago.room.user

import android.view.View
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.activityViewModels
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.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
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.gift.GiftDialog
import com.swago.room.inter.IFooter
import com.swago.room.inter.IHeader
import com.swago.room.vm.RoomOtherVm
import com.swago.room.widget.ComHeaderView
import com.swago.room.widget.UserFooterView

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


    private val roomOtherVm by activityViewModels<RoomOtherVm>()

    private var isForbid = false //是否被禁言

    override fun initOther() {
        super.initOther()
        roomOtherVm.getGameList()
        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.VISIBLE
                        }
                    }
                }
                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()
        roomVm.roomAuthLiveData.observe(this) {
            isForbid = it.is_forbid == 1
        }

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

        }

        roomOtherVm.gameListModelLiveData.observe(this) {
            footerView.setGameIcon(it.status == 1)
        }

        footerView.openMessageFun = {
            openMessageListDialog()
        }

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

        footerView.openGameFun = {
            openGameDialog()
        }

        msgVm.forbidSpeakOrCancel = {
            isForbid = it
        }

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


        (iHeader as ComHeaderView).showUserInfo = {
            PersonDataDFragment.newInstance(it, isAnchor = false, inRoom = true)
                .apply {
                    this.openGiftDialog = { nickName, userId ->
                        GiftDialog.newInstance(
                            nickName,
                            userId
                        ).show(parentFragmentManager, "GiftDialog")
                    }
                }.show(childFragmentManager, "PersonDataDFragment")
        }

    }

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

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

    override fun joinedRoom(iRoomInfo: IRoomInfo) {
        super.joinedRoom(iRoomInfo)
        UserInfo.getUserInfo()?.let {
            if (it.id.isNotEmpty()) {
                SwagoRoomManager.iRoomInfo?.let { iRoomInfo ->
                    roomVm.getUserRoomAuth(it.id, iRoomInfo.getRoomId())
                }
            }

            if (it.is_benefit == 0) {
                binding.ivBenefit.visibility = View.VISIBLE
            } else {
                binding.ivBenefit.visibility = View.VISIBLE
            }

        }
    }

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

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

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