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

import android.content.Context
import android.util.AttributeSet
import com.atmob.keyboard_android.component.child.ai.IPrologueAiChatComponent
import com.atmob.keyboard_android.component.child.impl.AiChatComponent
import com.atmob.keyboard_android.ext.setGone
import com.atmob.keyboard_android.ext.setVisible
import com.atmob.keyboard_android.util.KeyboardHolder

/**
 * 开场白-Ai生成内容页-实现类
 */
class PrologueAiChatComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AiChatComponent<IPrologueAiChatComponent>(context, attrs, defStyleAttr),
    IPrologueAiChatComponent {
    override fun getComponentInterfaceClazz(): Class<IPrologueAiChatComponent> {
        return IPrologueAiChatComponent::class.java
    }

    override fun setupViewModel() {
        // 监听Ai生成列表的更新
        KeyboardHolder.getKeyboardService()?.run {
            getKeyboardViewModel().prologueChatListResult.observe(getLifecycleOwner()) {
                if (it.isEmpty()) {
                    vReGenerateBtn.setGone()
                } else {
                    vReGenerateBtn.setVisible()
                }
                loadAiList(it)
            }
        }
    }

    override fun onComponentShow() {
        super.onComponentShow()
        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
            ?.updatePrologueAiChatPageShowing(true)
    }

    override fun onComponentHide() {
        super.onComponentHide()
        KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
            ?.updatePrologueAiChatPageShowing(false)
    }
}