using System.Text.Json;
using FrameProcessor.Domain;

namespace FrameProcessor.Tests;

public class DitherAlgorithmTests
{
    [Theory]
    [InlineData("\"floyd-steinberg\"", DitherAlgorithm.FloydSteinberg)]
    [InlineData("\"atkinson\"", DitherAlgorithm.Atkinson)]
    [InlineData("\"stucki\"", DitherAlgorithm.Stucki)]
    [InlineData("\"jarvis\"", DitherAlgorithm.Jarvis)]
    public void JsonDeserialize_KebabCaseLowercase(string json, DitherAlgorithm expected)
    {
        var value = JsonSerializer.Deserialize<DitherAlgorithm>(json);
        Assert.Equal(expected, value);
    }

    [Theory]
    [InlineData(DitherAlgorithm.FloydSteinberg, "\"floyd-steinberg\"")]
    [InlineData(DitherAlgorithm.Atkinson, "\"atkinson\"")]
    [InlineData(DitherAlgorithm.Stucki, "\"stucki\"")]
    [InlineData(DitherAlgorithm.Jarvis, "\"jarvis\"")]
    public void JsonSerialize_KebabCaseLowercase(DitherAlgorithm value, string expected)
    {
        var json = JsonSerializer.Serialize(value);
        Assert.Equal(expected, json);
    }

    [Theory]
    [InlineData("\"FloydSteinberg\"")]
    [InlineData("\"floyd_steinberg\"")]
    [InlineData("\"FLOYD-STEINBERG\"")]
    [InlineData("\"sierra\"")]
    [InlineData("\"\"")]
    public void JsonDeserialize_RejectsInvalid(string json)
    {
        Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<DitherAlgorithm>(json));
    }

    [Fact]
    public void JsonRoundTrip_PreservesValue()
    {
        foreach (var value in new[]
        {
            DitherAlgorithm.FloydSteinberg,
            DitherAlgorithm.Atkinson,
            DitherAlgorithm.Stucki,
            DitherAlgorithm.Jarvis,
        })
        {
            var json = JsonSerializer.Serialize(value);
            var roundTripped = JsonSerializer.Deserialize<DitherAlgorithm>(json);
            Assert.Equal(value, roundTripped);
        }
    }
}
