using Minint.Core.Models;
using Minint.Infrastructure.Serialization;

namespace Minint.Tests;

public class SerializerTests
{
    private readonly MinintSerializer _serializer = new();

    [Fact]
    public void RoundTrip_EmptyDocument_PreservesStructure()
    {
        var container = new MinintContainer(32, 16);
        container.AddNewDocument("Doc1");

        var result = RoundTrip(container);

        Assert.Equal(32, result.Width);
        Assert.Equal(16, result.Height);
        Assert.Single(result.Documents);
        Assert.Equal("Doc1", result.Documents[0].Name);
        Assert.Single(result.Documents[0].Layers);
        Assert.Equal(32 * 16, result.Documents[0].Layers[0].Pixels.Length);
    }

    [Fact]
    public void RoundTrip_MultipleDocuments_PreservesAll()
    {
        var container = new MinintContainer(8, 8);
        var doc1 = container.AddNewDocument("Frame1");
        doc1.FrameDelayMs = 200;
        var doc2 = container.AddNewDocument("Frame2");
        doc2.FrameDelayMs = 500;

        var result = RoundTrip(container);

        Assert.Equal(2, result.Documents.Count);
        Assert.Equal("Frame1", result.Documents[0].Name);
        Assert.Equal(200u, result.Documents[0].FrameDelayMs);
        Assert.Equal("Frame2", result.Documents[1].Name);
        Assert.Equal(500u, result.Documents[1].FrameDelayMs);
    }

    [Fact]
    public void RoundTrip_PaletteAndPixels_Preserved()
    {
        var container = new MinintContainer(4, 4);
        var doc = container.AddNewDocument("Test");
        var red = new RgbaColor(255, 0, 0, 255);
        doc.EnsureColorCached(red);

        var layer = doc.Layers[0];
        layer.Pixels[0] = 1; // red

        var result = RoundTrip(container);
        var rdoc = result.Documents[0];

        Assert.Equal(2, rdoc.Palette.Count); // transparent + red
        Assert.Equal(RgbaColor.Transparent, rdoc.Palette[0]);
        Assert.Equal(red, rdoc.Palette[1]);
        Assert.Equal(1, rdoc.Layers[0].Pixels[0]);
        Assert.Equal(0, rdoc.Layers[0].Pixels[1]);
    }

    [Fact]
    public void RoundTrip_LayerProperties_Preserved()
    {
        var container = new MinintContainer(2, 2);
        var doc = container.AddNewDocument("Test");
        doc.Layers[0].Name = "Background";
        doc.Layers[0].IsVisible = false;
        doc.Layers[0].Opacity = 128;

        var result = RoundTrip(container);
        var layer = result.Documents[0].Layers[0];

        Assert.Equal("Background", layer.Name);
        Assert.False(layer.IsVisible);
        Assert.Equal(128, layer.Opacity);
    }

    [Fact]
    public void RoundTrip_LargePalette_Uses2ByteIndices()
    {
        var container = new MinintContainer(2, 2);
        var doc = container.AddNewDocument("BigPalette");

        for (int i = 0; i < 300; i++)
            doc.EnsureColorCached(new RgbaColor((byte)(i % 256), (byte)(i / 256), 0, 255));

        Assert.Equal(2, doc.IndexByteWidth);

        int lastIdx = doc.Palette.Count - 1;
        doc.Layers[0].Pixels[0] = lastIdx;

        var result = RoundTrip(container);
        Assert.Equal(lastIdx, result.Documents[0].Layers[0].Pixels[0]);
    }

    [Fact]
    public void Read_InvalidSignature_Throws()
    {
        var ms = new MemoryStream("BADDATA"u8.ToArray());
        Assert.Throws<InvalidDataException>(() => _serializer.Read(ms));
    }

    [Fact]
    public void Read_TruncatedStream_Throws()
    {
        var ms = new MemoryStream("MINI"u8.ToArray());
        Assert.Throws<InvalidDataException>(() => _serializer.Read(ms));
    }

    private MinintContainer RoundTrip(MinintContainer container)
    {
        var ms = new MemoryStream();
        _serializer.Write(ms, container);
        ms.Position = 0;
        return _serializer.Read(ms);
    }
}
