"""
多平台输入支持插件
为EG引擎提供统一的多平台输入设备支持
"""

from .core.input_manager import InputManager
from .core.advanced_input_processor import AdvancedInputProcessor
from .devices.device_manager import DeviceManager
from .devices.device_calibrator import DeviceCalibrator
from .events.event_system import EventSystem
from .mapping.input_mapper import InputMapper
from .gestures.gesture_recognizer import GestureRecognizer
from .haptics.haptic_manager import HapticManager
from .config.config_manager import ConfigManager
from .editor.input_editor import InputEditor
from .utils.input_utils import InputUtils
from .platforms.platform_adapter import PlatformAdapter

class MultiPlatformInputPlugin:
    """
    多平台输入支持插件主类
    整合所有输入设备支持功能模块
    """
    
    def __init__(self, engine):
        """
        初始化多平台输入支持插件
        
        Args:
            engine: EG引擎实例
        """
        self.engine = engine
        self.name = "MultiPlatformInput"
        self.version = "1.0.0"
        self.author = "EG Plugin System"
        
        # 功能模块
        self.input_manager = None
        self.advanced_input_processor = None
        self.device_manager = None
        self.device_calibrator = None
        self.event_system = None
        self.input_mapper = None
        self.gesture_recognizer = None
        self.haptic_manager = None
        self.config_manager = None
        self.input_editor = None
        self.input_utils = None
        self.platform_adapter = None
        
        # 插件状态
        self.enabled = False
        self.initialized = False
        
        # 性能统计
        self.stats = {
            'devices_connected': 0,
            'events_processed': 0,
            'gestures_recognized': 0,
            'haptic_effects': 0,
            'input_mappings': 0
        }
        
        print(f"✓ 多平台输入支持插件 v{self.version} 已创建")
        
    def initialize(self) -> bool:
        """
        初始化插件
        
        Returns:
            是否初始化成功
        """
        try:
            print("正在初始化多平台输入支持插件...")
            
            # 初始化核心组件
            self.input_manager = InputManager(self)
            self.advanced_input_processor = AdvancedInputProcessor(self)
            self.device_manager = DeviceManager(self)
            self.device_calibrator = DeviceCalibrator(self)
            self.event_system = EventSystem(self)
            self.input_mapper = InputMapper(self)
            self.gesture_recognizer = GestureRecognizer(self)
            self.haptic_manager = HapticManager(self)
            self.config_manager = ConfigManager(self)
            self.input_editor = InputEditor(self)
            self.input_utils = InputUtils(self)
            self.platform_adapter = PlatformAdapter(self)
            
            # 初始化各个模块
            modules = [
                self.input_manager,
                self.advanced_input_processor,
                self.device_manager,
                self.device_calibrator,
                self.event_system,
                self.input_mapper,
                self.gesture_recognizer,
                self.haptic_manager,
                self.config_manager,
                self.input_editor,
                self.input_utils,
                self.platform_adapter
            ]
            
            for module in modules:
                if module and not module.initialize():
                    print(f"✗ 模块初始化失败: {module.__class__.__name__}")
                    return False
                    
            self.initialized = True
            print("✓ 多平台输入支持插件初始化完成")
            return True
            
        except Exception as e:
            print(f"✗ 多平台输入支持插件初始化失败: {e}")
            import traceback
            traceback.print_exc()
            return False
            
    def enable(self) -> bool:
        """
        启用插件
        
        Returns:
            是否启用成功
        """
        try:
            if not self.initialized:
                print("✗ 插件未初始化")
                return False
                
            print("正在启用多平台输入支持插件...")
            
            # 启用各个模块
            modules = [
                self.input_manager,
                self.advanced_input_processor,
                self.device_manager,
                self.device_calibrator,
                self.event_system,
                self.input_mapper,
                self.gesture_recognizer,
                self.haptic_manager,
                self.config_manager,
                self.input_editor,
                self.input_utils,
                self.platform_adapter
            ]
            
            for module in modules:
                if module and not module.enable():
                    print(f"✗ 模块启用失败: {module.__class__.__name__}")
                    return False
                    
            self.enabled = True
            print("✓ 多平台输入支持插件已启用")
            return True
            
        except Exception as e:
            print(f"✗ 多平台输入支持插件启用失败: {e}")
            import traceback
            traceback.print_exc()
            return False
            
    def disable(self):
        """禁用插件"""
        try:
            if not self.enabled:
                return
                
            print("正在禁用多平台输入支持插件...")
            
            # 禁用各个模块
            modules = [
                self.input_manager,
                self.advanced_input_processor,
                self.device_manager,
                self.device_calibrator,
                self.event_system,
                self.input_mapper,
                self.gesture_recognizer,
                self.haptic_manager,
                self.config_manager,
                self.input_editor,
                self.input_utils,
                self.platform_adapter
            ]
            
            for module in modules:
                if module:
                    module.disable()
                    
            self.enabled = False
            print("✓ 多平台输入支持插件已禁用")
            
        except Exception as e:
            print(f"✗ 多平台输入支持插件禁用失败: {e}")
            import traceback
            traceback.print_exc()
            
    def finalize(self):
        """清理插件资源"""
        try:
            print("正在清理多平台输入支持插件资源...")
            
            # 清理各个模块
            modules = [
                self.input_manager,
                self.advanced_input_processor,
                self.device_manager,
                self.device_calibrator,
                self.event_system,
                self.input_mapper,
                self.gesture_recognizer,
                self.haptic_manager,
                self.config_manager,
                self.input_editor,
                self.input_utils,
                self.platform_adapter
            ]
            
            for module in modules:
                if module:
                    module.finalize()
                    
            self.initialized = False
            print("✓ 多平台输入支持插件资源已清理")
            
        except Exception as e:
            print(f"✗ 多平台输入支持插件资源清理失败: {e}")
            import traceback
            traceback.print_exc()
            
    def update(self, dt: float):
        """
        更新插件状态
        
        Args:
            dt: 时间增量（秒）
        """
        try:
            if not self.enabled:
                return
                
            # 更新各个模块
            modules = [
                self.input_manager,
                self.advanced_input_processor,
                self.device_manager,
                self.device_calibrator,
                self.event_system,
                self.input_mapper,
                self.gesture_recognizer,
                self.haptic_manager,
                self.config_manager,
                self.input_editor,
                self.input_utils,
                self.platform_adapter
            ]
            
            for module in modules:
                if module:
                    module.update(dt)
                    
            # 更新统计信息
            self.stats['events_processed'] += 1
                    
        except Exception as e:
            print(f"✗ 多平台输入支持插件更新失败: {e}")
            import traceback
            traceback.print_exc()
            
    def get_stats(self) -> dict:
        """
        获取插件统计信息
        
        Returns:
            统计信息字典
        """
        return self.stats.copy()
        
    def reset_stats(self):
        """重置统计信息"""
        self.stats = {
            'devices_connected': 0,
            'events_processed': 0,
            'gestures_recognized': 0,
            'haptic_effects': 0,
            'input_mappings': 0
        }
        print("✓ 多平台输入支持统计信息已重置")