using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;

using DodoSSH.Client.Shell.ViewModels;

namespace DodoSSH.Client.Android.Views;

/// <summary>Designs 06 and 08 — one remote pane, over SFTP or over a bucket.</summary>
internal sealed partial class FilesScreen : UserControl
{
    public FilesScreen() => AvaloniaXamlLoader.Load(this);

    /// <summary>
    /// Opens a directory on a single tap.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The desktop pane opens on a double tap and on Enter. A phone has neither, so the gesture has to be
    /// the one gesture there is — and that is why this is here rather than in the shared view model: making
    /// selection navigate would give the desktop a single click that walks into a directory.
    /// </para>
    /// <para>
    /// <see cref="InputElement.Tapped"/> rather than a command on the row, because the row must stay a
    /// plain <see cref="Border"/>: a button as the item template swallows the press before the list sees it
    /// — it marks the pointer event handled, and the list's own handler is not registered for handled
    /// events — leaving nothing selected and every command that reads the selection doing nothing. Tapped
    /// fires after the list has moved its selection, which is what lets this read it.
    /// </para>
    /// <para>
    /// A file is left selected rather than opened. There is nothing this head could do with it — see the
    /// note about the document picker at the top of the screen — and the actions below act on the selection.
    /// </para>
    /// </remarks>
    private void OnRemoteEntryTapped(object? sender, TappedEventArgs e)
    {
        if (DataContext is TransfersViewModel { SelectedRemoteEntry.IsNavigable: true } transfers)
        {
            transfers.OpenRemoteCommand.Execute(null);
        }
    }
}
