#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
模块名称：__init__.py
模块描述：模拟器工厂，设备类型到模拟器类的映射

作者：czj
创建日期：2026-07-29
最后修改：2026-07-29
"""

from typing import Optional

from app.services.mqtt_manager import MqttManager
from app.simulators.base_simulator import BaseSimulator
from app.simulators.door_simulator import DoorSimulator
from app.simulators.paperless_simulator import PaperlessSimulator
from app.simulators.central_simulator import CentralSimulator
from app.simulators.client_simulator import ClientSimulator

# 设备类型到模拟器类的映射
SIMULATOR_MAP = {
    "door": DoorSimulator,
    "paperless": PaperlessSimulator,
    "central": CentralSimulator,
    "client": ClientSimulator,
}

# 各设备类型的默认上报间隔（秒）
DEFAULT_INTERVAL_MAP = {
    "door": 30,
    "paperless": 30,
    "central": 30,
    "client": 5,  # 集控客户端心跳频率更高
}


def create_simulator(device_type: str, device_id: str,
                     env_config_id: str, mqtt_manager: MqttManager,
                     report_config: Optional[dict] = None,
                     topics: Optional[dict] = None,
                     topic_params: Optional[dict] = None) -> Optional[BaseSimulator]:
    """
    模拟器工厂方法

    根据设备类型创建对应的模拟器实例。

    Args:
        device_type: 设备类型 (door/paperless/central/client)
        device_id: 设备 ID
        env_config_id: 环境配置 ID
        mqtt_manager: MQTT 管理器实例
        report_config: 上报配置，如 {'interval': 30, 'enabled': True}
        topics: 多主题前缀映射，key 为设备类型，value 为主题前缀
        topic_params: 主题动态参数，如 {'room_id': 'A101', 'company_id': '001'}

    Returns:
        BaseSimulator: 模拟器实例，如果设备类型不支持则返回 None
    """
    simulator_class = SIMULATOR_MAP.get(device_type)
    if not simulator_class:
        return None

    # 如果未提供 report_config，使用设备类型默认值
    if report_config is None:
        default_interval = DEFAULT_INTERVAL_MAP.get(device_type, 30)
        report_config = {"interval": default_interval, "enabled": True}

    return simulator_class(
        device_id=device_id,
        env_config_id=env_config_id,
        mqtt_manager=mqtt_manager,
        report_config=report_config,
        topics=topics or {},
        topic_params=topic_params,
    )


__all__ = [
    'BaseSimulator',
    'DoorSimulator',
    'PaperlessSimulator',
    'CentralSimulator',
    'ClientSimulator',
    'SIMULATOR_MAP',
    'DEFAULT_INTERVAL_MAP',
    'create_simulator',
]