"""
物理材质系统测试脚本
测试增强的物理材质功能
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from panda3d.core import Vec3, Vec4, Point3
from plugins.user.rigid_body_physics.core.material import PhysicsMaterial


def test_advanced_material_features():
    """测试增强的材质功能"""
    print("=== 测试增强的物理材质系统 ===\n")
    
    # 创建一个测试材质
    test_material = PhysicsMaterial('test_material', material_type=PhysicsMaterial.TYPE_METAL)
    
    print("1. 测试热效应计算:")
    # 测试热效应计算
    temp_change = test_material.calculate_thermal_effect(0.1, environment_temp=25.0)
    print(f"   基础温度变化: {temp_change:.4f}°C")
    
    # 测试带热源的热效应计算
    heat_sources = [{'position': Point3(0, 0, 0), 'temperature': 100.0, 'radius': 1.0}]
    thermal_result = test_material.calculate_thermal_effect(0.1, environment_temp=25.0, heat_sources=heat_sources)
    print(f"   带热源温度变化: {thermal_result:.4f}°C")
    
    # 测试热膨胀
    expansion_factor = test_material.calculate_thermal_expansion(temp_change)
    print(f"   热膨胀因子: {expansion_factor:.6f}")
    print()
    
    print("2. 测试环境效应:")
    # 测试环境效应
    weather_conditions = {
        'rain': 0.8,
        'wind': 0.5,
        'humidity': 0.9,
        'uv': 0.7
    }
    env_effects = test_material.calculate_environmental_effects(1.0, weather_conditions)
    print(f"   环境效应结果: {env_effects}")
    print(f"   当前湿润度: {test_material.state['wetness']:.3f}")
    print(f"   当前污染度: {test_material.state['contamination']:.3f}")
    print(f"   当前磨损度: {test_material.state['wear']:.3f}")
    print()
    
    print("3. 测试老化模拟:")
    # 测试老化模拟
    environmental_factors = {
        'temperature': 60,  # 60°C
        'humidity': 0.9,    # 90%湿度
        'chemical_exposure': 0.3
    }
    aging_result = test_material.simulate_aging(10.0, environmental_factors)
    print(f"   老化结果: {aging_result}")
    print(f"   当前磨损度: {test_material.state['wear']:.3f}")
    print()
    
    print("4. 测试摩擦系数计算:")
    # 测试摩擦系数计算
    other_material = PhysicsMaterial('other_material', material_type=PhysicsMaterial.TYPE_CONCRETE)
    effective_friction = test_material.get_effective_friction(
        other_material=other_material,
        velocity=Vec3(2.0, 0, 0),
        normal_force=100.0,
        surface_area=0.5
    )
    print(f"   有效摩擦系数: {effective_friction:.4f}")
    
    dynamic_friction = test_material.get_dynamic_friction(
        other_material=other_material,
        velocity=Vec3(2.0, 0, 0),
        normal_force=100.0,
        surface_area=0.5
    )
    print(f"   动态摩擦系数: {dynamic_friction:.4f}")
    print()
    
    print("5. 测试恢复系数计算:")
    # 测试恢复系数计算
    effective_restitution = test_material.get_effective_restitution(
        other_material=other_material,
        impact_velocity=10.0,
        temperature=50.0
    )
    print(f"   有效恢复系数: {effective_restitution:.4f}")
    print()
    
    print("6. 测试能量吸收:")
    # 测试能量吸收
    energy_absorption = test_material.get_energy_absorption(100.0, 0.1)
    print(f"   能量吸收结果: {energy_absorption}")
    print(f"   当前温度: {test_material.state['temperature']:.2f}°C")
    print(f"   当前应力: {test_material.state['stress']:.2f}")
    print()
    
    print("7. 测试磨损和疲劳:")
    # 测试磨损率计算
    wear_rate = test_material.calculate_wear_rate(Vec3(1.0, 0, 0), 50.0, 1.0)
    print(f"   磨损率: {wear_rate:.6f}")
    
    # 测试疲劳累积
    fatigue_result = test_material.calculate_fatigue_accumulation(200.0, 1000)
    print(f"   疲劳累积结果: {fatigue_result}")
    print(f"   当前疲劳度: {test_material.state['fatigue']:.4f}")
    print()
    
    print("8. 测试流体动力学:")
    # 测试流体动力学
    fluid_result = test_material.calculate_fluid_dynamics(Vec3(5.0, 0, 0), 1000.0, 1.0)
    print(f"   流体动力学结果: {fluid_result}")
    print(f"   当前湿润度: {test_material.state['wetness']:.3f}")
    print()
    
    print("9. 测试化学反应:")
    # 测试化学反应
    chemical_composition = {
        'acid': 0.5,
        'base': 0.2,
        'oxidizer': 0.3
    }
    chemical_result = test_material.calculate_chemical_reaction(chemical_composition, temperature=30.0, time_delta=1.0)
    print(f"   化学反应结果: {chemical_result}")
    print(f"   当前污染度: {test_material.state['contamination']:.3f}")
    print()
    
    print("10. 测试失效准则:")
    # 测试失效准则
    stress_tensor = [1000.0, 500.0, -200.0, 100.0, 50.0, 75.0]
    failure_analysis = test_material.check_failure_criteria(stress_tensor)
    print(f"   失效分析结果: {failure_analysis}")
    print()
    
    print("11. 测试结构完整性:")
    # 测试结构完整性
    integrity_assessment = test_material.get_structural_integrity()
    print(f"   结构完整性: {integrity_assessment}")
    print()
    
    print("12. 测试历史统计:")
    # 测试历史统计
    aging_stats = test_material.get_aging_stats()
    fatigue_stats = test_material.get_fatigue_stats()
    chemical_stats = test_material.get_chemical_stats()
    print(f"   老化统计: {aging_stats}")
    print(f"   疲劳统计: {fatigue_stats}")
    print(f"   化学统计: {chemical_stats}")
    print()
    
    print("=== 测试完成 ===")


def test_predefined_materials():
    """测试预定义材质"""
    print("=== 测试预定义材质 ===\n")
    
    # 测试几种预定义材质
    material_names = ['concrete', 'metal', 'wood', 'rubber']
    
    for name in material_names:
        material = PhysicsMaterial.MATERIALS.get(name)
        if material:
            print(f"{name.capitalize()} 材质属性:")
            print(f"  密度: {material.properties['density']} kg/m³")
            print(f"  摩擦系数: {material.properties['friction']}")
            print(f"  恢复系数: {material.properties['restitution']}")
            if 'tensile_strength' in material.properties:
                print(f"  抗拉强度: {material.properties['tensile_strength']} Pa")
            if 'compressive_strength' in material.properties:
                print(f"  抗压强度: {material.properties['compressive_strength']} Pa")
            print()


if __name__ == "__main__":
    test_advanced_material_features()
    print("\n" + "="*50 + "\n")
    test_predefined_materials()