using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;

namespace GsaEditor.Helpers;

/// <summary>
/// Provides simple dialog utilities (confirm, message, input) using programmatically-created Avalonia windows.
/// No third-party controls or custom themes are used.
/// </summary>
public static class Dialogs
{
    /// <summary>
    /// Shows a confirmation dialog with Yes/No buttons.
    /// </summary>
    /// <param name="parent">The parent window.</param>
    /// <param name="title">Dialog title.</param>
    /// <param name="message">Message to display.</param>
    /// <returns>True if the user clicked Yes, false otherwise.</returns>
    public static async Task<bool> ConfirmAsync(Window parent, string title, string message)
    {
        var result = false;

        var dialog = new Window
        {
            Title = title,
            Width = 420,
            Height = 170,
            WindowStartupLocation = WindowStartupLocation.CenterOwner,
            CanResize = false,
            ShowInTaskbar = false
        };

        var messageBlock = new TextBlock
        {
            Text = message,
            TextWrapping = Avalonia.Media.TextWrapping.Wrap,
            Margin = new Thickness(0, 0, 0, 15)
        };

        var yesButton = new Button
        {
            Content = "Yes",
            Width = 80,
            HorizontalContentAlignment = HorizontalAlignment.Center
        };
        yesButton.Click += (_, _) =>
        {
            result = true;
            dialog.Close();
        };

        var noButton = new Button
        {
            Content = "No",
            Width = 80,
            HorizontalContentAlignment = HorizontalAlignment.Center
        };
        noButton.Click += (_, _) =>
        {
            result = false;
            dialog.Close();
        };

        var buttonPanel = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Right,
            Spacing = 10,
            Children = { yesButton, noButton }
        };

        dialog.Content = new StackPanel
        {
            Margin = new Thickness(20),
            Spacing = 5,
            Children = { messageBlock, buttonPanel }
        };

        await dialog.ShowDialog(parent);
        return result;
    }

    /// <summary>
    /// Shows a simple message dialog with an OK button.
    /// </summary>
    /// <param name="parent">The parent window.</param>
    /// <param name="title">Dialog title.</param>
    /// <param name="message">Message to display.</param>
    public static async Task ShowMessageAsync(Window parent, string title, string message)
    {
        var dialog = new Window
        {
            Title = title,
            Width = 420,
            Height = 180,
            WindowStartupLocation = WindowStartupLocation.CenterOwner,
            CanResize = false,
            ShowInTaskbar = false
        };

        var messageBlock = new TextBlock
        {
            Text = message,
            TextWrapping = Avalonia.Media.TextWrapping.Wrap,
            Margin = new Thickness(0, 0, 0, 15)
        };

        var okButton = new Button
        {
            Content = "OK",
            Width = 80,
            HorizontalAlignment = HorizontalAlignment.Right,
            HorizontalContentAlignment = HorizontalAlignment.Center
        };
        okButton.Click += (_, _) => dialog.Close();

        dialog.Content = new StackPanel
        {
            Margin = new Thickness(20),
            Spacing = 5,
            Children = { messageBlock, okButton }
        };

        await dialog.ShowDialog(parent);
    }

    /// <summary>
    /// Shows an input dialog with a text box, OK and Cancel buttons.
    /// </summary>
    /// <param name="parent">The parent window.</param>
    /// <param name="title">Dialog title.</param>
    /// <param name="prompt">Prompt text shown above the input.</param>
    /// <param name="defaultValue">Default value in the text box.</param>
    /// <returns>The entered string, or null if cancelled.</returns>
    public static async Task<string?> InputAsync(Window parent, string title, string prompt, string defaultValue = "")
    {
        string? result = null;

        var dialog = new Window
        {
            Title = title,
            Width = 450,
            Height = 180,
            WindowStartupLocation = WindowStartupLocation.CenterOwner,
            CanResize = false,
            ShowInTaskbar = false
        };

        var promptBlock = new TextBlock
        {
            Text = prompt,
            Margin = new Thickness(0, 0, 0, 5)
        };

        var textBox = new TextBox
        {
            Text = defaultValue,
            Margin = new Thickness(0, 0, 0, 10)
        };

        var okButton = new Button
        {
            Content = "OK",
            Width = 80,
            HorizontalContentAlignment = HorizontalAlignment.Center
        };
        okButton.Click += (_, _) =>
        {
            result = textBox.Text;
            dialog.Close();
        };

        var cancelButton = new Button
        {
            Content = "Cancel",
            Width = 80,
            HorizontalContentAlignment = HorizontalAlignment.Center
        };
        cancelButton.Click += (_, _) =>
        {
            result = null;
            dialog.Close();
        };

        var buttonPanel = new StackPanel
        {
            Orientation = Orientation.Horizontal,
            HorizontalAlignment = HorizontalAlignment.Right,
            Spacing = 10,
            Children = { okButton, cancelButton }
        };

        dialog.Content = new StackPanel
        {
            Margin = new Thickness(20),
            Spacing = 5,
            Children = { promptBlock, textBox, buttonPanel }
        };

        await dialog.ShowDialog(parent);
        return result;
    }
}
