package com.atmob.keyboard_android.mvvm.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.enums.HelpMode
import com.atmob.keyboard_android.enums.KeyboardGlobalType
import com.atmob.keyboard_android.model.KeyboardSelectModel
import com.atmob.keyboard_android.mvvm.repository.KeyboardRepository
import com.atmob.keyboard_android.util.ContextUtil
import com.atmob.keyboard_android.util.bridge.model.base.EmptyResp
import com.atmob.keyboard_android.util.bridge.model.resp.CharacterListResp
import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.PrologueListItem
import com.atmob.keyboard_android.util.bridge.model.resp.PrologueListResp.Topic

/**
 * 键盘ViewModel
 */
class KeyboardViewModel : ViewModel() {
    /**
     * 仓库层
     */
    private val mKeyboardRepository = KeyboardRepository()

    // ----------------------------------- 响应式变量 -----------------------------------

    /**
     * 登录页，是否展示
     */
    private val _loginPageShowing = MutableLiveData(false)
    val loginPageShowing: LiveData<Boolean> = _loginPageShowing

    /**
     * 设置页，是否展示
     */
    private val _settingPageShowing = MutableLiveData(false)
    val settingPageShowing: LiveData<Boolean> = _settingPageShowing

    /**
     * VIP页，是否显示
     */
    private val _vipPageShowing = MutableLiveData(false)
    val vipPageShowing: LiveData<Boolean> = _vipPageShowing

    /**
     * 键盘选择页，是否显示
     */
    private val _keyboardSelectPageShowing = MutableLiveData(false)
    val keyboardSelectPageShowing: LiveData<Boolean> = _keyboardSelectPageShowing

    /**
     * Ai生成内容页，是否显示
     */
    private val _aiChatPageShowing = MutableLiveData(false)
    val aiChatPageShowing: LiveData<Boolean> = _aiChatPageShowing

    /**
     * 帮助模式，帮聊、教你说、开场白等
     */
    private val _helpMode = MutableLiveData(HelpMode.HELP_CHAT)
    val helpMode: LiveData<HelpMode> = _helpMode

    /**
     * 全局键盘类型
     */
    private val _keyboardGlobalType = MutableLiveData(KeyboardGlobalType.AI_KEYBOARD)
    val keyboardGlobalType: LiveData<KeyboardGlobalType> = _keyboardGlobalType

    /**
     * 键盘名称
     */
    private val _keyboardName =
        MutableLiveData(ContextUtil.getContext().resources.getString(R.string.common_keyboard))
    val keyboardName: LiveData<String> = _keyboardName

    /**
     * 当前选中的开场白Tab
     */
    private val _currentPrologueTab = MutableLiveData<String>()
    val currentPrologueTab: LiveData<String> = _currentPrologueTab

    /**
     * 开场白列表
     */
    private val _prologueList =
        MutableLiveData<List<PrologueListItem>>()
    val prologueList: LiveData<List<PrologueListItem>> = _prologueList

    /**
     * 用户的剪切板数据
     */
    private val _userClipboardData = MutableLiveData("")
    val userClipboardData: LiveData<String> = _userClipboardData

    /**
     * 键盘列表数据
     */
    private val _keyboardList = MutableLiveData<List<KeyboardSelectModel>>()
    val keyboardList: LiveData<List<KeyboardSelectModel>> = _keyboardList

    // ----------------------------------- 更新状态方法 -----------------------------------

    /**
     * 更新，登录页，是否显示
     */
    fun updateLoginPageShowing(isShowing: Boolean) {
        _loginPageShowing.value = isShowing
    }

    /**
     * 更新，设置页，是否显示
     */
    fun updateSettingPageShowing(isShowing: Boolean) {
        _settingPageShowing.value = isShowing
    }

    /**
     * 更新，键盘选择页，是否显示
     */
    fun updateKeyboardSelectPageShowing(isShowing: Boolean) {
        _keyboardSelectPageShowing.value = isShowing
    }

    /**
     * 更新，VIP页，是否显示
     */
    fun updateVipPageShowing(isShowing: Boolean) {
        _vipPageShowing.value = isShowing
    }

    /**
     * 更新，Ai生成内容页，是否显示
     */
    fun updateAiChatPageShowing(isShowing: Boolean) {
        _aiChatPageShowing.value = isShowing
    }

    /**
     * 更新帮助模式
     */
    fun updateHelpMode(newMode: HelpMode) {
        _helpMode.value = newMode
    }

    /**
     * 更新全局键盘类型
     */
    fun updateKeyboardGlobalType(keyboardGlobalType: KeyboardGlobalType) {
        this._keyboardGlobalType.value = keyboardGlobalType
    }

    /**
     * 更新当前选中的开场白的Tab
     */
    fun updateCurrentPrologueTab(tab: String) {
        _currentPrologueTab.value = tab
    }

    /**
     * 更新，用户的剪切板数据
     */
    fun updateUserClipboardData(newText: String) {
        _userClipboardData.value = newText
    }

    // ----------------------------------- 获取业务数据方法 -----------------------------------

    /**
     * 用户是否已登录
     */
    fun isLogin(
        onSuccess: (resultObj: Boolean) -> Unit,
        onFail: (msg: String) -> Unit
    ) {
        mKeyboardRepository.isLogin(onSuccess, onFail)
    }

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

    /**
     * 获取键盘列表
     */
    fun getKeyboardList(onFail: (msg: String) -> Unit) {
        mKeyboardRepository.getKeyboardList(onSuccess = {
            _keyboardList.value = it.keyboardInfos
        }, onFail)
    }

    /**
     * 选择键盘
     *
     * @param selectModel 键盘选择信息
     */
    fun selectedKeyboard(
        selectModel: KeyboardSelectModel,
        onSuccess: (resultObj: EmptyResp) -> Unit,
        onFail: (msg: String) -> Unit
    ) {
        mKeyboardRepository.selectedKeyboard(selectModel.id, onSuccess = {
            // 更新键盘名称
            _keyboardName.value = selectModel.name
            onSuccess.invoke(it)
        }, onFail)
    }

    /**
     * 获取人设列表
     */
    fun getCharacterList(
        onSuccess: (CharacterListResp) -> Unit,
        onFail: (String) -> Unit
    ) {
        mKeyboardRepository.getCurrentKeyboardId(onSuccess = {
            val keyboardId = it.keyboardId ?: ""
            mKeyboardRepository.getCharacterList(keyboardId, onSuccess, onFail)
        }, onFail)
    }

    /**
     * 获取开场白列表
     */
    fun getPrologueList(onFail: (String) -> Unit) {
        mKeyboardRepository.getPrologueList(onSuccess = {
            _prologueList.value = it.prologues ?: listOf()
        }, onFail = onFail)
    }

    /**
     * 过滤出对应Tab的开场白列表
     *
     * @param tab 选择的Tab类型
     */
    fun filterPrologueListByTab(tab: String): List<Topic> {
        // 按照选择Tab，过滤出对应Tab的开场白数据
        val tabPrologues = _prologueList.value?.firstOrNull {
            it.title == tab
        }
        // 该Tab下的开场白列表
        return tabPrologues?.topics ?: listOf()
    }
}