using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Olive.Commands;
using Olive.Helpers;
using Olive.Klipy;

namespace Olive.Pages;

internal sealed class GifListItemFactory
{
  private readonly IIconInfo _icon;
  private readonly SettingsManager _settingsManager;

  public GifListItemFactory(IIconInfo icon, SettingsManager settingsManager)
  {
    _icon = icon;
    _settingsManager = settingsManager;
  }

  public ListItem CreateGifItem(KlipyGif gif)
  {
    return new ListItem(new CopyGifCommand(gif, _settingsManager))
    {
      Title = string.Empty,
      Subtitle = string.Empty,
      Icon = new IconInfo(gif.ThumbnailGifUrl.ToString()),
      MoreCommands = CommonMoreCommands()
    };
  }

  public CommandItem InitialContent()
  {
    return new CommandItem(_settingsManager.HasKlipyApiKey ? new NoOpCommand() : _settingsManager.Settings.SettingsPage)
    {
      Title = _settingsManager.HasKlipyApiKey ? "Search GIFs from Klipy.com" : "Klipy API key missing",
      Subtitle = _settingsManager.HasKlipyApiKey
        ? "Type something like thanks, excited, cat, or confused."
        : "Open Olive settings and set the Klipy API key.",
      Icon = _icon,
      MoreCommands = CommonMoreCommands()
    };
  }

  public CommandItem LoadingContent(string search)
  {
    return new CommandItem(new NoOpCommand())
    {
      Title = "Loading GIFs...",
      Subtitle = $"Searching Klipy for \"{search}\"",
      Icon = _icon,
      MoreCommands = CommonMoreCommands()
    };
  }

  public CommandItem MissingApiKeyContent()
  {
    return new CommandItem(_settingsManager.Settings.SettingsPage)
    {
      Title = "Klipy API key missing",
      Subtitle = "Open Olive settings and paste your Klipy API key.",
      Icon = _icon,
      MoreCommands = CommonMoreCommands()
    };
  }

  public CommandItem NoResultsContent(string search)
  {
    return new CommandItem(new NoOpCommand())
    {
      Title = "No GIF found",
      Subtitle = $"Try another search for \"{search}\".",
      Icon = _icon,
      MoreCommands = CommonMoreCommands()
    };
  }

  public CommandItem SearchErrorContent(Exception ex)
  {
    return new CommandItem(new NoOpCommand())
    {
      Title = "Search failed",
      Subtitle = ex.Message,
      Icon = _icon,
      MoreCommands = CommonMoreCommands()
    };
  }

  private IContextItem[] CommonMoreCommands()
  {
    return
    [
      new CommandContextItem(_settingsManager.Settings.SettingsPage)
      {
        Title = "Olive Settings",
        Subtitle = "Configure Olive.",
        Icon = new IconInfo("\uE713")
      },
      LinkCommand("Visit Klipy", "Open klipy.com", "https://klipy.com", "\uE774"),
      LinkCommand("Source code", "Open the Olive repository", "https://git.endmove.eu/EndMove/Olive", "\uE943")
    ];
  }

  private static CommandContextItem LinkCommand(string title, string subtitle, string url, string icon)
  {
    return new CommandContextItem(new OpenUrlCommand(url) { Result = CommandResult.Dismiss() })
    {
      Title = title,
      Subtitle = subtitle,
      Icon = new IconInfo(icon)
    };
  }
}
