"""
植被分布和生态模拟插件
为EG引擎提供完整的植被分布和生态系统模拟功能
"""

from .core.vegetation_manager import VegetationManager
from .core.ecosystem_manager import EcosystemManager
from .ecosystem.animal_simulator import AnimalSimulator
from .ecosystem.ecosystem_dynamics import EcosystemDynamics
from .ecosystem.eco_interactions import EcoInteractions
from .vegetation.vegetation_spawner import VegetationSpawner
from .vegetation.growth_simulator import GrowthSimulator
from .terrain.terrain_influence import TerrainInfluence
from .seasons.seasonal_effects import SeasonalEffects
from .visualization.eco_visualizer import EcoVisualizer
from .visualization.advanced_visuals import AdvancedVisuals
from .editor.eco_editor import EcoEditor
from .editor.preset_manager import PresetManager
from .utils.eco_utils import EcoUtils

class VegetationEcosystemPlugin:
    """
    植被分布和生态模拟插件主类
    整合所有植被和生态系统功能模块
    """
    
    def __init__(self, engine):
        """
        初始化植被分布和生态模拟插件
        
        Args:
            engine: EG引擎实例
        """
        self.engine = engine
        self.name = "VegetationEcosystem"
        self.version = "1.0.0"
        self.author = "EG Plugin System"
        
        # 功能模块
        self.vegetation_manager = None
        self.ecosystem_manager = None
        self.animal_simulator = None
        self.ecosystem_dynamics = None
        self.ecosystem_interactions = None
        self.vegetation_spawner = None
        self.growth_simulator = None
        self.terrain_influence = None
        self.seasonal_effects = None
        self.eco_visualizer = None
        self.advanced_visuals = None
        self.eco_editor = None
        self.preset_manager = None
        self.eco_utils = None
        
        # 插件状态
        self.enabled = False
        self.initialized = False
        
        # 性能统计
        self.stats = {
            'vegetation_spawned': 0,
            'ecosystem_updates': 0,
            'animals_simulated': 0,
            'growth_cycles': 0
        }
        
        print(f"✓ 植被分布和生态模拟插件 v{self.version} 已创建")
        
    def initialize(self) -> bool:
        """
        初始化插件
        
        Returns:
            是否初始化成功
        """
        try:
            print("正在初始化植被分布和生态模拟插件...")
            
            # 初始化核心组件
            self.vegetation_manager = VegetationManager(self)
            self.ecosystem_manager = EcosystemManager(self)
            self.animal_simulator = AnimalSimulator(self)
            self.ecosystem_dynamics = EcosystemDynamics(self)
            self.ecosystem_interactions = EcoInteractions(self)
            self.vegetation_spawner = VegetationSpawner(self)
            self.growth_simulator = GrowthSimulator(self)
            self.terrain_influence = TerrainInfluence(self)
            self.seasonal_effects = SeasonalEffects(self)
            self.eco_visualizer = EcoVisualizer(self)
            self.advanced_visuals = AdvancedVisuals(self)
            self.eco_editor = EcoEditor(self)
            self.preset_manager = PresetManager(self)
            self.eco_utils = EcoUtils(self)
            
            # 初始化各个模块
            modules = [
                self.vegetation_manager,
                self.ecosystem_manager,
                self.animal_simulator,
                self.ecosystem_dynamics,
                self.ecosystem_interactions,
                self.vegetation_spawner,
                self.growth_simulator,
                self.terrain_influence,
                self.seasonal_effects,
                self.eco_visualizer,
                self.advanced_visuals,
                self.eco_editor,
                self.preset_manager,
                self.eco_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.vegetation_manager,
                self.ecosystem_manager,
                self.animal_simulator,
                self.ecosystem_dynamics,
                self.ecosystem_interactions,
                self.vegetation_spawner,
                self.growth_simulator,
                self.terrain_influence,
                self.seasonal_effects,
                self.eco_visualizer,
                self.advanced_visuals,
                self.eco_editor,
                self.preset_manager,
                self.eco_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.vegetation_manager,
                self.ecosystem_manager,
                self.animal_simulator,
                self.ecosystem_dynamics,
                self.ecosystem_interactions,
                self.vegetation_spawner,
                self.growth_simulator,
                self.terrain_influence,
                self.seasonal_effects,
                self.eco_visualizer,
                self.advanced_visuals,
                self.eco_editor,
                self.preset_manager,
                self.eco_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.vegetation_manager,
                self.ecosystem_manager,
                self.animal_simulator,
                self.ecosystem_dynamics,
                self.ecosystem_interactions,
                self.vegetation_spawner,
                self.growth_simulator,
                self.terrain_influence,
                self.seasonal_effects,
                self.eco_visualizer,
                self.advanced_visuals,
                self.eco_editor,
                self.preset_manager,
                self.eco_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.vegetation_manager,
                self.ecosystem_manager,
                self.animal_simulator,
                self.ecosystem_dynamics,
                self.ecosystem_interactions,
                self.vegetation_spawner,
                self.growth_simulator,
                self.terrain_influence,
                self.seasonal_effects,
                self.eco_visualizer,
                self.advanced_visuals,
                self.eco_editor,
                self.preset_manager,
                self.eco_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 = {
            'vegetation_spawned': 0,
            'ecosystem_updates': 0,
            'animals_simulated': 0,
            'growth_cycles': 0
        }
        print("✓ 植被分布和生态模拟统计信息已重置")