using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using ClawdDotNet.Desktop.ViewModels;

namespace ClawdDotNet.Desktop.Views;

public partial class RestoreBackupWindow : Window
{
    public RestoreBackupWindow()
    {
        InitializeComponent();
    }

    protected override void OnDataContextChanged(EventArgs e)
    {
        base.OnDataContextChanged(e);
        if (DataContext is RestoreBackupViewModel vm)
        {
            vm.CloseRequested += () => Close();
        }
    }

    private async void OnBrowseFolder(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not RestoreBackupViewModel vm) return;

        var storage = StorageProvider;
        if (storage is null) return;

        var folders = await storage.OpenFolderPickerAsync(new FolderPickerOpenOptions
        {
            Title = "Zielordner für Wiederherstellung wählen",
            AllowMultiple = false
        });

        if (folders.Count > 0 && folders[0].TryGetLocalPath() is { } path)
        {
            vm.TargetDirectory = path;
        }
    }
}
