using System.Globalization;
using SharepointToolbox.Views.Converters;

namespace SharepointToolbox.Tests.Converters;

[Trait("Category", "Unit")]
public class Base64ToImageSourceConverterTests
{
    private readonly Base64ToImageSourceConverter _converter = new();

    [Fact]
    public void Convert_NullValue_ReturnsNull()
    {
        var result = _converter.Convert(null, typeof(object), null, CultureInfo.InvariantCulture);
        Assert.Null(result);
    }

    [Fact]
    public void Convert_EmptyString_ReturnsNull()
    {
        var result = _converter.Convert(string.Empty, typeof(object), null, CultureInfo.InvariantCulture);
        Assert.Null(result);
    }

    [Fact]
    public void Convert_NonStringValue_ReturnsNull()
    {
        var result = _converter.Convert(42, typeof(object), null, CultureInfo.InvariantCulture);
        Assert.Null(result);
    }

    [Fact]
    public void Convert_MalformedString_NoBase64Marker_ReturnsNull()
    {
        var result = _converter.Convert("not-a-data-uri", typeof(object), null, CultureInfo.InvariantCulture);
        Assert.Null(result);
    }

    [Fact]
    public void Convert_InvalidBase64AfterMarker_ReturnsNull()
    {
        // Has the marker but invalid base64 content — should not throw
        var result = _converter.Convert("data:image/png;base64,!!!invalid!!!", typeof(object), null, CultureInfo.InvariantCulture);
        Assert.Null(result);
    }

    [Fact]
    public void ConvertBack_ThrowsNotImplementedException()
    {
        Assert.Throws<NotImplementedException>(() =>
            _converter.ConvertBack(null, typeof(object), null, CultureInfo.InvariantCulture));
    }
}
