#!/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_interval: Optional[int] = None,
                     topics: Optional[dict] = None) -> Optional[BaseSimulator]:
    """
    模拟器工厂方法

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

    Args:
        device_type: 设备类型 (door/paperless/central/client)
        device_id: 设备 ID
        env_config_id: 环境配置 ID
        mqtt_manager: MQTT 管理器实例
        report_interval: 上报间隔（秒），None 使用默认值
        topics: 多主题前缀映射，key 为设备类型，value 为主题前缀

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

    if report_interval is None:
        report_interval = DEFAULT_INTERVAL_MAP.get(device_type, 30)

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


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