"""
天气和季节系统插件
为EG引擎提供完整的天气和季节变化系统
"""

from .core.season_manager import SeasonManager
from .core.weather_manager import WeatherManager
from .effects.environment_effects import EnvironmentEffects
from .effects.visual_effects import VisualEffects
from .audio.audio_manager import AudioManager
from .weather.weather_system import WeatherSystem
from .seasons.season_system import SeasonSystem
from .particles.particle_effects import ParticleEffects
from .editor.weather_editor import WeatherEditor
from .editor.preset_manager import PresetManager
from .utils.weather_utils import WeatherUtils

class WeatherSeasonSystemPlugin:
    """
    天气和季节系统插件主类
    整合所有天气和季节功能模块
    """
    
    def __init__(self, engine):
        """
        初始化天气和季节系统插件
        
        Args:
            engine: EG引擎实例
        """
        self.engine = engine
        self.name = "WeatherSeasonSystem"
        self.version = "1.0.0"
        self.author = "EG Plugin System"
        
        # 功能模块
        self.season_manager = None
        self.weather_manager = None
        self.environment_effects = None
        self.visual_effects = None
        self.audio_manager = None
        self.weather_system = None
        self.season_system = None
        self.particle_effects = None
        self.weather_editor = None
        self.preset_manager = None
        self.weather_utils = None
        
        # 插件状态
        self.enabled = False
        self.initialized = False
        
        # 性能统计
        self.stats = {
            'weather_changes': 0,
            'season_changes': 0,
            'effects_updated': 0,
            'particles_generated': 0
        }
        
        print(f"✓ 天气和季节系统插件 v{self.version} 已创建")
        
    def initialize(self) -> bool:
        """
        初始化插件
        
        Returns:
            是否初始化成功
        """
        try:
            print("正在初始化天气和季节系统插件...")
            
            # 初始化核心组件
            self.season_manager = SeasonManager(self)
            self.weather_manager = WeatherManager(self)
            self.environment_effects = EnvironmentEffects(self)
            self.visual_effects = VisualEffects(self)
            self.audio_manager = AudioManager(self)
            self.weather_system = WeatherSystem(self)
            self.season_system = SeasonSystem(self)
            self.particle_effects = ParticleEffects(self)
            self.weather_utils = WeatherUtils(self)
            
            # 初始化新增的编辑器组件
            from .editor.weather_editor import WeatherEditor
            from .editor.preset_manager import PresetManager
            
            self.weather_editor = WeatherEditor(self)
            self.preset_manager = PresetManager(self)
            
            # 初始化各个模块
            modules = [
                self.season_manager,
                self.weather_manager,
                self.environment_effects,
                self.visual_effects,
                self.audio_manager,
                self.weather_system,
                self.season_system,
                self.particle_effects,
                self.weather_utils,
                self.weather_editor,
                self.preset_manager
            ]
            
            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.season_manager,
                self.weather_manager,
                self.environment_effects,
                self.visual_effects,
                self.audio_manager,
                self.weather_system,
                self.season_system,
                self.particle_effects,
                self.weather_utils,
                self.weather_editor,
                self.preset_manager
            ]
            
            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.season_manager,
                self.weather_manager,
                self.environment_effects,
                self.visual_effects,
                self.audio_manager,
                self.weather_system,
                self.season_system,
                self.particle_effects,
                self.weather_utils,
                self.weather_editor,
                self.preset_manager
            ]
            
            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.season_manager,
                self.weather_manager,
                self.environment_effects,
                self.visual_effects,
                self.audio_manager,
                self.weather_system,
                self.season_system,
                self.particle_effects,
                self.weather_utils,
                self.weather_editor,
                self.preset_manager
            ]
            
            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.season_manager,
                self.weather_manager,
                self.environment_effects,
                self.visual_effects,
                self.audio_manager,
                self.weather_system,
                self.season_system,
                self.particle_effects,
                self.weather_utils,
                self.weather_editor,
                self.preset_manager
            ]
            
            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 = {
            'weather_changes': 0,
            'season_changes': 0,
            'effects_updated': 0,
            'particles_generated': 0
        }
        print("✓ 天气和季节系统统计信息已重置")