using CommunityToolkit.Mvvm.ComponentModel;
using System.Diagnostics;   // 添加命名空间

namespace ASTM_D7896_Tester.ViewModels;

public partial class MeasurementResult : ObservableObject
{
    [ObservableProperty]
    private int _index;

    [ObservableProperty]
    private double _thermalConductivity; // W/m·K

    [ObservableProperty]
    private double _thermalDiffusivity;  // ×10⁻⁶ m²/s

    [ObservableProperty]
    private double _specificHeatCapacity;  // 比热容 J/(kg·K)

    public void CalculateVhcAndCp(double density)
    {
        // 1. 计算体积热容 VHC = λ / α
        // 注意：ThermalDiffusivity 已经是标准单位 m²/s，绝对不能再乘 1e-6！
        if (ThermalDiffusivity > 0)
        {
            double vhc_J = ThermalConductivity / ThermalDiffusivity; // 单位: J/(m³·K)
            VolumetricHeatCapacity = vhc_J / 1000.0; // 转换为 kJ/(m³·K)
        }
        else
        {
            VolumetricHeatCapacity = 0;
        }

        // 2. 计算比热容 Cp = VHC / ρ
        if (density > 0)
        {
            // VHC 转回 J/(m³·K) 除以密度
            SpecificHeatCapacity = (VolumetricHeatCapacity * 1000.0) / density;
        }
        else
        {
            SpecificHeatCapacity = 0;
        }

        Debug.WriteLine($"[MeasurementResult] 计算完成: VHC={VolumetricHeatCapacity:F2} kJ/(m³·K), Cp={SpecificHeatCapacity:F2} J/(kg·K)");
    }

    [ObservableProperty]
    private double _volumetricHeatCapacity; // kJ/m³·K (自动计算)

    public void CalculateVhc()
    {
        Debug.WriteLine($"[MeasurementResult] 计算体积热容 - 热导率: {ThermalConductivity} W/(m·K), 热扩散率: {ThermalDiffusivity} ×10⁻⁶ m²/s");

        if (ThermalDiffusivity > 0)
        {
            VolumetricHeatCapacity = ThermalConductivity / (ThermalDiffusivity * 1e-6) / 1000.0;
            Debug.WriteLine($"[MeasurementResult] 计算得到体积热容: {VolumetricHeatCapacity} kJ/(m³·K)");
        }
        else
        {
            VolumetricHeatCapacity = 0;
            Debug.WriteLine($"[MeasurementResult] 警告: 热扩散率为0，体积热容设为0");
        }
    }
}