using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;

namespace DodoSSH.Client.App.Views;

/// <summary>Settings mode's own titlebar — see the remark in the markup for why it is not TitleBar itself.</summary>
internal sealed partial class SettingsTitleBar : UserControl
{
    public SettingsTitleBar() => InitializeComponent();

    private Window? Host => TopLevel.GetTopLevel(this) as Window;

    /// <summary>Left button only, and only on a press nothing inside the bar has already handled.</summary>
    private void OnDrag(object? sender, PointerPressedEventArgs e)
    {
        if (e.Handled || !e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
        {
            return;
        }

        Host?.BeginMoveDrag(e);
    }

    private void OnMinimise(object? sender, RoutedEventArgs e)
    {
        if (Host is { } window)
        {
            window.WindowState = WindowState.Minimized;
        }
    }

    /// <summary>Both the button and a double-click on the bar arrive here, as Windows convention expects.</summary>
    private void OnToggleMaximised(object? sender, RoutedEventArgs e)
    {
        if (Host is not { } window)
        {
            return;
        }

        window.WindowState = window.WindowState == WindowState.Maximized
            ? WindowState.Normal
            : WindowState.Maximized;
    }

    private void OnClose(object? sender, RoutedEventArgs e) => Host?.Close();
}
