namespace JSMR.Worker.UI;

using Spectre.Console;

public static class CliUi
{
    public static void PageHeader(int page, int? endPage)
    {
        AnsiConsole.MarkupLine($"[grey]Scanning page[/] [bold]{page}[/] [grey]of[/] [bold]{endPage}[/][grey]...[/]");
    }

    public static void PageHighlights(int newUpcoming, int newRelease)
    {
        if (newUpcoming == 0 && newRelease == 0)
            return;

        // Nice little boxed summary
        var grid = new Grid().AddColumn().AddColumn();

        if (newUpcoming > 0)
            grid.AddRow("[yellow]Upcoming[/]", $"[yellow]{newUpcoming}[/]");
        if (newRelease > 0)
            grid.AddRow("[green]New releases[/]", $"[green]{newRelease}[/]");

        var panel = new Panel(grid)
            .Header("[bold]Page updates[/]")
            .Border(BoxBorder.Rounded)
            .Padding(1, 0, 1, 0);

        AnsiConsole.Write(panel);
    }

    public static void PageErrors(string productId, string[] messages)
    {
        if (messages.Length == 0)
            return;

        var grid = new Grid().AddColumn();

        foreach (string message in messages )
        {
            grid.AddRow($"[red]{Escape(message)}[/]");
        }

        var panel = new Panel(grid)
            .Header($"[bold]{productId}[/]")
            .Border(BoxBorder.Rounded)
            .Padding(1, 0, 1, 0);

        AnsiConsole.Write(panel);
    }

    public static void Information(string message) =>
        AnsiConsole.MarkupLine($"[blue]🛈 {Escape(message)}[/]");

    public static void Warning(string message) =>
        AnsiConsole.MarkupLine($"[yellow]⚠ {Escape(message)}[/]");

    public static void Error(string message) =>
        AnsiConsole.MarkupLine($"[red]✖ {Escape(message)}[/]");

    private static string Escape(string s) => Markup.Escape(s);
}