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 static ListItem CreateGifItem(KlipyGif gif)
  {
    return new ListItem(new CopyGifCommand(gif))
    {
      Title = string.Empty,
      Subtitle = string.Empty,
      Icon = new IconInfo(gif.ThumbnailGifUrl.ToString())
    };
  }

  public CommandItem InitialContent()
  {
    return new CommandItem(new NoOpCommand())
    {
      Title = _settingsManager.HasKlipyApiKey ? "Search GIFs" : "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
    };
  }

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

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

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

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