using System.Windows;
using ReAct_PME.Helpers;

namespace ReAct_PME
{
    public static class NotificationManager
    {
        private static readonly List<NotificationWindow> _openNotifications = new();
        private const int Margin = 10;

        public static void ShowNotification_old(string message)
        {
            var toast = new NotificationWindow(message);

            var workArea = SystemParameters.WorkArea;

            double offsetY = 0;

            foreach (var win in _openNotifications)
                offsetY += win.Height + Margin;

            toast.Left = workArea.Right - toast.Width - Margin;
            toast.Top = workArea.Bottom - toast.Height - offsetY - Margin;

            _openNotifications.Add(toast);

            toast.Closed += (s, e) => _openNotifications.Remove(toast);

            toast.OffsetY = offsetY;
            toast.Show();

        }

        public static void ShowNotification(string message, string titre="")
        {
            var toast = new NotificationWindow(message, titre);
            toast.Loaded += (s, e) => PositionNotifications();

            toast.Closed += (s, e) =>
            {
                _openNotifications.Remove(toast);
                PositionNotifications();
            };

            _openNotifications.Add(toast);
            toast.Show();
        }

        private static void PositionNotifications()
        {
            var workArea = SystemParameters.WorkArea;

            double offsetY = 0;

            foreach (var win in _openNotifications)
            {
                // Assurez-vous que le layout est terminé
                win.Dispatcher.Invoke(() =>
                {
                    win.Left = workArea.Right - win.ActualWidth - Margin;
                    win.Top = workArea.Bottom - win.ActualHeight - offsetY - Margin;
                });

                offsetY += win.ActualHeight + Margin;
            }
        }
    }

}
