from PyQt5.QtCore import QSettings
from DatabaseManager import DatabaseManager

# 创建全局的配置字典
config = {}
# 本地命令集合
local_commands = []


def load_config():
    print('load_config 被调用了-------------')

    global local_commands  # 使用 global 关键字声明 local_commands 是全局变量
    # 加载本地命令集合
    db = DatabaseManager()
    local_commands = db.fetch_all_commands()
    db.__del__()

    # 创建配置对象
    settings = QSettings("config.ini", QSettings.Format.IniFormat)

    # 读取配置项并存储到config字典中
    config["DiPai_ASR_APPID"] = settings.value("DiPai/ASR_APPID")
    config["DiPai_ASR_APIKey"] = settings.value("DiPai/ASR_APIKey")
    config["DiPai_ASR_APISecret"] = settings.value("DiPai/ASR_APISecret")
    config["DiPai_ASR_URL"] = settings.value("DiPai/ASR_URL")

    config["DiPai_TTS_APPID"] = settings.value("DiPai/TTS_APPID")
    config["DiPai_TTS_APIKey"] = settings.value("DiPai/TTS_APIKey")
    config["DiPai_TTS_APISecret"] = settings.value("DiPai/TTS_APISecret")
    config["DiPai_TTS_URL"] = settings.value("DiPai/TTS_URL")
    config["DiPai_NLP_URL"] = settings.value("DiPai/NLP_URL")

    config["AIUI_APPID"] = settings.value("AIUI/APPID")
    config["AIUI_APIKey"] = settings.value("AIUI/APIKey")

    config["SCKJ_APPID"] = settings.value("SCKJ/APPID")
    config["SCKJ_APIKey"] = settings.value("SCKJ/APIKey")
    config["SCKJ_ASR_URL"] = settings.value("SCKJ/ASR_URL")
    config["SCKJ_TTS_URL"] = settings.value("SCKJ/TTS_URL")
    config["SCKJ_NLP_URL"] = settings.value("SCKJ/NLP_URL")

    config["MODE_TYPE"] = settings.value("APP/MODE_TYPE")
    config["ENGINE_TYPE"] = settings.value("APP/ENGINE_TYPE")
    config["ASR_TXT_SHOW"] = settings.value("APP/ASR_TXT_SHOW")
    config["ASR_TXT_COLOR"] = settings.value("APP/ASR_TXT_COLOR")
    config["NOTIFY_URL"] = settings.value("APP/NOTIFY_URL")


def save_config(key, value):
    # 创建配置对象
    settings = QSettings("config.ini", QSettings.Format.IniFormat)

    # 更新内存中的配置
    config[key] = value

    # 将config字典中的值保存到配置文件中
    settings.setValue(key, value)


# 在模块导入时自动调用load_config函数
load_config()
