// PRE-GENERATED (design session). Preset library window code-behind.
using System;
using System.Windows;
using DansoriEQ.Core.Presets;

namespace DansoriEQ.App;

public partial class PresetLibraryWindow : Wpf.Ui.Controls.FluentWindow
{
    private readonly PresetLibrary _lib;
    private readonly Func<EqPreset> _capture;
    private readonly Action<EqPreset> _apply;

    public PresetLibraryWindow(PresetLibrary lib, Func<EqPreset> capture, Action<EqPreset> apply)
    {
        InitializeComponent();
        _lib = lib;
        _capture = capture;
        _apply = apply;
        Refresh();
    }

    private void Refresh() => PresetList.ItemsSource = _lib.List();

    private void Save_Click(object sender, RoutedEventArgs e)
    {
        var name = TxtName.Text?.Trim();
        if (string.IsNullOrEmpty(name)) return;
        _lib.Save(name, _capture());
        TxtName.Clear();
        Refresh();
    }

    private void Load_Click(object sender, RoutedEventArgs e)
    {
        if (PresetList.SelectedItem is string name)
        {
            _apply(_lib.Load(name));
            Close();
        }
    }

    private void Delete_Click(object sender, RoutedEventArgs e)
    {
        if (PresetList.SelectedItem is string name)
        {
            _lib.Delete(name);
            Refresh();
        }
    }

    private void Close_Click(object sender, RoutedEventArgs e) => Close();
}
