"""
地形编辑器核心类
"""

from plugins.plugin_manager import BasePlugin

class TerrainEditorPlugin(BasePlugin):
    """
    地形编辑器插件核心类
    """
    
    def __init__(self, plugin_manager, name):
        super().__init__(plugin_manager, name)
        self.config = {
            "version": "1.0.0",
            "author": "EG Team",
            "description": "地形编辑器插件，提供完整的地形创建和编辑功能"
        }
        self.terrain_editor_ui = None
        self.terrain_tools = None
        self.terrain_materials = None
        self.terrain_io = None
        self.terrain_optimization = None
        
    def initialize(self) -> bool:
        """
        初始化地形编辑器插件
        """
        print(f"初始化地形编辑器插件: {self.name}")
        return True
        
    def enable(self) -> bool:
        """
        启用地形编辑器插件
        """
        if not super().enable():
            return False
            
        print(f"启用地形编辑器插件: {self.name}")
        
        # 初始化各个模块
        self._init_modules()
        
        # 创建UI界面
        self._create_ui()
        
        # 注册事件处理器
        self._register_events()
        
        return True
        
    def disable(self) -> bool:
        """
        禁用地形编辑器插件
        """
        if not super().disable():
            return False
            
        print(f"禁用地形编辑器插件: {self.name}")
        
        # 清理UI界面
        self._cleanup_ui()
        
        # 注销事件处理器
        self._unregister_events()
        
        return True
        
    def finalize(self):
        """
        清理地形编辑器插件资源
        """
        print(f"清理地形编辑器插件资源: {self.name}")
        
    def _init_modules(self):
        """
        初始化各个功能模块
        """
        try:
            # 动态导入各个模块
            from .ui.terrain_editor_ui import TerrainEditorUI
            from .tools.terrain_tools import TerrainTools
            from .materials.terrain_materials import TerrainMaterials
            from .io.terrain_io import TerrainIO
            from .optimization.terrain_optimization import TerrainOptimization
            
            # 创建模块实例
            self.terrain_editor_ui = TerrainEditorUI(self.plugin_manager.world)
            self.terrain_tools = TerrainTools(self.plugin_manager.world)
            self.terrain_materials = TerrainMaterials(self.plugin_manager.world)
            self.terrain_io = TerrainIO(self.plugin_manager.world)
            self.terrain_optimization = TerrainOptimization(self.plugin_manager.world)
            
            print("✓ 地形编辑器各功能模块初始化成功")
        except Exception as e:
            print(f"❌ 地形编辑器模块初始化失败: {e}")
            
    def _create_ui(self):
        """
        创建UI界面
        """
        if self.terrain_editor_ui:
            self.terrain_editor_ui.create_interface()
            
    def _cleanup_ui(self):
        """
        清理UI界面
        """
        if self.terrain_editor_ui:
            self.terrain_editor_ui.cleanup()
            
    def _register_events(self):
        """
        注册事件处理器
        """
        world = self.plugin_manager.world
        # 注册快捷键
        world.accept("f6", self._toggle_terrain_editor)
        
    def _unregister_events(self):
        """
        注销事件处理器
        """
        world = self.plugin_manager.world
        # 注销快捷键
        world.ignore("f6")
        
    def _toggle_terrain_editor(self):
        """
        切换地形编辑器显示/隐藏
        """
        if self.terrain_editor_ui:
            self.terrain_editor_ui.toggle_visibility()