"""
地形编辑器UI界面
"""

from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, 
                            QPushButton, QSlider, QLabel, QComboBox, QGroupBox,
                            QTabWidget, QScrollArea, QFrame)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QDoubleValidator

class TerrainEditorUI:
    """
    地形编辑器UI界面类
    """
    
    def __init__(self, world):
        self.world = world
        self.main_widget = None
        self.terrain_tools_panel = None
        self.terrain_properties_panel = None
        self.material_panel = None
        
    def create_interface(self):
        """
        创建地形编辑器UI界面
        """
        print("创建地形编辑器UI界面")
        
        # 创建主窗口部件
        self.main_widget = QWidget()
        self.main_widget.setWindowTitle("地形编辑器")
        self.main_widget.setGeometry(100, 100, 400, 600)
        
        # 创建主布局
        main_layout = QVBoxLayout()
        
        # 创建标签页
        tab_widget = QTabWidget()
        
        # 工具标签页
        tools_tab = self._create_tools_tab()
        tab_widget.addTab(tools_tab, "工具")
        
        # 属性标签页
        properties_tab = self._create_properties_tab()
        tab_widget.addTab(properties_tab, "属性")
        
        # 材质标签页
        material_tab = self._create_material_tab()
        tab_widget.addTab(material_tab, "材质")
        
        # 添加标签页到主布局
        main_layout.addWidget(tab_widget)
        
        # 添加应用和取消按钮
        button_layout = QHBoxLayout()
        apply_btn = QPushButton("应用")
        cancel_btn = QPushButton("取消")
        apply_btn.clicked.connect(self._apply_changes)
        cancel_btn.clicked.connect(self._cancel_changes)
        
        button_layout.addWidget(apply_btn)
        button_layout.addWidget(cancel_btn)
        
        main_layout.addLayout(button_layout)
        
        self.main_widget.setLayout(main_layout)
        
        # 隐藏界面，等待用户打开
        self.main_widget.hide()
        
    def _create_tools_tab(self):
        """
        创建工具标签页
        """
        tools_tab = QWidget()
        layout = QVBoxLayout()
        
        # 创建工具选择组
        tools_group = QGroupBox("编辑工具")
        tools_layout = QVBoxLayout()
        
        # 工具选择下拉框
        self.tool_selector = QComboBox()
        self.tool_selector.addItems([
            "抬高工具", 
            "降低工具", 
            "平坦化工具", 
            "平滑工具", 
            "噪声工具"
        ])
        self.tool_selector.currentTextChanged.connect(self._on_tool_changed)
        
        tools_layout.addWidget(QLabel("选择工具:"))
        tools_layout.addWidget(self.tool_selector)
        
        # 工具参数组
        params_group = QGroupBox("工具参数")
        params_layout = QGridLayout()
        
        # 半径参数
        params_layout.addWidget(QLabel("半径:"), 0, 0)
        self.radius_slider = QSlider(Qt.Horizontal)
        self.radius_slider.setRange(1, 100)
        self.radius_slider.setValue(10)
        self.radius_slider.valueChanged.connect(self._on_radius_changed)
        params_layout.addWidget(self.radius_slider, 0, 1)
        
        self.radius_label = QLabel("10")
        params_layout.addWidget(self.radius_label, 0, 2)
        
        # 强度参数
        params_layout.addWidget(QLabel("强度:"), 1, 0)
        self.strength_slider = QSlider(Qt.Horizontal)
        self.strength_slider.setRange(1, 100)
        self.strength_slider.setValue(50)
        self.strength_slider.valueChanged.connect(self._on_strength_changed)
        params_layout.addWidget(self.strength_slider, 1, 1)
        
        self.strength_label = QLabel("50")
        params_layout.addWidget(self.strength_label, 1, 2)
        
        # 操作类型
        params_layout.addWidget(QLabel("操作:"), 2, 0)
        self.operation_selector = QComboBox()
        self.operation_selector.addItems(["添加", "减去", "设置"])
        self.operation_selector.currentTextChanged.connect(self._on_operation_changed)
        params_layout.addWidget(self.operation_selector, 2, 1, 1, 2)
        
        params_group.setLayout(params_layout)
        
        tools_layout.addWidget(params_group)
        tools_group.setLayout(tools_layout)
        
        layout.addWidget(tools_group)
        
        # 预览设置组
        preview_group = QGroupBox("预览设置")
        preview_layout = QVBoxLayout()
        
        self.preview_checkbox = QPushButton("启用实时预览")
        self.preview_checkbox.setCheckable(True)
        self.preview_checkbox.setChecked(True)
        self.preview_checkbox.clicked.connect(self._on_preview_toggled)
        
        preview_layout.addWidget(self.preview_checkbox)
        preview_group.setLayout(preview_layout)
        
        layout.addWidget(preview_group)
        
        tools_tab.setLayout(layout)
        return tools_tab
        
    def _create_properties_tab(self):
        """
        创建属性标签页
        """
        properties_tab = QWidget()
        layout = QVBoxLayout()
        
        # 地形尺寸组
        size_group = QGroupBox("地形尺寸")
        size_layout = QGridLayout()
        
        size_layout.addWidget(QLabel("宽度:"), 0, 0)
        self.width_input = self._create_double_input("100.0")
        size_layout.addWidget(self.width_input, 0, 1)
        
        size_layout.addWidget(QLabel("长度:"), 1, 0)
        self.length_input = self._create_double_input("100.0")
        size_layout.addWidget(self.length_input, 1, 1)
        
        size_layout.addWidget(QLabel("高度:"), 2, 0)
        self.height_input = self._create_double_input("50.0")
        size_layout.addWidget(self.height_input, 2, 1)
        
        size_group.setLayout(size_layout)
        
        layout.addWidget(size_group)
        
        # 分辨率组
        resolution_group = QGroupBox("分辨率")
        resolution_layout = QGridLayout()
        
        resolution_layout.addWidget(QLabel("X分辨率:"), 0, 0)
        self.x_resolution_input = self._create_double_input("129.0")
        resolution_layout.addWidget(self.x_resolution_input, 0, 1)
        
        resolution_layout.addWidget(QLabel("Y分辨率:"), 1, 0)
        self.y_resolution_input = self._create_double_input("129.0")
        resolution_layout.addWidget(self.y_resolution_input, 1, 1)
        
        resolution_group.setLayout(resolution_layout)
        
        layout.addWidget(resolution_group)
        
        # LOD设置组
        lod_group = QGroupBox("LOD设置")
        lod_layout = QVBoxLayout()
        
        self.lod_enabled_checkbox = QPushButton("启用LOD")
        self.lod_enabled_checkbox.setCheckable(True)
        self.lod_enabled_checkbox.setChecked(True)
        
        lod_layout.addWidget(self.lod_enabled_checkbox)
        
        # LOD阈值滑块
        lod_layout.addWidget(QLabel("LOD阈值:"))
        self.lod_threshold_slider = QSlider(Qt.Horizontal)
        self.lod_threshold_slider.setRange(1, 100)
        self.lod_threshold_slider.setValue(50)
        lod_layout.addWidget(self.lod_threshold_slider)
        
        lod_group.setLayout(lod_layout)
        
        layout.addWidget(lod_group)
        
        properties_tab.setLayout(layout)
        return properties_tab
        
    def _create_material_tab(self):
        """
        创建材质标签页
        """
        material_tab = QWidget()
        layout = QVBoxLayout()
        
        # 材质类型组
        type_group = QGroupBox("材质类型")
        type_layout = QVBoxLayout()
        
        self.material_type_selector = QComboBox()
        self.material_type_selector.addItems([
            "单纹理", 
            "多纹理混合", 
            "程序化生成"
        ])
        self.material_type_selector.currentTextChanged.connect(self._on_material_type_changed)
        
        type_layout.addWidget(self.material_type_selector)
        type_group.setLayout(type_layout)
        
        layout.addWidget(type_group)
        
        # 纹理路径组
        texture_group = QGroupBox("纹理设置")
        texture_layout = QVBoxLayout()
        
        texture_layout.addWidget(QLabel("基础纹理:"))
        self.base_texture_input = self._create_file_input()
        texture_layout.addWidget(self.base_texture_input)
        
        # 添加纹理按钮
        add_texture_btn = QPushButton("添加纹理")
        add_texture_btn.clicked.connect(self._on_add_texture)
        texture_layout.addWidget(add_texture_btn)
        
        texture_group.setLayout(texture_layout)
        
        layout.addWidget(texture_group)
        
        # 材质属性组
        material_props_group = QGroupBox("材质属性")
        material_props_layout = QGridLayout()
        
        material_props_layout.addWidget(QLabel("环境光:"), 0, 0)
        self.ambient_input = self._create_double_input("0.5")
        material_props_layout.addWidget(self.ambient_input, 0, 1)
        
        material_props_layout.addWidget(QLabel("漫反射:"), 1, 0)
        self.diffuse_input = self._create_double_input("0.8")
        material_props_layout.addWidget(self.diffuse_input, 1, 1)
        
        material_props_layout.addWidget(QLabel("镜面反射:"), 2, 0)
        self.specular_input = self._create_double_input("0.2")
        material_props_layout.addWidget(self.specular_input, 2, 1)
        
        material_props_layout.addWidget(QLabel("光泽度:"), 3, 0)
        self.shininess_input = self._create_double_input("5.0")
        material_props_layout.addWidget(self.shininess_input, 3, 1)
        
        material_props_group.setLayout(material_props_layout)
        
        layout.addWidget(material_props_group)
        
        material_tab.setLayout(layout)
        return material_tab
        
    def _create_double_input(self, default_value):
        """
        创建双精度输入框
        """
        from PyQt5.QtWidgets import QLineEdit
        input_field = QLineEdit(default_value)
        validator = QDoubleValidator()
        input_field.setValidator(validator)
        return input_field
        
    def _create_file_input(self):
        """
        创建文件路径输入框
        """
        from PyQt5.QtWidgets import QLineEdit, QHBoxLayout, QPushButton, QFileDialog
        widget = QWidget()
        layout = QHBoxLayout()
        
        input_field = QLineEdit()
        browse_btn = QPushButton("浏览")
        
        def browse_file():
            file_path, _ = QFileDialog.getOpenFileName(
                None, "选择纹理文件", "", "纹理文件 (*.png *.jpg *.jpeg *.bmp *.tga)"
            )
            if file_path:
                input_field.setText(file_path)
                
        browse_btn.clicked.connect(browse_file)
        
        layout.addWidget(input_field)
        layout.addWidget(browse_btn)
        widget.setLayout(layout)
        
        return widget
        
    def _on_tool_changed(self, text):
        """
        工具选择变化事件处理
        """
        print(f"工具选择变化: {text}")
        
    def _on_radius_changed(self, value):
        """
        半径变化事件处理
        """
        self.radius_label.setText(str(value))
        
    def _on_strength_changed(self, value):
        """
        强度变化事件处理
        """
        self.strength_label.setText(str(value))
        
    def _on_operation_changed(self, text):
        """
        操作类型变化事件处理
        """
        print(f"操作类型变化: {text}")
        
    def _on_preview_toggled(self, checked):
        """
        预览开关事件处理
        """
        print(f"预览开关: {checked}")
        
    def _on_material_type_changed(self, text):
        """
        材质类型变化事件处理
        """
        print(f"材质类型变化: {text}")
        
    def _on_add_texture(self):
        """
        添加纹理事件处理
        """
        print("添加纹理")
        
    def _apply_changes(self):
        """
        应用更改
        """
        print("应用更改")
        
    def _cancel_changes(self):
        """
        取消更改
        """
        print("取消更改")
        
    def toggle_visibility(self):
        """
        切换界面可见性
        """
        if self.main_widget:
            if self.main_widget.isVisible():
                self.main_widget.hide()
            else:
                self.main_widget.show()
                
    def cleanup(self):
        """
        清理UI资源
        """
        if self.main_widget:
            self.main_widget.close()
            self.main_widget = None