"""
水体和流体模拟插件
为EG引擎提供完整的水体和流体模拟功能
"""

from .core.fluid_manager import FluidManager
from .core.water_manager import WaterManager
from .fluids.fluid_dynamics import FluidDynamics
from .fluids.fluid_solver import FluidSolver
from .water.water_surface import WaterSurface
from .water.wave_system import WaveSystem
from .rendering.fluid_renderer import FluidRenderer
from .rendering.water_renderer import WaterRenderer
from .effects.fluid_effects import FluidEffects
from .effects.water_effects import WaterEffects
from .interaction.fluid_interaction import FluidInteraction
from .interaction.object_interaction import ObjectInteraction
from .audio.fluid_audio import FluidAudio
from .particles.fluid_particles import FluidParticles
from .editor.fluid_editor import FluidEditor
from .editor.preset_manager import PresetManager
from .utils.fluid_utils import FluidUtils
from .physics.fluid_physics import FluidPhysics

class FluidSimulationPlugin:
    """
    水体和流体模拟插件主类
    整合所有水体和流体模拟功能模块
    """
    
    def __init__(self, engine):
        """
        初始化水体和流体模拟插件
        
        Args:
            engine: EG引擎实例
        """
        self.engine = engine
        self.name = "FluidSimulation"
        self.version = "1.0.0"
        self.author = "EG Plugin System"
        
        # 功能模块
        self.fluid_manager = None
        self.water_manager = None
        self.fluid_dynamics = None
        self.fluid_solver = None
        self.water_surface = None
        self.wave_system = None
        self.fluid_renderer = None
        self.water_renderer = None
        self.fluid_effects = None
        self.water_effects = None
        self.fluid_interaction = None
        self.object_interaction = None
        self.fluid_audio = None
        self.fluid_particles = None
        self.fluid_editor = None
        self.preset_manager = None
        self.fluid_utils = None
        self.fluid_physics = None
        
        # 插件状态
        self.enabled = False
        self.initialized = False
        
        # 性能统计
        self.stats = {
            'fluid_cells': 0,
            'water_vertices': 0,
            'simulation_updates': 0,
            'render_calls': 0,
            'particles_spawned': 0
        }
        
        print(f"✓ 水体和流体模拟插件 v{self.version} 已创建")
        
    def initialize(self) -> bool:
        """
        初始化插件
        
        Returns:
            是否初始化成功
        """
        try:
            print("正在初始化水体和流体模拟插件...")
            
            # 初始化核心组件
            self.fluid_manager = FluidManager(self)
            self.water_manager = WaterManager(self)
            self.fluid_dynamics = FluidDynamics(self)
            self.fluid_solver = FluidSolver(self)
            self.water_surface = WaterSurface(self)
            self.wave_system = WaveSystem(self)
            self.fluid_renderer = FluidRenderer(self)
            self.water_renderer = WaterRenderer(self)
            self.fluid_effects = FluidEffects(self)
            self.water_effects = WaterEffects(self)
            self.fluid_interaction = FluidInteraction(self)
            self.object_interaction = ObjectInteraction(self)
            self.fluid_audio = FluidAudio(self)
            self.fluid_particles = FluidParticles(self)
            self.fluid_editor = FluidEditor(self)
            self.preset_manager = PresetManager(self)
            self.fluid_utils = FluidUtils(self)
            self.fluid_physics = FluidPhysics(self)
            
            # 初始化各个模块
            modules = [
                self.fluid_manager,
                self.water_manager,
                self.fluid_dynamics,
                self.fluid_solver,
                self.water_surface,
                self.wave_system,
                self.fluid_renderer,
                self.water_renderer,
                self.fluid_effects,
                self.water_effects,
                self.fluid_interaction,
                self.object_interaction,
                self.fluid_audio,
                self.fluid_particles,
                self.fluid_editor,
                self.preset_manager,
                self.fluid_utils,
                self.fluid_physics
            ]
            
            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.fluid_manager,
                self.water_manager,
                self.fluid_dynamics,
                self.fluid_solver,
                self.water_surface,
                self.wave_system,
                self.fluid_renderer,
                self.water_renderer,
                self.fluid_effects,
                self.water_effects,
                self.fluid_interaction,
                self.object_interaction,
                self.fluid_audio,
                self.fluid_particles,
                self.fluid_editor,
                self.preset_manager,
                self.fluid_utils,
                self.fluid_physics
            ]
            
            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.fluid_manager,
                self.water_manager,
                self.fluid_dynamics,
                self.fluid_solver,
                self.water_surface,
                self.wave_system,
                self.fluid_renderer,
                self.water_renderer,
                self.fluid_effects,
                self.water_effects,
                self.fluid_interaction,
                self.object_interaction,
                self.fluid_audio,
                self.fluid_particles,
                self.fluid_editor,
                self.preset_manager,
                self.fluid_utils,
                self.fluid_physics
            ]
            
            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.fluid_manager,
                self.water_manager,
                self.fluid_dynamics,
                self.fluid_solver,
                self.water_surface,
                self.wave_system,
                self.fluid_renderer,
                self.water_renderer,
                self.fluid_effects,
                self.water_effects,
                self.fluid_interaction,
                self.object_interaction,
                self.fluid_audio,
                self.fluid_particles,
                self.fluid_editor,
                self.preset_manager,
                self.fluid_utils,
                self.fluid_physics
            ]
            
            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.fluid_manager,
                self.water_manager,
                self.fluid_dynamics,
                self.fluid_solver,
                self.water_surface,
                self.wave_system,
                self.fluid_renderer,
                self.water_renderer,
                self.fluid_effects,
                self.water_effects,
                self.fluid_interaction,
                self.object_interaction,
                self.fluid_audio,
                self.fluid_particles,
                self.fluid_editor,
                self.preset_manager,
                self.fluid_utils,
                self.fluid_physics
            ]
            
            for module in modules:
                if module:
                    module.update(dt)
                    
            # 更新统计信息
            self.stats['simulation_updates'] += 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 = {
            'fluid_cells': 0,
            'water_vertices': 0,
            'simulation_updates': 0,
            'render_calls': 0,
            'particles_spawned': 0
        }
        print("✓ 水体和流体模拟统计信息已重置")