package com.swago.room.user

import android.view.View
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.swago.baseswago.PersonDataDFragment
import com.swago.baseswago.UserVm
import com.swago.baseswago.agora.AgoraManager
import com.swago.baseswago.baseroom.IRoomInfo
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.dialog.BenefitBagDialog
import com.swago.baseswago.model.live.gift.GiftModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.UserInfo
import com.swago.room.R
import com.swago.room.adapter.GameAdapter
import com.swago.room.base.BaseComFragment
import com.swago.room.databinding.FragmentBaseComBinding
import com.swago.room.gift.GiftConfig
import com.swago.room.gift.GiftDialog
import com.swago.room.gift.GiftVm
import com.swago.room.inter.IFooter
import com.swago.room.inter.IHeader
import com.swago.room.vm.RoomOtherVm
import com.swago.room.widget.UserFooterView
import com.swago.room.widget.UserHeaderView

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

    private val userVm by activityViewModels<UserVm>()
    private val giftVm by activityViewModels<GiftVm>()
    private val roomOtherVm by activityViewModels<RoomOtherVm>()

    //热门礼物列表
    private val hotList = ArrayList<GiftModel>()

    //奢华礼物列表
    private val luxuryList = ArrayList<GiftModel>()

    //幸运礼物列表
    private val luckyData = ArrayList<GiftModel>()

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

    override fun initOther() {
        super.initOther()
        giftVm.getGiftList()
        roomOtherVm.getGameList()
    }


    override fun initLiveData() {
        super.initLiveData()
        roomVm.roomUserDataLiveData.observe(this) {
            iHeader.updateRoomUser(it)
        }

        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.setMirrorFun = {
            SwagoRoomManager.iRoomInfo?.let {
                AgoraManager.setMirror(it.getAnchorId().toInt())
            }
        }

        footerView.openMessageFun = {
            openMessageListDialog()
        }

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

        footerView.openGameFun = {
            openGameDialog()
        }

        footerView.openBenefitFun = {
            val dialog = BenefitBagDialog.newInstance()
            dialog.benefitIconDismissFun = {
                footerView.setBenefit()
            }
            dialog.show(childFragmentManager, "BenefitBagDialog")
        }

        val headerView = iHeader as UserHeaderView
        headerView.followFun = {
            userVm.followUser(it, 0)
        }

        headerView.showUserInfo = {
            PersonDataDFragment.newInstance(it,isAnchor = false,inRoom = true).show(childFragmentManager, "PersonDataDFragment")
        }

        userVm.followStateLiveData.observe(this) {
            headerView.setFollow()
        }

        giftVm.allGiftLiveData.observe(this) {
            it.forEach { giftAllModel ->
                when (giftAllModel.gift_type) {
                    1 -> {
                        //热门礼物
                        hotList.addAll(giftAllModel.data)
                    }

                    2 -> {
                        //奢华礼物
                        luxuryList.addAll(giftAllModel.data)
                    }

                    3 -> {
                        //幸运礼物
                        luckyData.addAll(giftAllModel.data)
                        GiftConfig.list.clear()
                        GiftConfig.list.addAll(giftAllModel.position)
                    }
                }
            }
        }

        msgVm.inComeChange = {
            headerView.updateIncome(it)
        }

        msgVm.updateHotValue = {
            headerView.updateHotValue(it)
        }

        msgVm.forbidSpeakOrCancel = {
            isForbid = it
        }
    }

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

    }

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

    override fun endRoom(iRoomInfo: IRoomInfo?) {
    }

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

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