using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using DodoSSH.Client.Shell.ViewModels;

namespace DodoSSH.Client.App.Views;

/// <summary>
/// Importing hosts from <c>~/.ssh/config</c>.
/// </summary>
/// <remarks>
/// A task rather than a destination, which is why it is reached from preferences and not from the nav rail.
/// </remarks>
internal sealed partial class ImportScreen : UserControl
{
    public ImportScreen()
    {
        InitializeComponent();

        // The count on the import button is derived from the ticks, and a CheckBox bound with
        // {Binding IsSelected} tells its own row and nothing else. Rather than have every row hold a
        // reference back to the screen, the screen listens for the event they all bubble.
        AddHandler(ToggleButton.IsCheckedChangedEvent, OnTickChanged, RoutingStrategies.Bubble);
    }

    /// <summary>Where the keyboard lands when this screen is the one showing.</summary>
    internal IInputElement KeyboardTarget => this;

    private void OnTickChanged(object? sender, RoutedEventArgs e)
    {
        if (DataContext is ImportViewModel import)
        {
            import.NoteSelectionChanged();
        }
    }
}
