using Avalonia.Media.Imaging;
using Ehwrj.Core.Models;

namespace Ehwrj.App.Models;

public sealed class LiveSnapshot
{
    private static readonly IReadOnlyList<EndpointHealth> EmptyEndpointHealth =
    [
        new("map_info.json", Required: true, EndpointHealthState.NotChecked, "not checked"),
        new("map_obj.json", Required: true, EndpointHealthState.NotChecked, "not checked"),
        new("map.img", Required: true, EndpointHealthState.NotChecked, "not checked"),
        new("state", Required: false, EndpointHealthState.NotChecked, "not checked"),
        new("hudmsg", Required: false, EndpointHealthState.NotChecked, "not checked"),
        new("gamechat", Required: false, EndpointHealthState.NotChecked, "not checked")
    ];

    public static LiveSnapshot Empty { get; } = new(
        MapInfo.Empty,
        Array.Empty<MapObject>(),
        FlightState.Empty,
        Array.Empty<BattleMessage>(),
        null,
        DateTimeOffset.MinValue,
        false,
        "Waiting for War Thunder",
        EmptyEndpointHealth);

    public LiveSnapshot(
        MapInfo mapInfo,
        IReadOnlyList<MapObject> objects,
        FlightState flightState,
        IReadOnlyList<BattleMessage> messages,
        Bitmap? mapImage,
        DateTimeOffset updatedAt,
        bool isGameRunning,
        string status,
        IReadOnlyList<EndpointHealth>? endpointHealth = null)
    {
        MapInfo = mapInfo;
        Objects = objects;
        FlightState = flightState;
        Messages = messages;
        MapImage = mapImage;
        UpdatedAt = updatedAt;
        IsGameRunning = isGameRunning;
        Status = status;
        EndpointHealth = endpointHealth ?? EmptyEndpointHealth;
    }

    public MapInfo MapInfo { get; }
    public IReadOnlyList<MapObject> Objects { get; }
    public FlightState FlightState { get; }
    public IReadOnlyList<BattleMessage> Messages { get; }
    public Bitmap? MapImage { get; }
    public DateTimeOffset UpdatedAt { get; }
    public bool IsGameRunning { get; }
    public string Status { get; }
    public IReadOnlyList<EndpointHealth> EndpointHealth { get; }
    public MapObject? Player => Objects.FirstOrDefault(static o => o.IsPlayer) ?? Objects.FirstOrDefault(static o => o.IsAircraft);
}
