package com.atmob.keyboard_android.mvvm.repository

import com.atmob.keyboard_android.ext.asFlow
import com.atmob.keyboard_android.ext.callback2Coroutines
import com.atmob.keyboard_android.model.base.BaseResp
import com.atmob.keyboard_android.mvvm.base.BaseRepository
import com.atmob.keyboard_android.util.bridge.FlutterBridgeManager
import com.atmob.keyboard_android.util.bridge.model.base.EmptyResp
import com.atmob.keyboard_android.util.bridge.model.req.SuperReplyReq
import com.atmob.keyboard_android.util.bridge.model.req.SuperSpeakReq
import com.atmob.keyboard_android.util.bridge.model.resp.CharacterListResp
import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardInfo
import com.atmob.keyboard_android.util.bridge.model.resp.KeyboardListResp
import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp
import com.atmob.keyboard_android.util.bridge.model.resp.PrologueResp
import com.atmob.keyboard_android.util.bridge.model.resp.SuperSpeakResp
import kotlinx.coroutines.flow.Flow

/**
 * 键盘Repository
 */
class KeyboardRepository : BaseRepository() {
    /**
     * 用户是否已登录
     */
    fun isLogin(
        onSuccess: (resultObj: Boolean) -> Unit,
        onFail: (errorCode: Int, errorMsg: String) -> Unit
    ) {
        FlutterBridgeManager.isLogin(onSuccess, onFail)
    }

    /**
     * 用户是否为VIP
     */
    fun isMember(
        onSuccess: (resultObj: Boolean) -> Unit,
        onFail: (errorCode: Int, errorMsg: String) -> Unit
    ) {
        FlutterBridgeManager.isMember(onSuccess, onFail)
    }

    /**
     * 获取键盘列表
     */
    fun getKeyboardList(): Flow<BaseResp<KeyboardListResp>> = asFlow {
        callback2Coroutines { onSuccess, onFail ->
            FlutterBridgeManager.getKeyboardList(onSuccess, onFail)
        }
    }

    /**
     * 选择键盘
     */
    fun selectedKeyboard(
        keyboardId: String,
    ): Flow<BaseResp<EmptyResp>> = asFlow {
        callback2Coroutines { onSuccess, onFail ->
            FlutterBridgeManager.selectedKeyboard(
                keyboardId,
                onSuccess,
                onFail
            )
        }
    }

    /**
     * 获取当前键盘信息
     */
    fun getCurrentKeyboardInfo(): Flow<BaseResp<KeyboardInfo>> = asFlow {
        callback2Coroutines { onSuccess, onFail ->
            FlutterBridgeManager.getCurrentKeyboardInfo(onSuccess, onFail)
        }
    }

    /**
     * 获取人设列表
     */
    fun getCharacterList(
        keyboardId: String,
    ): Flow<BaseResp<CharacterListResp>> = asFlow {
        callback2Coroutines { onSuccess, onFail ->
            FlutterBridgeManager.getCharacterList(keyboardId, onSuccess, onFail)
        }
    }

    /**
     * 获取开场白列表
     */
    fun getPrologueList(): Flow<BaseResp<PrologueListResp>> = asFlow {
        callback2Coroutines { onSuccess, onFail ->
            FlutterBridgeManager.getPrologueList(onSuccess, onFail)
        }
    }

    /**
     * AI聊天-超会回（帮聊）
     */
    fun chatSuperReply(
        req: SuperReplyReq,
    ) = asFlow {
        callback2Coroutines { onSuccess, onFail ->
            FlutterBridgeManager.chatSuperReply(req, onSuccess, onFail)
        }
    }

    /**
     * AI聊天-超会说（教你说）
     */
    fun chatSuperSpeak(
        req: SuperSpeakReq,
    ): Flow<BaseResp<SuperSpeakResp>> = asFlow {
        callback2Coroutines { onSuccess, onFail ->
            FlutterBridgeManager.chatSuperSpeak(req, onSuccess, onFail)
        }
    }

    /**
     * AI聊天-开场白
     */
    fun chatPrologue(
        name: String,
    ): Flow<BaseResp<PrologueResp>> = asFlow {
        callback2Coroutines { onSuccess, onFail ->
            FlutterBridgeManager.chatPrologue(name, onSuccess, onFail)
        }
    }
}