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

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.component.base.RouteComponent
import com.atmob.keyboard_android.component.child.IAiKeyboardComponent
import com.atmob.keyboard_android.enums.HelpMode
import com.atmob.keyboard_android.enums.KeyboardGlobalType
import com.atmob.keyboard_android.util.KeyboardHolder


/**
 * AI键盘组件，里面包含2个组件，分别为通用类型键盘面板、开场白类型键盘面板
 */
class AiKeyboardComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<IAiKeyboardComponent>(context, attrs, defStyleAttr), IAiKeyboardComponent {
    private lateinit var vAiKeyboardRouteComponent: RouteComponent

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

    override fun findView(view: View) {
        vAiKeyboardRouteComponent = view.findViewById(R.id.ai_keyboard_route_component)
    }

    override fun bindView(view: View) {
        setupViewModel()
        setData()
    }

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

    /**
     * 配置ViewModel
     */
    private fun setupViewModel() {
        KeyboardHolder.getKeyboardService()?.run {
            // 监听全局键盘类型切换
            getKeyboardViewModel().keyboardGlobalType.observe(getLifecycleOwner()) { globalKeyboardType ->
                if (KeyboardGlobalType.AI_KEYBOARD == globalKeyboardType) {
                    show()
                } else {
                    hide()
                }
            }
            // 监听帮助模式更新，切换键盘
            getKeyboardViewModel().helpMode.observe(getLifecycleOwner()) { newMode ->
                // 切换键盘
                updateKeyboardByHelpMode(newMode)
            }
        }
    }

    /**
     * 填充数据
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun setData() {
    }

    /**
     * 根据帮助模式，切换键盘内容
     */
    private fun updateKeyboardByHelpMode(helpMode: HelpMode) {
        // 根据不同的模式，切换不同的键盘
        if (HelpMode.OPEN_REMARKS == helpMode) {
            // 开场白模式
            vAiKeyboardRouteComponent.routeChildComponent(
                AiKeyboardProloguePanelComponent::class.java,
                true
            )
        } else {
            // 其他模式
            vAiKeyboardRouteComponent.routeChildComponent(
                AiKeyboardCommonPanelComponent::class.java,
                true
            )
        }
    }
}