namespace ConeCalorimeter.Models;

public sealed record RealtimeDataRecord(
    DateTime Timestamp,
    double OrificeFlow,
    double OrificePressure,
    double OrificeTemperature,
    double ConeTemperature,
    double SampleTemperature,
    double Irradiance,
    bool FlameDetected,
    double Oxygen,
    double CarbonDioxide,
    double CarbonMonoxide,
    double Absorbance,
    double HeatReleaseRate,
    double PeakHeatReleaseRate,
    double CFactor,
    double Qa180,
    double Qa300,
    double TotalHeatRelease,
    double SmokeProduction,
    double CurrentMass,
    double InitialMass,
    double MassLoss,
    double MassLossRate,
    int IgnitionSeconds,
    int TestSeconds,
    double TotalSmoke,
    double RawHeatReleaseRate = double.NaN,
    double RawTotalHeatRelease = double.NaN,
    bool IsHeatReleaseCorrected = false,
    string HeatReleaseCorrectionVersion = "")
{
    private const double DefaultIrradiatedAreaSquareMeters = 0.01;

    public static RealtimeDataRecord FromSnapshot(RealtimeSnapshot snapshot)
    {
        return new RealtimeDataRecord(
            DateTime.Now,
            snapshot.OrificeFlow,
            snapshot.OrificePressure,
            snapshot.OrificeTemperature,
            snapshot.ConeTemperature,
            snapshot.SampleTemperature,
            snapshot.Irradiance,
            snapshot.FlameDetected,
            snapshot.Oxygen,
            snapshot.CarbonDioxide,
            snapshot.CarbonMonoxide,
            snapshot.Absorbance,
            snapshot.HeatReleaseRate,
            snapshot.PeakHeatReleaseRate,
            snapshot.CFactor,
            snapshot.Qa180,
            snapshot.Qa300,
            snapshot.TotalHeatRelease,
            snapshot.SmokeProduction,
            snapshot.CurrentMass,
            snapshot.InitialMass,
            snapshot.MassLoss,
            snapshot.MassLossRate,
            snapshot.IgnitionSeconds,
            snapshot.TestSeconds,
            snapshot.TotalSmoke,
            snapshot.HeatReleaseRate,
            snapshot.TotalHeatRelease);
    }

    public double HeatReleaseRateKw =>
        double.IsFinite(HeatReleaseRate) ? HeatReleaseRate : double.NaN;

    public double EffectiveHeatOfCombustion =>
        double.IsFinite(HeatReleaseRate) && double.IsFinite(MassLossRate) && MassLossRate > 0
            ? HeatReleaseRate * DefaultIrradiatedAreaSquareMeters / MassLossRate
            : double.NaN;
}
