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

namespace DodoSSH.Client.App.Views;

/// <summary>The window's destinations, down the left edge — the switcher, the six rail rows and the user chip.</summary>
internal sealed partial class NavRail : UserControl
{
    public NavRail() => InitializeComponent();

    /// <summary>Opens the user popover.</summary>
    /// <remarks>
    /// A handler rather than relying on the click that opening a <c>Flyout</c> answers to on its own: a
    /// named method is a thing a test can call directly, where an implicit open is not.
    /// </remarks>
    private void OnUserChipPressed(object? sender, RoutedEventArgs e)
    {
        if (sender is Control chip)
        {
            FlyoutBase.ShowAttachedFlyout(chip);
        }
    }

    /// <summary>Hides the popover, whatever handler is about to navigate.</summary>
    private void ClosePopover()
    {
        if (this.FindControl<Button>("UserChip") is { } chip)
        {
            FlyoutBase.GetAttachedFlyout(chip)?.Hide();
        }
    }

    /// <summary>Leaves for the vaults screen with the new-vault form open, shutting the popover behind it.</summary>
    /// <remarks>
    /// The popover is closed first: the command navigates, and a popup left open would be hanging over a
    /// screen it has nothing to do with. A <c>Flyout</c> does not close on its own when something inside it
    /// is pressed — which is what the vault switches above it want, and not what this wants.
    /// </remarks>
    private void OnPopoverNewVaultPressed(object? sender, RoutedEventArgs e)
    {
        ClosePopover();

        if (DataContext is MainWindowViewModel shell)
        {
            shell.ShowNewVaultCommand.Execute(null);
        }
    }

    /// <summary>Opens settings mode on its default landing page — see the remark in the markup.</summary>
    private void OnPopoverSettingsPressed(object? sender, RoutedEventArgs e)
    {
        ClosePopover();

        if (DataContext is MainWindowViewModel shell)
        {
            shell.EnterSettingsCommand.Execute(SettingsPage.General);
        }
    }

    /// <summary>Opens settings mode on its Preferences page.</summary>
    private void OnPopoverPreferencesPressed(object? sender, RoutedEventArgs e)
    {
        ClosePopover();

        if (DataContext is MainWindowViewModel shell)
        {
            shell.EnterSettingsCommand.Execute(SettingsPage.Preferences);
        }
    }

    /// <summary>Opens settings mode on its Vaults page.</summary>
    private void OnPopoverVaultsPressed(object? sender, RoutedEventArgs e)
    {
        ClosePopover();

        if (DataContext is MainWindowViewModel shell)
        {
            shell.EnterSettingsCommand.Execute(SettingsPage.Vaults);
        }
    }

    /// <summary>
    /// Starts a sign-out, through the Account settings page so the confirmation card has somewhere to be
    /// seen — see <see cref="MainWindowViewModel.SignOutFromPopover"/>.
    /// </summary>
    private void OnPopoverLogoutPressed(object? sender, RoutedEventArgs e)
    {
        ClosePopover();

        if (DataContext is MainWindowViewModel shell)
        {
            shell.SignOutFromPopoverCommand.Execute(null);
        }
    }
}
