using System;
using Avalonia.Controls;
using Avalonia.Interactivity;

namespace MarkdownEditor.Views;

/// <summary>
/// Dialog for confirming whether to save unsaved changes before closing.
/// </summary>
public partial class SaveConfirmationDialog : Window
{
    /// <summary>
    /// Gets the user's choice: Save, Don't Save, or Cancel.
    /// </summary>
    public SaveConfirmationResult Result { get; private set; } = SaveConfirmationResult.Cancel;

    public SaveConfirmationDialog()
    {
        InitializeComponent();
    }

    public SaveConfirmationDialog(string message) : this()
    {
        if (MessageText != null)
        {
            MessageText.Text = message;
        }
    }

    private void OnSave(object? sender, RoutedEventArgs e)
    {
        Result = SaveConfirmationResult.Save;
        Close();
    }

    private void OnDontSave(object? sender, RoutedEventArgs e)
    {
        Result = SaveConfirmationResult.DontSave;
        Close();
    }

    private void OnCancel(object? sender, RoutedEventArgs e)
    {
        Result = SaveConfirmationResult.Cancel;
        Close();
    }
}

/// <summary>
/// Result of the save confirmation dialog.
/// </summary>
public enum SaveConfirmationResult
{
    /// <summary>
    /// User chose to save changes.
    /// </summary>
    Save,

    /// <summary>
    /// User chose not to save changes.
    /// </summary>
    DontSave,

    /// <summary>
    /// User cancelled the close operation.
    /// </summary>
    Cancel
}
