// PRE-GENERATED (design session). Local library of saved .tweq presets (Peace-style 1-click switch).
using System.Collections.Generic;
using System.IO;

namespace DansoriEQ.Core.Presets;

public sealed class PresetLibrary
{
    private readonly string _dir;

    public PresetLibrary(string directory)
    {
        _dir = directory;
        Directory.CreateDirectory(_dir);
    }

    public IReadOnlyList<string> List()
    {
        var names = new List<string>();
        foreach (var f in Directory.GetFiles(_dir, "*.tweq"))
            names.Add(Path.GetFileNameWithoutExtension(f));
        names.Sort();
        return names;
    }

    public void Save(string name, EqPreset preset)
        => File.WriteAllText(PathFor(name), PresetSerializer.Serialize(preset));

    public EqPreset Load(string name)
        => PresetSerializer.Deserialize(File.ReadAllText(PathFor(name)));

    public void Delete(string name)
    {
        var p = PathFor(name);
        if (File.Exists(p)) File.Delete(p);
    }

    private string PathFor(string name)
    {
        foreach (var c in Path.GetInvalidFileNameChars())
            name = name.Replace(c, '_');
        return Path.Combine(_dir, name + ".tweq");
    }
}
