package com.adealink.weparty.room.chatroom

import android.content.Intent
import android.view.MotionEvent
import android.view.View
import androidx.fragment.app.viewModels
import com.adealink.frame.ext.isUIValid
import com.adealink.frame.ext.isViewBindingValid
import com.adealink.frame.log.Log
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.weparty.commonui.widget.floatview.view.ITouchDispatchEventListener
import com.adealink.weparty.module.room.listener.IMicSeatListener
import com.adealink.weparty.module.room.listener.IMicSeatViewListener
import com.adealink.weparty.module.room.listener.IRoomBottomListener
import com.adealink.weparty.room.BaseRoomFragment
import com.adealink.weparty.room.R
import com.adealink.weparty.room.chatroom.page.BaseChatRoomPageFragment
import com.adealink.weparty.room.chatroom.viewmodel.ChatRoomEventViewModel
import com.adealink.weparty.room.constant.TAG_ROOM_CHAT
import com.adealink.weparty.room.databinding.FragmentChatRoomBinding

class ChatRoomFragment : BaseRoomFragment(R.layout.fragment_chat_room),
    IMicSeatListener,
    IRoomBottomListener,
    IMicSeatViewListener,
    ITouchDispatchEventListener {

    companion object {

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

    private val binding by viewBinding(FragmentChatRoomBinding::bind)
    private val eventViewModel by viewModels<ChatRoomEventViewModel>()

    private var currFragment: BaseChatRoomPageFragment? = null

    override fun initViews() {
        super.initViews()
        showPage()
    }

    private fun showPage() {
        showPage(ChatRoomPage.DISPATCH_CENTER)
    }

    override fun observeViewModel() {
        super.observeViewModel()
        eventViewModel.backToChatRoomLD.observeWithoutCache(viewLifecycleOwner) {
            showPage(ChatRoomPage.DISPATCH_CENTER)
        }
    }

    override fun handleNewIntent(intent: Intent?) {
        if (!isViewBindingValid()) {
            return
        }
        currFragment?.handleNewIntent(intent)
    }

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

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

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

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

    override fun onTouchDispatchEvent(event: MotionEvent?) {
        currFragment?.onTouchDispatchEvent(event)
    }

    private fun showPage(page: ChatRoomPage) {
        Log.d(TAG_ROOM_CHAT, "showPage, page:$page")
        if (currFragment?.tag == page.tag) {
            return
        }
        val act = activity ?: return
        eventViewModel.updateChatRoomPage(page)
        page.init(act) { fragment ->
            if (!isUIValid()) {
                return@init
            }
            if (fragment == null) {
                //避免直接进游戏,但是游戏未准备好,则进入语聊房
                if (currFragment == null) {
                    showPage(ChatRoomPage.DISPATCH_CENTER)
                }
            } else {
                currFragment = fragment as? BaseChatRoomPageFragment
                childFragmentManager
                    .beginTransaction()
                    .setCustomAnimations(0, getExitAnim(page))
                    .replace(R.id.fl_chat_room, fragment, page.tag)
                    .commitAllowingStateLoss()
            }
        }
    }

    private fun getExitAnim(page: ChatRoomPage): Int {
        if (page.tag == ChatRoomPage.DISPATCH_CENTER.tag) {
            return R.anim.room_game_exit_to_icon
        }
        return 0
    }
}