using System.Globalization;
using ConeCalorimeter.Models;

namespace ConeCalorimeter.ViewModels;

public sealed class RealtimeDataRowViewModel
{
    public RealtimeDataRowViewModel()
    {
    }

    public RealtimeDataRowViewModel(RealtimeDataRecord record)
    {
        TimeText = record.TestSeconds < 0 ? string.Empty : record.TestSeconds.ToString(CultureInfo.InvariantCulture);
        OxygenText = Format(record.Oxygen);
        CarbonDioxideText = Format(record.CarbonDioxide);
        CarbonMonoxideText = Format(record.CarbonMonoxide);
        OrificePressureText = Format(record.OrificePressure);
        OrificeTemperatureText = Format(record.OrificeTemperature);
        AbsorbanceText = Format(record.Absorbance, "0.00000");
        Hrr50Text = Format(record.HeatReleaseRate);
        ThrText = Format(record.TotalHeatRelease);
        Spr50Text = Format(record.SmokeProduction);
        TsrText = Format(record.TotalSmoke);
        MlrText = Format(record.MassLossRate);
        HeatReleaseText = Format(record.HeatReleaseRateKw);
        EhcText = Format(record.EffectiveHeatOfCombustion);
        MassLossText = FormatAbsolute(record.MassLoss);
    }

    public string TimeText { get; init; } = string.Empty;

    public string OxygenText { get; init; } = string.Empty;

    public string CarbonDioxideText { get; init; } = string.Empty;

    public string CarbonMonoxideText { get; init; } = string.Empty;

    public string OrificePressureText { get; init; } = string.Empty;

    public string OrificeTemperatureText { get; init; } = string.Empty;

    public string AbsorbanceText { get; init; } = string.Empty;

    public string Hrr50Text { get; init; } = string.Empty;

    public string ThrText { get; init; } = string.Empty;

    public string Spr50Text { get; init; } = string.Empty;

    public string TsrText { get; init; } = string.Empty;

    public string MlrText { get; init; } = string.Empty;

    public string HeatReleaseText { get; init; } = string.Empty;

    public string EhcText { get; init; } = string.Empty;

    public string MassLossText { get; init; } = string.Empty;

    private static string Format(double value, string format = "0.00")
    {
        return double.IsFinite(value) ? value.ToString(format, CultureInfo.InvariantCulture) : string.Empty;
    }

    private static string FormatAbsolute(double value, string format = "0.00")
    {
        return double.IsFinite(value) ? Math.Abs(value).ToString(format, CultureInfo.InvariantCulture) : string.Empty;
    }
}
