namespace RailEnterprise.Protocol.Messages;
// Messaggi della fase F6: tooltip «perché» (ADR 0024). La spiegazione di un valore arriva su richiesta
// (RequestDetail con DetailKind.Explain, bersaglio nel blocco 1 della richiesta) ed è calcolata dal server
// con Explanations del Core: il client la mostra com'è, senza rifare formule né valori.
///
/// Termine di una spiegazione: simbolo nella formula, etichetta (testo italiano fisso, chiave di traduzione),
/// valore, unità (ExplainUnit del Core: 0 numero, 1 importo in centesimi, 2 prezzo in centesimi, 3 percentuale,
/// 4 fattore, 5 km, 6 giorni, 7 unità, 8 condizione 0–100, 9 anni, 10 testo) e testo per i valori testuali.
///
public sealed class ExplanationTermInfo
{
public string Symbol { get; init; } = "";
public string Label { get; init; } = "";
public double Value { get; init; }
public byte Unit { get; init; }
public string Text { get; init; } = "";
public void Write(ref SpanWriter w)
{
w.String(Symbol); w.String(Label); w.F64(Value); w.U8(Unit); w.String(Text);
w.Flags(0);
}
public static ExplanationTermInfo Read(ref SpanReader r)
{
var m = new ExplanationTermInfo { Symbol = r.String(), Label = r.String(), Value = r.F64(), Unit = r.U8(), Text = r.String() };
r.Flags();
return m;
}
}
///
/// Spiegazione di un valore per la compagnia della sessione (F6, ADR 0024): bersaglio (soggetto e due
/// argomenti, come nella richiesta), giorno, disponibilità, titolo e nome dell'oggetto, valore con unità e
/// testo, formula, termini e note. Il valore è lo stesso numero del motore.
///
public sealed class ExplanationReport : IMessage
{
public MessageId Id => MessageId.ExplanationReport;
public long Day { get; init; }
public byte Subject { get; init; }
public int Target { get; init; } = -1;
public int Arg { get; init; } = -1;
public bool Available { get; init; }
public string Title { get; init; } = "";
public string Name { get; init; } = "";
public double Value { get; init; }
public byte Unit { get; init; }
public string ValueText { get; init; } = "";
public string Formula { get; init; } = "";
public List Terms { get; init; } = [];
public List Notes { get; init; } = [];
/// Chiave della cache del client: soggetto e argomenti.
public (byte Subject, int Target, int Arg) Key => (Subject, Target, Arg);
public void Write(ref SpanWriter w)
{
w.I64(Day); w.U8(Subject); w.I32(Target); w.I32(Arg); w.Bool(Available);
w.String(Title); w.String(Name); w.F64(Value); w.U8(Unit); w.String(ValueText); w.String(Formula);
w.Count(Terms.Count);
foreach (var t in Terms) t.Write(ref w);
w.Count(Notes.Count);
foreach (var n in Notes) w.String(n);
w.Flags(0);
}
public static ExplanationReport Read(ref SpanReader r)
{
long day = r.I64();
byte subject = r.U8();
int target = r.I32(), arg = r.I32();
bool available = r.Bool();
string title = r.String(), name = r.String();
double value = r.F64();
byte unit = r.U8();
string valueText = r.String(), formula = r.String();
var terms = new List();
for (int n = r.Count(), i = 0; i < n; i++) terms.Add(ExplanationTermInfo.Read(ref r));
var notes = new List();
for (int n = r.Count(), i = 0; i < n; i++) notes.Add(r.String());
var m = new ExplanationReport
{
Day = day, Subject = subject, Target = target, Arg = arg, Available = available, Title = title, Name = name,
Value = value, Unit = unit, ValueText = valueText, Formula = formula, Terms = terms, Notes = notes,
};
r.Flags();
return m;
}
}