using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Sonex.DBWrapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sonex.Client.Helpers
{
    public static class Messages
    {
        public static async Task ShowMessageAsync(this FrameworkElement element, DB.Result? result)
        {
            if (result == null || !result.Success)
                await ShowErrorAsync(element, "Error", result?.ErrorMessage??"");
        }
        public static async Task ShowErrorAsync(this FrameworkElement element, string title, string content)
        {
            ArgumentNullException.ThrowIfNull(element);

            var dialog = new ContentDialog
            {
                XamlRoot = element.XamlRoot,
                Title = title,
                Content = content,
                CloseButtonText = "OK",
                DefaultButton = ContentDialogButton.Close
            };

            await dialog.ShowAsync();
        }

        public static Task ShowErrorAsync(this Window window, string title, string content)
        {
            ArgumentNullException.ThrowIfNull(window);

            if (window.Content is not FrameworkElement root)
                throw new InvalidOperationException("Window.Content is not a FrameworkElement.");

            return root.ShowErrorAsync(title, content);
        }
    }
}
