using Metin2Launcher.Formats;
using Metin2Launcher.Manifest;
using Xunit;
using ManifestDto = Metin2Launcher.Manifest.Manifest;

namespace Metin2Launcher.Tests;

public class LegacyJsonBlobFormatTests
{
    private static ManifestDto SampleManifest() => new()
    {
        Version = "v1",
        Launcher = new ManifestLauncherEntry { Path = "Metin2Launcher.exe", Sha256 = "x", Size = 1 },
        Files =
        {
            new ManifestFile { Path = "a.pck", Sha256 = "h1", Size = 10 },
            new ManifestFile { Path = "Metin2.exe", Sha256 = "h2", Size = 20, Platform = "windows" },
            new ManifestFile { Path = "linux-bin", Sha256 = "h3", Size = 30, Platform = "linux" },
        },
    };

    [Fact]
    public void Name_is_legacy_json_blob()
    {
        Assert.Equal("legacy-json-blob", new LegacyJsonBlobFormat().Name);
    }

    [Fact]
    public void FilterApplicable_windows_platform()
    {
        var format = new LegacyJsonBlobFormat();
        var filtered = format.FilterApplicable(SampleManifest(), "windows");
        Assert.Equal(2, filtered.Count);
        Assert.Contains(filtered, f => f.Path == "a.pck");
        Assert.Contains(filtered, f => f.Path == "Metin2.exe");
    }

    [Fact]
    public void FilterApplicable_linux_platform()
    {
        var format = new LegacyJsonBlobFormat();
        var filtered = format.FilterApplicable(SampleManifest(), "linux");
        Assert.Equal(2, filtered.Count);
        Assert.Contains(filtered, f => f.Path == "a.pck");
        Assert.Contains(filtered, f => f.Path == "linux-bin");
    }

    [Fact]
    public void OnApplied_is_noop()
    {
        var outcome = new ReleaseOutcome();
        var tmp = Path.Combine(Path.GetTempPath(), "lg-" + Guid.NewGuid());
        Directory.CreateDirectory(tmp);
        try
        {
            new LegacyJsonBlobFormat().OnApplied(
                tmp,
                new LoadedManifest(Array.Empty<byte>(), Array.Empty<byte>(), SampleManifest()),
                outcome);
            Assert.Null(outcome.RuntimeKey);
        }
        finally { Directory.Delete(tmp, true); }
    }
}
