using Microsoft.Iris.DecompXml;
using Microsoft.Iris.Markup;
using UIX.Test.Fixtures;
using Xunit.Abstractions;

namespace UIX.Test;

[Collection(MarkupSystemCollection.Name)]
public class DecompileControlFlow
{
    private readonly ITestOutputHelper _output;

    public DecompileControlFlow(ITestOutputHelper output, MarkupSystemFixture markupSystemFixture)
    {
        _output = output;
        markupSystemFixture.SetupDebug(output);
    }

    [Theory]
    [InlineData("cfa_if-elseif-else01")]
    [InlineData("cfa_if-elseif-else02")]
    [InlineData("cfa_if-elseif-else03")]
    [InlineData("cfa_if-elseif-else04")]
    [InlineData("cfa_if-elseif-else05")]
    public async Task IfElseIfElse(string testId) => await TestDecompile(testId);

    [Theory]
    [InlineData("cfa_foreach01")]
    [InlineData("cfa_foreach02")]
    [InlineData("cfa_foreach03")]
    [InlineData("cfa_foreach04")]
    [InlineData("cfa_foreach05")]
    [InlineData("cfa_foreach06")]
    [InlineData("cfa_foreach07")]
    public async Task Foreach(string testId) => await TestDecompile(testId);

    private async Task TestDecompile(string fileNameWithoutExtension)
    {
        using TempFile uixFile = new($"{fileNameWithoutExtension}.uix", ".uix");
        await uixFile.InitAsync();

        var sourceLoadResult = MarkupSystem.ResolveLoadResult($"file://{uixFile.Path}", MarkupSystem.RootIslandId);
        var decompiler = Decompiler.Load(sourceLoadResult);
        var uixDocAc = decompiler.Decompile();

        var uixScriptAc = uixDocAc.Descendants()
            .Where(e => e.Name.LocalName == "Script")
            .FirstOrDefault()?.Value;
        Assert.NotNull(uixScriptAc);
        _output.WriteLine(uixScriptAc);
    }
}
