using Microsoft.CommandPalette.Extensions.Toolkit;

namespace Olive.Helpers;

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

    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 = "Paste your Klipy API key here",
    };

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

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

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

    public string SettingsPath => FilePath;

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

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

    public SettingsManager()
    {
        FilePath = SettingsJsonPath();

        Settings.Add(_klipyApiKey);

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

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