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

import android.content.Context
import android.util.AttributeSet
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.component.child.IAiKeyboardPanelComponent
import com.atmob.keyboard_android.util.KeyboardHolder

/**
 * Ai键盘-面板基类
 */
abstract class BaseAiKeyboardPanelComponent<T : IAiKeyboardPanelComponent> @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent<T>(context, attrs, defStyleAttr) {

    /**
     * 控制Ai生成内容面板，是否显示
     */
    protected abstract fun controlAiChatPageShowing(isShow: Boolean)

    /**
     * 生成<帮聊>的内容
     *
     * @param characterId 人设Id
     */
    protected fun doChatSuperReply(
        characterId: String,
        startCallback: (() -> Unit)? = null,
        finishCallback: (() -> Unit)? = null
    ) {
        val viewModel = KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
        startCallback?.invoke()
        // 获取数据
        viewModel?.chatSuperReply(characterId, onSuccess = {
            // 获取成功
            finishCallback?.invoke()
        }, onFail = {
            // 获取失败
            finishCallback?.invoke()
        })
    }

    /**
     * 生成<教你说>的内容
     */
    protected fun doChatSuperSpeak(characterId: String, finishCallback: (() -> Unit)? = null) {
        val viewModel = KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
        viewModel?.chatSuperSpeak(characterId, onStart = {
            controlAiChatPageShowing(true)
        }, onSuccess = {
            finishCallback?.invoke()
        }, onFail = {
            controlAiChatPageShowing(false)
        })
    }

    /**
     * 生成<开场白>的内容
     */
    protected fun doChatPrologue(name: String, finishCallback: (() -> Unit)? = null) {
        val viewModel = KeyboardHolder.getKeyboardService()?.getKeyboardViewModel()
        viewModel?.chatPrologue(name, onStart = {
            controlAiChatPageShowing(true)
        }, onSuccess = {
            finishCallback?.invoke()
        }, onFail = {
            controlAiChatPageShowing(false)
        })
    }
}