using RioJoy.Core.Overlay;
using Xunit;

namespace RioJoy.Core.Tests.Overlay;

public class OverlayTemplateStoreTests
{
    [Fact]
    public void RoundTrips_GeometryAndOverrides()
    {
        var template = new OverlayTemplate
        {
            BaseImagePath = "cockpit.png",
            Width = 1920,
            Height = 1080,
            Regions =
            {
                new OverlayRegion { Name = "b-00", X = 10, Y = 20, Width = 80, Height = 24, FontFamily = "Consolas", FontSizePx = 18 },
                new OverlayRegion { Name = "b-41", X = 5, Y = 7, Width = 60, Height = 30, HJust = OverlayHJust.Right, VJust = OverlayVJust.Bottom },
            },
        };

        OverlayTemplate back = OverlayTemplateStore.Deserialize(OverlayTemplateStore.Serialize(template));

        Assert.Equal(1920, back.Width);
        Assert.Equal(2, back.Regions.Count);

        OverlayRegion? r0 = back.FindRegion("b-00");
        Assert.NotNull(r0);
        Assert.Equal(80, r0!.Width, 6);
        Assert.Equal("Consolas", r0.FontFamily);
        Assert.Null(r0.HJust);

        OverlayRegion? r1 = back.FindRegion("B-41"); // case-insensitive lookup
        Assert.NotNull(r1);
        Assert.Equal(OverlayHJust.Right, r1!.HJust);
        Assert.Equal(OverlayVJust.Bottom, r1.VJust);
    }

    [Fact]
    public void Serialize_WritesEnumsAsStrings()
    {
        var template = new OverlayTemplate
        {
            Regions = { new OverlayRegion { Name = "x", HJust = OverlayHJust.Center } },
        };

        string json = OverlayTemplateStore.Serialize(template);

        Assert.Contains("\"Center\"", json);
        Assert.DoesNotContain("\"HJust\": 1", json);
    }
}
