using Microsoft.CommandPalette.Extensions.Toolkit;

namespace Olive.Helpers;

internal sealed class SettingsManager : JsonSettingsManager
{
  private const string Namespace = "Olive";

  public SettingsManager()
  {
    FilePath = SettingsJsonPath();
    Settings.Add(_klipyApiKey);
    Settings.Add(_resultCount);

    // Load settings from file upon initialization
    LoadSettings();

    Settings.SettingsChanged += (_, _) => SaveSettings();
  }

  public string SettingsPath => FilePath;

  private readonly TextSetting _klipyApiKey = new(
    Namespaced(nameof(KlipyApiKey)),
    "Klipy API key",
    "Private key used to call the Klipy API. It stays stored locally in Olive settings.",
    string.Empty)
  {
    Placeholder = "Required",
    IsRequired = true,
    ErrorMessage = "Klipy API key is required."
  };

  private readonly IntegerSetting _resultCount = new(
    Namespaced(nameof(ResultCount)),
    "Result count",
    "Number of GIF results to load for each search.",
    50,
    20,
    100)
  {
    Placeholder = "Min 20, max 100",
    IsRequired = true,
    ErrorMessage = "Enter a number between 20 and 100."
  };

  public string KlipyApiKey
  {
    get
    {
      LoadSettings();
      return _klipyApiKey.Value ?? string.Empty;
    }
  }

  public bool HasKlipyApiKey => !string.IsNullOrWhiteSpace(KlipyApiKey);

  public int ResultCount
  {
    get
    {
      LoadSettings();
      return Math.Clamp(_resultCount.Value, 30, 100);
    }
  }

  private static string SettingsJsonPath()
  {
    var directory = Utilities.BaseSettingsPath("Olive");
    Directory.CreateDirectory(directory);

    return Path.Combine(directory, "settings.json");
  }

  private static string Namespaced(string propertyName)
  {
    return $"{Namespace}.{propertyName}";
  }
}
