"""
软体物理和布料模拟插件测试脚本
用于测试插件的基本功能
"""

import sys
import os

# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
sys.path.insert(0, project_root)

def test_plugin_structure():
    """
    测试插件结构是否正确
    """
    print("开始测试软体物理和布料模拟插件结构...")
    
    # 测试导入插件模块
    try:
        from plugins.user.soft_body_cloth_physics.plugin import Plugin
        print("✓ 插件主类导入成功")
    except ImportError as e:
        print(f"✗ 插件主类导入失败: {e}")
        return False
        
    # 测试导入核心模块
    try:
        from plugins.user.soft_body_cloth_physics.core.cloth_manager import ClothManager
        from plugins.user.soft_body_cloth_physics.core.soft_body_manager import SoftBodyManager
        print("✓ 核心管理器导入成功")
    except ImportError as e:
        print(f"✗ 核心管理器导入失败: {e}")
        return False
        
    # 测试导入物理模块
    try:
        from plugins.user.soft_body_cloth_physics.core.cloth_physics import ClothPhysics
        from plugins.user.soft_body_cloth_physics.core.soft_body_physics import SoftBodyPhysics
        print("✓ 物理核心类导入成功")
    except ImportError as e:
        print(f"✗ 物理核心类导入失败: {e}")
        return False
        
    # 测试导入材质系统
    try:
        from plugins.user.soft_body_cloth_physics.materials.cloth_materials import ClothMaterialManager
        print("✓ 材质系统导入成功")
    except ImportError as e:
        print(f"✗ 材质系统导入失败: {e}")
        return False
        
    # 测试导入工具模块
    try:
        from plugins.user.soft_body_cloth_physics.utils.mesh_generator import MeshGenerator
        print("✓ 网格生成工具导入成功")
    except ImportError as e:
        print(f"✗ 网格生成工具导入失败: {e}")
        return False
        
    # 测试导入调试模块
    try:
        from plugins.user.soft_body_cloth_physics.debug.cloth_debug import PhysicsDebugManager
        print("✓ 调试工具导入成功")
    except ImportError as e:
        print(f"✗ 调试工具导入失败: {e}")
        return False
        
    # 测试配置文件是否存在
    config_path = os.path.join(
        project_root, 
        "plugins", 
        "user", 
        "soft_body_cloth_physics", 
        "config", 
        "cloth_settings.json"
    )
    
    if os.path.exists(config_path):
        print("✓ 配置文件存在")
    else:
        print("✗ 配置文件不存在")
        return False
        
    print("插件结构测试通过!")
    return True

def test_plugin_functionality():
    """
    测试插件基本功能
    """
    print("\n开始测试插件基本功能...")
    
    # 创建一个模拟的世界对象
    class MockWorld:
        def __init__(self):
            self.plugins = {}
            self.world = self  # 添加world属性以避免错误
            
    try:
        # 测试插件初始化
        from plugins.user.soft_body_cloth_physics.plugin import Plugin
        
        mock_world = MockWorld()
        plugin = Plugin(mock_world, "soft_body_cloth_physics")
        
        if plugin.initialize():
            print("✓ 插件初始化成功")
        else:
            print("✗ 插件初始化失败")
            return False
            
        # 测试启用插件
        if plugin.enable():
            print("✓ 插件启用成功")
        else:
            print("✗ 插件启用失败")
            return False
            
        # 测试获取插件信息
        info = plugin.get_info()
        if info and "name" in info and info["name"] == "SoftBodyClothPhysics":
            print("✓ 插件信息获取成功")
        else:
            print("✗ 插件信息获取失败")
            return False
            
        # 测试禁用插件
        if plugin.disable():
            print("✓ 插件禁用成功")
        else:
            print("✗ 插件禁用失败")
            return False
            
        # 测试清理插件
        plugin.finalize()
        print("✓ 插件清理成功")
            
        print("插件功能测试通过!")
        return True
        
    except Exception as e:
        print(f"✗ 插件功能测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_cloth_materials():
    """
    测试布料材质系统
    """
    print("\n开始测试布料材质系统...")
    
    try:
        from plugins.user.soft_body_cloth_physics.materials.cloth_materials import ClothMaterialManager
        
        # 创建材质管理器
        material_manager = ClothMaterialManager()
        
        # 测试获取材质
        cotton = material_manager.get_material("cotton")
        if cotton:
            print("✓ 棉布材质获取成功")
        else:
            print("✗ 棉布材质获取失败")
            return False
            
        # 测试获取所有材质类型
        material_types = material_manager.get_all_material_types()
        if material_types:
            print(f"✓ 材质类型获取成功: {len(material_types)} 种")
        else:
            print("✗ 材质类型获取失败")
            return False
            
        print("布料材质系统测试通过!")
        return True
        
    except Exception as e:
        print(f"✗ 布料材质系统测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def main():
    """
    主测试函数
    """
    print("软体物理和布料模拟插件测试")
    print("=" * 40)
    
    # 运行所有测试
    tests = [
        test_plugin_structure,
        test_plugin_functionality,
        test_cloth_materials
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        if test():
            passed += 1
        print()
        
    print("=" * 40)
    print(f"测试结果: {passed}/{total} 通过")
    
    if passed == total:
        print("所有测试通过! 插件已准备好使用。")
        return True
    else:
        print("部分测试失败，请检查插件实现。")
        return False

if __name__ == "__main__":
    main()