#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
独立GUI编辑器测试脚本
测试GUI编辑器窗口的功能
"""

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt

# 模拟world对象
class MockWorld:
    def __init__(self):
        self.gui_elements = []  # 存储GUI元素
        
    def createGUIButton(self, pos, text, scale):
        """模拟创建GUI按钮"""
        print(f"模拟创建按钮: {text} at {pos} scale {scale}")
        button = MockGUIElement("button", text, pos, scale)
        self.gui_elements.append(button)
        return button
        
    def createGUILabel(self, pos, text, scale):
        """模拟创建GUI标签"""
        print(f"模拟创建标签: {text} at {pos} scale {scale}")
        label = MockGUIElement("label", text, pos, scale)
        self.gui_elements.append(label)
        return label
        
    def createGUIEntry(self, pos, text, scale):
        """模拟创建GUI输入框"""
        print(f"模拟创建输入框: {text} at {pos} scale {scale}")
        entry = MockGUIElement("entry", text, pos, scale)
        self.gui_elements.append(entry)
        return entry
        
    def createGUISlider(self, pos, text, scale):
        """模拟创建GUI滑块"""
        print(f"模拟创建滑块: {text} at {pos} scale {scale}")
        slider = MockGUIElement("slider", text, pos, scale)
        self.gui_elements.append(slider)
        return slider
        
    def createGUI3DText(self, pos, text, scale):
        """模拟创建3D文本"""
        print(f"模拟创建3D文本: {text} at {pos} scale {scale}")
        text3d = MockGUIElement("3d_text", text, pos, scale)
        self.gui_elements.append(text3d)
        return text3d
        
    def createGUIVirtualScreen(self, pos, size, text):
        """模拟创建虚拟屏幕"""
        print(f"模拟创建虚拟屏幕: {text} at {pos} size {size}")
        screen = MockGUIElement("virtual_screen", text, pos, 1.0, size=size)
        self.gui_elements.append(screen)
        return screen
        
    def deleteGUIElement(self, element):
        """模拟删除GUI元素"""
        if element in self.gui_elements:
            self.gui_elements.remove(element)
            print(f"模拟删除GUI元素: {element.text}")
            return True
        return False
        
    def editGUIElement(self, element, property_name, value):
        """模拟编辑GUI元素"""
        if property_name == "text":
            element.text = value
            print(f"模拟编辑GUI元素文本: {element.text} -> {value}")
        elif property_name == "position":
            element.pos = value
            print(f"模拟编辑GUI元素位置: {element.pos} -> {value}")
        elif property_name == "scale":
            element.scale = value
            print(f"模拟编辑GUI元素缩放: {element.scale} -> {value}")
        return True

class MockGUIElement:
    """模拟GUI元素"""
    def __init__(self, gui_type, text, pos, scale, size=None):
        self.gui_type = gui_type
        self.text = text
        self.pos = pos
        self.scale = scale
        self.size = size or (1, 1)
        
    def getTag(self, tag_name):
        """模拟获取标签"""
        if tag_name == "gui_type":
            return self.gui_type
        elif tag_name == "gui_text":
            return self.text
        return ""
        
    def setTag(self, tag_name, value):
        """模拟设置标签"""
        if tag_name == "gui_text":
            self.text = value
            
    def getPos(self):
        """模拟获取位置"""
        return MockVector3(self.pos)
        
    def setPos(self, *args):
        """模拟设置位置"""
        if len(args) == 1:
            self.pos = (args[0].getX(), args[0].getY(), args[0].getZ())
        else:
            self.pos = args[:3]
            
    def getScale(self):
        """模拟获取缩放"""
        return MockVector3((self.scale, self.scale, self.scale))
        
    def setScale(self, *args):
        """模拟设置缩放"""
        if len(args) == 1:
            if hasattr(args[0], 'getX'):
                self.scale = args[0].getX()
            else:
                self.scale = args[0]
        else:
            self.scale = args[0]

class MockVector3:
    """模拟三维向量"""
    def __init__(self, values):
        if isinstance(values, (list, tuple)):
            self.x, self.y, self.z = values[:3]
        else:
            self.x = self.y = self.z = values
            
    def getX(self):
        return self.x
        
    def getY(self):
        return self.y
        
    def getZ(self):
        return self.z

class TestMainWindow(QMainWindow):
    """测试主窗口"""
    def __init__(self):
        super().__init__()
        self.world = MockWorld()
        self.gui_editor_window = None
        
        self.setupUI()
        
    def setupUI(self):
        """设置UI"""
        self.setWindowTitle("GUI编辑器测试")
        self.setGeometry(100, 100, 400, 200)
        
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        layout = QVBoxLayout(central_widget)
        
        # 打开GUI编辑器按钮
        open_button = QPushButton("打开GUI编辑器")
        open_button.clicked.connect(self.openGUIEditor)
        layout.addWidget(open_button)
        
        # 添加一些测试GUI元素按钮
        add_button = QPushButton("添加测试GUI元素")
        add_button.clicked.connect(self.addTestGUIElements)
        layout.addWidget(add_button)
        
        # 显示状态
        self.status_label = QPushButton("状态: 准备就绪")
        self.status_label.setEnabled(False)
        layout.addWidget(self.status_label)
        
    def openGUIEditor(self):
        """打开GUI编辑器"""
        try:
            from gui_editor_window import GUIEditorWindow
            
            if self.gui_editor_window is None:
                self.gui_editor_window = GUIEditorWindow(world=self.world)
                self.gui_editor_window.editor_closed.connect(self.onGUIEditorClosed)
                self.gui_editor_window.show()
                self.status_label.setText("状态: GUI编辑器已打开")
                print("GUI编辑器窗口已打开")
            else:
                self.gui_editor_window.raise_()
                self.gui_editor_window.activateWindow()
                
        except ImportError as e:
            print(f"无法导入GUI编辑器: {e}")
            self.status_label.setText("状态: 导入GUI编辑器失败")
        except Exception as e:
            print(f"打开GUI编辑器失败: {e}")
            self.status_label.setText(f"状态: 打开失败 - {e}")
            
    def onGUIEditorClosed(self):
        """GUI编辑器关闭回调"""
        self.gui_editor_window = None
        self.status_label.setText("状态: GUI编辑器已关闭")
        print("GUI编辑器窗口已关闭")
        
    def addTestGUIElements(self):
        """添加测试GUI元素"""
        # 添加一些测试元素
        self.world.createGUIButton((0, 0, 0), "测试按钮", 0.08)
        self.world.createGUILabel((3, 0, 0), "测试标签", 0.06)
        self.world.createGUI3DText((0, 5, 0), "测试3D文本", 0.5)
        
        # 如果GUI编辑器窗口打开，同步元素
        if self.gui_editor_window:
            self.gui_editor_window.syncGUIElements()
            
        self.status_label.setText(f"状态: 已添加测试元素，总计 {len(self.world.gui_elements)} 个")

def main():
    """主函数"""
    app = QApplication(sys.argv)
    
    # 创建测试窗口
    main_window = TestMainWindow()
    main_window.show()
    
    print("GUI编辑器测试启动")
    print("点击'打开GUI编辑器'按钮来测试独立的GUI编辑器窗口")
    
    sys.exit(app.exec_())

if __name__ == "__main__":
    main() 