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

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.child.IPinyinKeyboardComponent
import com.atmob.keyboard_android.enums.KeyboardGlobalType
import com.atmob.keyboard_android.util.KeyboardHolder

/**
 * 拼音键盘组件
 */
class PinyinKeyboardComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<IPinyinKeyboardComponent>(context, attrs, defStyleAttr),
    IPinyinKeyboardComponent {
    override fun onInflateViewId(): Int {
        return R.layout.component_pinyin_keyboard
    }

    override fun findView(view: View) {
    }

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

    private fun setupViewModel() {
        KeyboardHolder.getKeyboardService()?.run {
            // 监听键盘类型切换
            getKeyboardViewModel().keyboardGlobalType.observe(getLifecycleOwner()) { globalKeyboardType ->
                if (KeyboardGlobalType.PINYIN_KEYBOARD == globalKeyboardType) {
                    show()
                } else {
                    hide()
                }
            }
        }
    }

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