package com.adealink.weparty.im.session.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.im.databinding.LayoutSessionBottomBarBinding
import com.adealink.weparty.im.session.comp.input.InputAction
import com.adealink.weparty.im.session.comp.input.InputState
import com.adealink.weparty.im.session.comp.viewmodel.SessionInputViewModel
import com.tencent.qcloud.tuikit.tuichat.bean.ChatInfo
import com.tencent.qcloud.tuikit.tuichat.presenter.ChatPresenter

class SessionBottomComp(
    lifecycleOwner: LifecycleOwner,
    val bottomBar: LayoutSessionBottomBarBinding,
    val chatInfo: ChatInfo? = null,
    val presenter: ChatPresenter? = null
) : ViewComponent(lifecycleOwner) {

    private val inputViewModel by activityViewModels<SessionInputViewModel>()

    override fun onCreate() {
        super.onCreate()
        initView()
        initComponent()
        observeViewModel()
    }

    private fun initView() {
        inputViewModel.execute(InputAction.EMPTY_CLICKED)
    }

    private fun initComponent() {
        SessionBottomInputComp(
            lifecycleOwner,
            bottomBar.vInputBar,
            bottomBar.flEmotion,
            chatInfo,
            presenter
        ).attach()
        SessionBottomAudioComp(lifecycleOwner, bottomBar.vVoiceBar, chatInfo, presenter).attach()
    }

    private fun observeViewModel() {
        inputViewModel.inputStateLD.observe(viewLifecycleOwner) { state ->
            when (state.currentState) {
                InputState.STATE_NONE,
                InputState.STATE_SOFT_INPUT,
                InputState.STATE_EMOJI_INPUT,
                InputState.STATE_IMAGE_INPUT -> {
                    bottomBar.vVoiceBar.root.gone()
                    bottomBar.vInputBar.root.show()
                }

                InputState.STATE_AUDIO_INPUT -> {
                    bottomBar.vVoiceBar.root.show()
                    bottomBar.vInputBar.root.gone()
                }
            }
        }

        inputViewModel.sendMessageResultLD.observe(viewLifecycleOwner) { result ->
            showFailedToast(result)
        }

    }

}