import yaml
from loguru import logger

# 清除所有已有的日志记录器配置
logger.remove()

logger.add("./resources/log/core.log", 
           format="{time:YYYY-MM-DD HH:mm:ss} - {level} - {name}:{function}:{line} - {message}", 
           rotation="100 MB", 
           compression="zip", 
           encoding="utf-8")
# shell终端打印日志
logger.add(lambda msg: print(msg), 
           format="{time:YYYY-MM-DD HH:mm:ss} - {level} - {name}:{function}:{line} - {message}")

def get_core_config():
    # 加载参数
    with open('./config/config.yaml', 'r', encoding="utf-8") as file:
        config = yaml.safe_load(file)
    debug = config.get('debug', 'False')  # 使用 get 方法提供默认值
    if str(debug).lower() == "true":  # 统一转换为小写进行比较
        logger.debug("Debug mode is on")
        logger.debug(f"Loaded config: {config}")  # 输出加载的配置

    time_choice = int(f"{config['time_mode']}")
    choice = config['mode']  # 假设 mode 是一个列表
    e_hour = int(config.get('e_hour', '4'))  # 默认循环时间为4小时

    fs_activate = f"{config['fs_activate']}"
    if fs_activate == "True":
        fs_key = config.get('fs_key')
        fs_secret = config.get('fs_secret')
        if not fs_key or not fs_secret:
            logger.error("飞书相关配置不能为空，请检查配置文件./config/config.yaml")
            exit(5)

    wx_activate = f"{config['wx_activate']}"
    if wx_activate == "True":
        wx_key = config.get('wx_key')
        if not wx_key:
            logger.error("企业微信相关配置不能为空，请检查配置文件./config/config.yaml")
            exit(5)

    ding_activate = f"{config['ding_activate']}"
    if ding_activate == "True":
        ding_key = config.get('ding_key')
        if not ding_key:
            logger.error("钉钉相关配置不能为空，请检查配置文件./config/config.yaml")
            exit(5)

    lx_activate = f"{config['lx_activate']}"
    if lx_activate == "True":
        lx_key = config.get('lx_key')
        if not lx_key:
            logger.error("蓝信相关配置不能为空，请检查配置文件./config/config.yaml")
            exit(5)

    url_web = f"{config['url']}"

    return e_hour, time_choice, choice, fs_activate, wx_activate, ding_activate, lx_activate, url_web
    
def get_debug_config():
    with open('./config/config.yaml', 'r', encoding="utf-8") as file:
        config = yaml.safe_load(file)
    debug = f"{config['debug']}"

    return debug

def get_keywords_config(item):
    with open('./config/keywords.yaml', 'r', encoding="utf-8") as file:
        config = yaml.safe_load(file)
    if item == 'Doonsec':
        Doonsec_switch = config.get('Doonsec-switch', False)
        Doonsec = config['Doonsec']
        return Doonsec_switch, Doonsec
    elif item == 'Sogou-WX':
        Sogou_WX = config['Sogou-WX']
        return Sogou_WX
    elif item == 'Baidu':
        Baidu = config['Baidu']
        return Baidu
    elif item == 'Unity':
        uni_switch = config.get('uni-switch', False)
        Unity = config['Unity']
        return uni_switch, Unity
    else:
        logger.error("参数错误，请检查./config/keywords.yaml")
    