"""
匹配系统插件主文件
"""

class MatchmakingSystemPlugin:
    """
    匹配系统插件
    为EG引擎提供玩家匹配功能
    """
    
    def __init__(self, engine):
        """
        初始化匹配系统插件
        
        Args:
            engine: EG引擎实例
        """
        self.engine = engine
        self.name = "MatchmakingSystem"
        self.version = "1.0.0"
        
        # 插件组件
        self.match_manager = None
        self.queue_manager = None
        self.algorithm_manager = None
        self.room_allocator = None
        self.rule_manager = None
        self.monitor = None
        self.event_handler = None
        self.config_manager = None
        self.editor = None
        self.stats_manager = None
        
        print("✓ 匹配系统插件已创建")
        
    def initialize(self) -> bool:
        """
        初始化插件
        
        Returns:
            是否初始化成功
        """
        try:
            print("正在初始化匹配系统插件...")
            
            # 导入模块
            from .core.match_manager import MatchManager
            from .queue.queue_manager import QueueManager
            from .algorithms.algorithm_manager import AlgorithmManager
            from .rooms.room_allocator import RoomAllocator
            from .rules.rule_manager import RuleManager
            from .monitoring.match_monitor import MatchMonitor
            from .events.event_handler import EventHandler
            from .config.match_config import MatchConfig
            from .editor.match_editor import MatchEditor
            from .stats.stats_manager import StatsManager
            
            # 创建组件
            self.config_manager = MatchConfig(self)
            self.event_handler = EventHandler(self)
            self.rule_manager = RuleManager(self)
            self.algorithm_manager = AlgorithmManager(self)
            self.queue_manager = QueueManager(self)
            self.room_allocator = RoomAllocator(self)
            self.match_manager = MatchManager(self)
            self.monitor = MatchMonitor(self)
            self.editor = MatchEditor(self)
            self.stats_manager = StatsManager(self)
            
            # 初始化组件
            components = [
                self.config_manager,
                self.event_handler,
                self.rule_manager,
                self.algorithm_manager,
                self.queue_manager,
                self.room_allocator,
                self.match_manager,
                self.monitor,
                self.editor,
                self.stats_manager
            ]
            
            for component in components:
                if hasattr(component, 'initialize') and not component.initialize():
                    print(f"✗ 组件初始化失败: {component.__class__.__name__}")
                    return False
                    
            print("✓ 匹配系统插件初始化完成")
            return True
            
        except Exception as e:
            print(f"✗ 匹配系统插件初始化失败: {e}")
            import traceback
            traceback.print_exc()
            return False
            
    def enable(self) -> bool:
        """
        启用插件
        
        Returns:
            是否启用成功
        """
        try:
            print("正在启用匹配系统插件...")
            
            # 启用组件
            components = [
                self.config_manager,
                self.event_handler,
                self.rule_manager,
                self.algorithm_manager,
                self.queue_manager,
                self.room_allocator,
                self.match_manager,
                self.monitor,
                self.editor,
                self.stats_manager
            ]
            
            for component in components:
                if hasattr(component, 'enable') and not component.enable():
                    print(f"✗ 组件启用失败: {component.__class__.__name__}")
                    return False
                    
            print("✓ 匹配系统插件已启用")
            return True
            
        except Exception as e:
            print(f"✗ 匹配系统插件启用失败: {e}")
            import traceback
            traceback.print_exc()
            return False
            
    def disable(self):
        """禁用插件"""
        try:
            print("正在禁用匹配系统插件...")
            
            # 禁用组件
            components = [
                self.stats_manager,
                self.editor,
                self.monitor,
                self.match_manager,
                self.room_allocator,
                self.queue_manager,
                self.algorithm_manager,
                self.rule_manager,
                self.event_handler,
                self.config_manager
            ]
            
            for component in components:
                if hasattr(component, 'disable'):
                    component.disable()
                    
            print("✓ 匹配系统插件已禁用")
            
        except Exception as e:
            print(f"✗ 匹配系统插件禁用失败: {e}")
            import traceback
            traceback.print_exc()
            
    def finalize(self):
        """清理插件资源"""
        try:
            print("正在清理匹配系统插件资源...")
            
            # 清理组件
            components = [
                self.stats_manager,
                self.editor,
                self.monitor,
                self.match_manager,
                self.room_allocator,
                self.queue_manager,
                self.algorithm_manager,
                self.rule_manager,
                self.event_handler,
                self.config_manager
            ]
            
            for component in components:
                if hasattr(component, 'finalize'):
                    component.finalize()
                    
            print("✓ 匹配系统插件资源已清理")
            
        except Exception as e:
            print(f"✗ 匹配系统插件资源清理失败: {e}")
            import traceback
            traceback.print_exc()
            
    def update(self, dt: float):
        """
        更新插件状态
        
        Args:
            dt: 时间增量（秒）
        """
        try:
            # 更新组件
            components = [
                self.config_manager,
                self.event_handler,
                self.rule_manager,
                self.algorithm_manager,
                self.queue_manager,
                self.room_allocator,
                self.match_manager,
                self.monitor,
                self.editor,
                self.stats_manager
            ]
            
            for component in components:
                if hasattr(component, 'update'):
                    component.update(dt)
                    
        except Exception as e:
            print(f"✗ 匹配系统插件更新失败: {e}")
            import traceback
            traceback.print_exc()