package com.atmob.keyboard_android.component.child.impl

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.component.ComponentMediator
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.component.child.IQuickSwitchComponent
import com.atmob.keyboard_android.component.root.IKeyboardRootComponent
import com.atmob.keyboard_android.enums.HelpMode
import com.atmob.keyboard_android.ext.click
import com.atmob.keyboard_android.ui.popup.ModeSwitchPopupWindow
import com.atmob.keyboard_android.util.KeyboardHolder

/**
 * 快速切换组件
 */
class QuickSwitchComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<IQuickSwitchComponent>(context, attrs, defStyleAttr), IQuickSwitchComponent {
    private lateinit var vModeSwitchLayout: View
    private lateinit var vModeText: TextView
    private lateinit var vKeyboardSelectLayout: View
    private lateinit var vKeyboardName: TextView

    /**
     * 模式切换的弹窗
     */
    private val mModeSwitchPopupWindow = ModeSwitchPopupWindow()

    override fun onInflateViewId(): Int {
        return R.layout.quick_switch_component
    }

    override fun findView(view: View) {
        vModeSwitchLayout = view.findViewById(R.id.mode_switch_layout)
        vModeText = view.findViewById(R.id.mode_text)
        vKeyboardSelectLayout = view.findViewById(R.id.keyboard_select_layout)
        vKeyboardName = view.findViewById(R.id.keyboard_name)
    }

    override fun bindView(view: View) {
        vModeSwitchLayout.click {
            // 弹出模式切换弹窗
            if (mModeSwitchPopupWindow.isShowing()) {
                mModeSwitchPopupWindow.dismiss()
            } else {
                mModeSwitchPopupWindow.show(vModeSwitchLayout, HelpMode.HELP_CHAT, object :
                    ModeSwitchPopupWindow.OnModeSelectedListener {
                    override fun onModeSelected(newMode: HelpMode) {
                        // 切换模式
                        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
                            ?.updateHelpMode(newMode)
                        // 切换到键盘页
                        val keyboardRootComponent =
                            ComponentMediator.findComponent<IKeyboardRootComponent>(
                                IKeyboardRootComponent::class.java
                            )
                        keyboardRootComponent?.switchKeyBoardPage()
                    }
                })
            }
        }
        vKeyboardSelectLayout.click {
            // 显示键盘选择页
            val keyboardRootComponent = ComponentMediator.findComponent<IKeyboardRootComponent>(
                IKeyboardRootComponent::class.java
            )
            keyboardRootComponent?.switchKeyboardSelectPage()
        }

        setupViewModel()
    }

    /**
     * 配置ViewModel
     */
    private fun setupViewModel() {
        KeyboardHolder.getKeyboardService()?.run {
            // 监听帮助模式切换
            getKeyboardViewModel().helpMode.observe(getLifecycleOwner()) { newMode ->
                vModeText.text = newMode.modeName
            }
            // 监听键盘名称切换
            getKeyboardViewModel().keyboardName.observe(getLifecycleOwner()) { newKeyboardName ->
                vKeyboardName.text = newKeyboardName
            }
        }
    }

    override fun getComponentInterfaceClazz(): Class<IQuickSwitchComponent> {
        return IQuickSwitchComponent::class.java
    }
}