// (c) 2005 Electronic Arts Inc.
//

using System;

namespace EA.GameTalkRouter
{
	/// <summary>
	/// Manages communication between various views and other systems
	/// </summary>
	sealed public class ServerEventManager
	{
		/// <summary>
		/// Track the list of event handlers
		/// </summary>
		private static event ServerEventHandler Receivers;

		private static event LoggingEventHandler LoggingReceivers;

		/// <summary>
		/// Private constructor prevents default constructors from being created
		/// </summary>
		private ServerEventManager()
		{
		}

		public static void Register(ServerEventHandler eventHandler)
		{
			Receivers += eventHandler;
		}

		public static void Unregister(ServerEventHandler eventHandler)
		{
			Receivers -= eventHandler;
		}

		public static void LoggerRegister(LoggingEventHandler eventHandler)
		{
			LoggingReceivers += eventHandler;
		}

		public static void LoggerUnregister(LoggingEventHandler eventHandler)
		{
			LoggingReceivers -= eventHandler;
		}

		public static void Send(object sender, ServerEventArgs e)
		{
			try
			{
				// You can do a null check "if (Receivers != null)" - but it should be very rare - so we can just catch exception and ignore
				Receivers.DynamicInvoke(new object[]{sender, e});
			}
			catch {}
		}

		public static void LoggingSend(object sender, LoggingEventArgs e)
		{
			try
			{
				// You can do a null check "if (LoggingReceivers != null)" - but it should be very rare - so we can just catch exception and ignore
				LoggingReceivers.DynamicInvoke(new object[] { sender, e });
			}
			catch { }
		}
	}
}
