package com.adealink.weparty.room.chatroom.page.dispatchcenter

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.fastLazy
import com.adealink.frame.ext.isViewBindingValid
import com.adealink.frame.log.Log
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.room.data.TAG_ROOM
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.layout.DynamicLayers
import com.adealink.weparty.room.R
import com.adealink.weparty.room.chat.ChatMessageFragment
import com.adealink.weparty.room.chatroom.page.BaseChatRoomPageFragment
import com.adealink.weparty.room.constant.logRoomTime
import com.adealink.weparty.room.databinding.FragmentChatRoomDispatchCenterBinding
import com.adealink.weparty.room.guide.HowToOrderGuide
import com.adealink.weparty.room.guide.RoomGuideLabel
import com.adealink.weparty.room.micseat.MicSeatFragment
import com.adealink.weparty.room.theme.ThemeComp
import com.adealink.weparty.R as APP_R


class DispatchCenterRoomPageFragment :
    BaseChatRoomPageFragment(R.layout.fragment_chat_room_dispatch_center) {

    companion object {

        fun newInstance(): DispatchCenterRoomPageFragment {
            return DispatchCenterRoomPageFragment()
        }
    }

    private val binding by viewBinding(FragmentChatRoomDispatchCenterBinding::bind)

    //动态布局
    private lateinit var baseDynamicLayers: DynamicLayers<ConstraintLayout, ConstraintLayout.LayoutParams>
    private lateinit var roomInfoFragment: DispatchCenterRoomInfoFragment
    private lateinit var micSeatFragment: MicSeatFragment
    private lateinit var chatMessageFragment: ChatMessageFragment
    private lateinit var bottomOperateFragment: DispatchRoomBottomOperateFragment
    private val fragmentList = arrayListOf<BaseFragment>()
    private val compList = arrayListOf<ViewComponent>()

    private val howToOrderGuide by fastLazy {
        HowToOrderGuide(
            onBeforeShowPageListener = { label ->
                if (label == RoomGuideLabel.HOW_TO_ORDER_1) {
                    bottomOperateFragment.showJoinGuideButton(true)
                } else if (label == RoomGuideLabel.HOW_TO_ORDER_4) {
                    binding.vGuide4.root.show()
                }
            },
            pageDismissListener = { label ->
                if (label == RoomGuideLabel.HOW_TO_ORDER_1) {
                    bottomOperateFragment.showJoinGuideButton(false)
                } else if (label == RoomGuideLabel.HOW_TO_ORDER_4) {
                    binding.vGuide4.root.gone()
                }
            }
        )
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        howToOrderGuide.register(this)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        howToOrderGuide.unregister()
    }

    override fun initViews() {
        super.initViews()
        logRoomTime("DispatchCenterRoomPageFragment initViews")
        //仅测试使用
        //toastEnterRoomTime()
        //初始化动态层级
        baseDynamicLayers =
            DynamicLayers(
                this,
                binding.root,
                DISPATCH_CENTER_BASE_LAYER,
                DispatchCenterBaseLayerFactory()
            )

        //初始化房间基础控件
        binding.idRoomTopBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = activity.statusBarHeight() + 12.dp()
        }
        roomInfoFragment = findFragmentByTag(getCompatString(R.string.tag_room_top_info_fragment))
        micSeatFragment = findFragmentByTag(getCompatString(R.string.tag_room_mic_seat_fragment))
        chatMessageFragment =
            findFragmentByTag(getCompatString(R.string.tag_room_chat_message_fragment))
        bottomOperateFragment =
            findFragmentByTag(getCompatString(R.string.tag_room_bottom_bar_fragment))
        fragmentList.add(roomInfoFragment)
        fragmentList.add(micSeatFragment)
        fragmentList.add(chatMessageFragment)
        fragmentList.add(bottomOperateFragment)
        binding.exitBtn.setOnClickListener {
            activity?.finish()
            //exitRoom(LeaveRoomReason.INITIATIVE, false)
        }
    }

    override fun initComponents() {
        super.initComponents()
        Log.d(TAG_ROOM, "chat fragment initComponents")
        compList.add(ThemeComp(this, binding.flTheme, APP_R.drawable.room_default_bg).attach())
    }

    override fun handleNewIntent(intent: Intent?) {
        if (!isViewBindingValid()) {
            return
        }
        fragmentList.forEach {
            it.onNewIntent(intent)
        }
        compList.forEach {
            it.onNewIntent(intent)
        }

    }

    @Suppress("UNCHECKED_CAST")
    private fun <T : Fragment> findFragmentByTag(tag: String): T {
        return childFragmentManager.findFragmentByTag(tag) as T
    }

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

    override fun onRoomMinimize(minimize: (Boolean) -> Unit) {
        minimize.invoke(true)
    }

    override fun onRoomClose(close: (Boolean) -> Unit) {
        close.invoke(true)
    }

    override fun getSeatViewByUid(uid: String): View? {
        return micSeatFragment.getSeatViewByUid(uid)
    }

    override fun getOperateItemById(itemId: Int): View? {
        return bottomOperateFragment.getOperateItemById(itemId)
    }

}