using System.Globalization;
using System.Text.Json;
using Anubis.Core;
using Anubis.Demo;
using Anubis.Platform;
namespace Anubis.Server;
/// What the top bar shows and the WebSocket sends: state of the bot, mode, currency, how fresh the data are.
internal sealed record ShellSnapshot(
string Mode,
string Bot,
string BotLabel,
string BotShort,
bool RealMoney,
string Currency,
string Theme,
string Now,
int AgeSeconds,
bool Demo,
string Version);
/// Reads the shell state once per request; also the money formatter with the chosen display currency.
internal sealed class ShellData(AnubisHost host, IClock clock)
{
private decimal? _demoRate;
private int? _demoAge;
public bool IsDemo => host.IsDemo;
public TimeZoneInfo Zone => host.Config.TimeZone;
public DateTimeOffset Now => clock.UtcNow;
public async Task SnapshotAsync(CancellationToken cancellationToken)
{
var services = host.Services;
var bot = "active";
var currency = "EUR";
var theme = "auto";
if (services is not null)
{
bot = (await services.Settings.GetAsync(Core.SettingsStore.BotState.Key, cancellationToken).ConfigureAwait(false)).Value;
currency = (await services.Settings.GetAsync(Core.SettingsStore.Currency.Key, cancellationToken).ConfigureAwait(false)).Value;
theme = (await services.Settings.GetAsync(Core.SettingsStore.Theme.Key, cancellationToken).ConfigureAwait(false)).Value;
}
var (label, shortLabel) = bot switch
{
"paused" => ("Bot in pausa", "In pausa"),
"stopped" => ("Bot fermo", "Fermo"),
_ => ("Bot attivo", "Attivo"),
};
var age = DemoAge() ?? (host.LastCheckUtc is { } last ? (int)Math.Max(0, (clock.UtcNow - last).TotalSeconds) : 0);
return new ShellSnapshot(host.Status.Mode.ToString(), bot, label, shortLabel, RealMoney: false, currency, theme,
Format.LongDateTime(clock.UtcNow, Zone), age, host.IsDemo, AnubisInfo.Version);
}
///
/// An amount in euro shown in the display currency. Exchange rates arrive with the market data (phase 2): until
/// then dollars appear only in demo mode, with the rate of appendix F, and otherwise amounts stay in euro.
///
public string Money(decimal euro, string currency, bool withSign = false, int decimals = 2) =>
Format.Money(Convert(euro, currency), Effective(currency), withSign, decimals);
public decimal Convert(decimal euro, string currency) => currency == "USD" && DemoRate() is { } rate ? euro * rate : euro;
/// The display currency that can honour.
public string Effective(string currency) => currency == "USD" && DemoRate() is not null ? "USD" : "EUR";
private decimal? DemoRate()
{
if (!host.IsDemo)
{
return null;
}
if (_demoRate is null && Aggregates() is { } doc)
{
_demoRate = decimal.Parse(doc.RootElement.GetProperty("cambio_eur_usd").GetString()!, CultureInfo.InvariantCulture);
}
return _demoRate;
}
private int? DemoAge()
{
if (!host.IsDemo)
{
return null;
}
if (_demoAge is null && Aggregates() is { } doc)
{
_demoAge = doc.RootElement.GetProperty("dati_aggiornati_secondi").GetInt32();
}
return _demoAge;
}
/// The aggregates of appendix F; only in demo mode (SPEC 13.1).
public JsonDocument? Aggregates() => host.IsDemo ? DemoMode.ReadAggregates(host.Config.Data) : null;
public PlatformKind Platform => host.Config.Platform;
}