Code/NotificationSystem.cs
/// <summary>
/// Represents a system that manages notifications within a scene.
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using BetterUI.Extensions;

namespace BetterUI;

/// <summary>
/// A sealed class that handles notifications in a scene.
/// </summary>
public sealed class NotificationSystem( Scene scene ) : GameObjectSystem( scene )
{
	private readonly List<INotification> _notifications = new();

	/// <summary>
	/// Gets or sets the expiration time for notifications.
	/// </summary>
	public TimeSpan ExpirationTime { get; set; } = TimeSpan.FromSeconds( 5 );

	/// <summary>
	/// Adds a notification to the system and runs scene events.
	/// </summary>
	/// <param name="notification">The notification to add.</param>
	/// <param name="include">Specifies which components to include.</param>
	public void Push( INotification notification, EventInclude include = EventInclude.ComponentAndPanel )
	{
		_notifications.Add( notification );
		Scene.RunSceneEvent<INotificationEvent>( x => x.OnNotification( notification ), include );
	}

	/// <summary>
	/// Retrieves all notifications that have not expired.
	/// </summary>
	/// <returns>An enumerable of notifications.</returns>
	public IEnumerable<INotification> GetNotifications() =>
		_notifications.Where( x => (DateTime.Now - x.CreatedAt).TotalSeconds <= ExpirationTime.TotalSeconds );

	/// <summary>
	/// Retrieves notifications of a specific type.
	/// </summary>
	/// <typeparam name="T">The type of notifications to retrieve.</typeparam>
	/// <returns>An enumerable of notifications of type T.</returns>
	public IEnumerable<T> GetNotifications<T>() where T : INotification => _notifications.OfType<T>();

	/// <summary>
	/// Clears all notifications.
	/// </summary>
	public void Clear() => _notifications.Clear();

	/// <summary>
	/// Clears notifications of a specific type.
	/// </summary>
	/// <typeparam name="T">The type of notifications to clear.</typeparam>
	public void Clear<T>() where T : INotification => _notifications.RemoveAll( x => x is T );

	/// <summary>
	/// Disposes of the notification system, clearing all notifications.
	/// </summary>
	public override void Dispose()
	{
		_notifications.Clear();
	}
}