"""
触觉反馈系统插件
为EG引擎提供高质量的触觉反馈支持
"""

from .core.haptic_manager import HapticManager
from .effects.effect_processor import EffectProcessor
from .devices.device_manager import HapticDeviceManager
from .physics.physics_integration import PhysicsIntegration
from .audio.audio_haptics import AudioHaptics
from .events.event_handler import HapticEventHandler
from .config.config_manager import HapticConfigManager
from .editor.haptic_editor import HapticEditor
from .utils.haptic_utils import HapticUtils

class HapticFeedbackSystemPlugin:
    """
    触觉反馈系统插件主类
    整合所有触觉反馈功能模块
    """
    
    def __init__(self, engine):
        """
        初始化触觉反馈系统插件
        
        Args:
            engine: EG引擎实例
        """
        self.engine = engine
        self.name = "HapticFeedbackSystem"
        self.version = "1.0.0"
        self.author = "EG Plugin System"
        
        # 功能模块
        self.haptic_manager = None
        self.effect_processor = None
        self.device_manager = None
        self.physics_integration = None
        self.audio_haptics = None
        self.event_handler = None
        self.config_manager = None
        self.haptic_editor = None
        self.haptic_utils = None
        
        # 插件状态
        self.enabled = False
        self.initialized = False
        
        # 性能统计
        self.stats = {
            'effects_played': 0,
            'devices_connected': 0,
            'events_handled': 0,
            'audio_haptics_generated': 0,
            'physics_haptics_generated': 0
        }
        
        print(f"✓ 触觉反馈系统插件 v{self.version} 已创建")
        
    def initialize(self) -> bool:
        """
        初始化插件
        
        Returns:
            是否初始化成功
        """
        try:
            print("正在初始化触觉反馈系统插件...")
            
            # 初始化核心组件
            self.haptic_manager = HapticManager(self)
            self.effect_processor = EffectProcessor(self)
            self.device_manager = HapticDeviceManager(self)
            self.physics_integration = PhysicsIntegration(self)
            self.audio_haptics = AudioHaptics(self)
            self.event_handler = HapticEventHandler(self)
            self.config_manager = HapticConfigManager(self)
            self.haptic_editor = HapticEditor(self)
            self.haptic_utils = HapticUtils(self)
            
            # 初始化各个模块
            modules = [
                self.haptic_manager,
                self.effect_processor,
                self.device_manager,
                self.physics_integration,
                self.audio_haptics,
                self.event_handler,
                self.config_manager,
                self.haptic_editor,
                self.haptic_utils
            ]
            
            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.haptic_manager,
                self.effect_processor,
                self.device_manager,
                self.physics_integration,
                self.audio_haptics,
                self.event_handler,
                self.config_manager,
                self.haptic_editor,
                self.haptic_utils
            ]
            
            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.haptic_manager,
                self.effect_processor,
                self.device_manager,
                self.physics_integration,
                self.audio_haptics,
                self.event_handler,
                self.config_manager,
                self.haptic_editor,
                self.haptic_utils
            ]
            
            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.haptic_manager,
                self.effect_processor,
                self.device_manager,
                self.physics_integration,
                self.audio_haptics,
                self.event_handler,
                self.config_manager,
                self.haptic_editor,
                self.haptic_utils
            ]
            
            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.haptic_manager,
                self.effect_processor,
                self.device_manager,
                self.physics_integration,
                self.audio_haptics,
                self.event_handler,
                self.config_manager,
                self.haptic_editor,
                self.haptic_utils
            ]
            
            for module in modules:
                if module:
                    module.update(dt)
                    
        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 = {
            'effects_played': 0,
            'devices_connected': 0,
            'events_handled': 0,
            'audio_haptics_generated': 0,
            'physics_haptics_generated': 0
        }
        print("✓ 触觉反馈系统统计信息已重置")