
import os
import json

import toml

USER_AGENTS_PATH = 'settings/lakes/user_agents.txt'
PROXIES_PATH = 'settings/lakes/proxies.txt'
USED_URLS_DIR_PATH = 'history'

BASIC_SETTING_FILE = 'settings/basic_setting.toml'

BASIC_SETTING_DEFAULT_CONFIG = {
    'save_dir': 'download',
    'type_name': 'food',
}


def load_config(filepath, default):
    """
    加载配置文件，如果文件不存在或解析失败则使用默认配置

    :param filepath: TOML 文件路径
    :param default: 默认配置字典
    :return: 加载的配置字典
    """
    # 尝试读取现有的 TOML 文件
    try:
        if os.path.exists(filepath):
            with open(filepath, 'r', encoding='utf-8') as f:
                config = toml.load(f)
        else:
            config = default
    except (FileNotFoundError, toml.TomlDecodeError):
        config = default

    # 检查是否缺少必要的参数，如果缺少则更新为默认值
    for key, value in default.items():
        if key not in config:
            config[key] = value

    return config


def save_config(file_path, config_dict):
    """
    将配置字典保存到 TOML 文件中

    :param file_path: TOML 文件路径
    :param config_dict: 配置字典
    """
    with open(file_path, 'w', encoding='utf-8') as f:
        toml.dump(config_dict, f)


def load_json(json_path):
    """
    加载 JSON 文件并返回其内容

    :param json_path: JSON 文件的路径
    :return: JSON 文件的内容（字典或列表），如果加载失败则返回 None
    """
    try:
        # 尝试打开并读取 JSON 文件
        with open(json_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
        return data
    except FileNotFoundError:
        print(f"错误：文件未找到 - {json_path}")
    except json.JSONDecodeError:
        print(f"错误：文件格式无效（非标准 JSON） - {json_path}")
    except PermissionError:
        print(f"错误：没有权限读取文件 - {json_path}")
    except Exception as e:
        print(f"错误：加载 JSON 文件时发生未知错误 - {e}")
    return None
