using System;
using System.Collections.Generic;
using UnityEngine;

internal static class OVRMessenger
{
	public static void MarkAsPermanent(string eventType)
	{
		OVRMessenger.permanentMessages.Add(eventType);
	}

	public static void Cleanup()
	{
		List<string> list = new List<string>();
		foreach (KeyValuePair<string, Delegate> keyValuePair in OVRMessenger.eventTable)
		{
			bool flag = false;
			foreach (string b in OVRMessenger.permanentMessages)
			{
				if (keyValuePair.Key == b)
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				list.Add(keyValuePair.Key);
			}
		}
		foreach (string key in list)
		{
			OVRMessenger.eventTable.Remove(key);
		}
	}

	public static void PrintEventTable()
	{
		Debug.Log("\t\t\t=== MESSENGER PrintEventTable ===");
		foreach (KeyValuePair<string, Delegate> keyValuePair in OVRMessenger.eventTable)
		{
			Debug.Log(string.Concat(new object[]
			{
				"\t\t\t",
				keyValuePair.Key,
				"\t\t",
				keyValuePair.Value
			}));
		}
		Debug.Log("\n");
	}

	public static void OnListenerAdding(string eventType, Delegate listenerBeingAdded)
	{
		if (!OVRMessenger.eventTable.ContainsKey(eventType))
		{
			OVRMessenger.eventTable.Add(eventType, null);
		}
		Delegate @delegate = OVRMessenger.eventTable[eventType];
		if (@delegate != null && @delegate.GetType() != listenerBeingAdded.GetType())
		{
			throw new OVRMessenger.ListenerException(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, @delegate.GetType().Name, listenerBeingAdded.GetType().Name));
		}
	}

	public static void OnListenerRemoving(string eventType, Delegate listenerBeingRemoved)
	{
		if (!OVRMessenger.eventTable.ContainsKey(eventType))
		{
			throw new OVRMessenger.ListenerException(string.Format("Attempting to remove listener for type \"{0}\" but Messenger doesn't know about this event type.", eventType));
		}
		Delegate @delegate = OVRMessenger.eventTable[eventType];
		if (@delegate == null)
		{
			throw new OVRMessenger.ListenerException(string.Format("Attempting to remove listener with for event type \"{0}\" but current listener is null.", eventType));
		}
		if (@delegate.GetType() != listenerBeingRemoved.GetType())
		{
			throw new OVRMessenger.ListenerException(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, @delegate.GetType().Name, listenerBeingRemoved.GetType().Name));
		}
	}

	public static void OnListenerRemoved(string eventType)
	{
		if (OVRMessenger.eventTable[eventType] == null)
		{
			OVRMessenger.eventTable.Remove(eventType);
		}
	}

	public static void OnBroadcasting(string eventType)
	{
	}

	public static OVRMessenger.BroadcastException CreateBroadcastSignatureException(string eventType)
	{
		return new OVRMessenger.BroadcastException(string.Format("Broadcasting message \"{0}\" but listeners have a different signature than the broadcaster.", eventType));
	}

	public static void AddListener(string eventType, OVRCallback handler)
	{
		OVRMessenger.OnListenerAdding(eventType, handler);
		OVRMessenger.eventTable[eventType] = (OVRCallback)Delegate.Combine((OVRCallback)OVRMessenger.eventTable[eventType], handler);
	}

	public static void AddListener<T>(string eventType, OVRCallback<T> handler)
	{
		OVRMessenger.OnListenerAdding(eventType, handler);
		OVRMessenger.eventTable[eventType] = (OVRCallback<T>)Delegate.Combine((OVRCallback<T>)OVRMessenger.eventTable[eventType], handler);
	}

	public static void AddListener<T, U>(string eventType, OVRCallback<T, U> handler)
	{
		OVRMessenger.OnListenerAdding(eventType, handler);
		OVRMessenger.eventTable[eventType] = (OVRCallback<T, U>)Delegate.Combine((OVRCallback<T, U>)OVRMessenger.eventTable[eventType], handler);
	}

	public static void AddListener<T, U, V>(string eventType, OVRCallback<T, U, V> handler)
	{
		OVRMessenger.OnListenerAdding(eventType, handler);
		OVRMessenger.eventTable[eventType] = (OVRCallback<T, U, V>)Delegate.Combine((OVRCallback<T, U, V>)OVRMessenger.eventTable[eventType], handler);
	}

	public static void RemoveListener(string eventType, OVRCallback handler)
	{
		OVRMessenger.OnListenerRemoving(eventType, handler);
		OVRMessenger.eventTable[eventType] = (OVRCallback)Delegate.Remove((OVRCallback)OVRMessenger.eventTable[eventType], handler);
		OVRMessenger.OnListenerRemoved(eventType);
	}

	public static void RemoveListener<T>(string eventType, OVRCallback<T> handler)
	{
		OVRMessenger.OnListenerRemoving(eventType, handler);
		OVRMessenger.eventTable[eventType] = (OVRCallback<T>)Delegate.Remove((OVRCallback<T>)OVRMessenger.eventTable[eventType], handler);
		OVRMessenger.OnListenerRemoved(eventType);
	}

	public static void RemoveListener<T, U>(string eventType, OVRCallback<T, U> handler)
	{
		OVRMessenger.OnListenerRemoving(eventType, handler);
		OVRMessenger.eventTable[eventType] = (OVRCallback<T, U>)Delegate.Remove((OVRCallback<T, U>)OVRMessenger.eventTable[eventType], handler);
		OVRMessenger.OnListenerRemoved(eventType);
	}

	public static void RemoveListener<T, U, V>(string eventType, OVRCallback<T, U, V> handler)
	{
		OVRMessenger.OnListenerRemoving(eventType, handler);
		OVRMessenger.eventTable[eventType] = (OVRCallback<T, U, V>)Delegate.Remove((OVRCallback<T, U, V>)OVRMessenger.eventTable[eventType], handler);
		OVRMessenger.OnListenerRemoved(eventType);
	}

	public static void Broadcast(string eventType)
	{
		OVRMessenger.OnBroadcasting(eventType);
		Delegate @delegate;
		if (OVRMessenger.eventTable.TryGetValue(eventType, out @delegate))
		{
			OVRCallback ovrcallback = @delegate as OVRCallback;
			if (ovrcallback == null)
			{
				throw OVRMessenger.CreateBroadcastSignatureException(eventType);
			}
			ovrcallback();
		}
	}

	public static void Broadcast<T>(string eventType, T arg1)
	{
		OVRMessenger.OnBroadcasting(eventType);
		Delegate @delegate;
		if (OVRMessenger.eventTable.TryGetValue(eventType, out @delegate))
		{
			OVRCallback<T> ovrcallback = @delegate as OVRCallback<T>;
			if (ovrcallback == null)
			{
				throw OVRMessenger.CreateBroadcastSignatureException(eventType);
			}
			ovrcallback(arg1);
		}
	}

	public static void Broadcast<T, U>(string eventType, T arg1, U arg2)
	{
		OVRMessenger.OnBroadcasting(eventType);
		Delegate @delegate;
		if (OVRMessenger.eventTable.TryGetValue(eventType, out @delegate))
		{
			OVRCallback<T, U> ovrcallback = @delegate as OVRCallback<T, U>;
			if (ovrcallback == null)
			{
				throw OVRMessenger.CreateBroadcastSignatureException(eventType);
			}
			ovrcallback(arg1, arg2);
		}
	}

	public static void Broadcast<T, U, V>(string eventType, T arg1, U arg2, V arg3)
	{
		OVRMessenger.OnBroadcasting(eventType);
		Delegate @delegate;
		if (OVRMessenger.eventTable.TryGetValue(eventType, out @delegate))
		{
			OVRCallback<T, U, V> ovrcallback = @delegate as OVRCallback<T, U, V>;
			if (ovrcallback == null)
			{
				throw OVRMessenger.CreateBroadcastSignatureException(eventType);
			}
			ovrcallback(arg1, arg2, arg3);
		}
	}

	private static MessengerHelper messengerHelper = new GameObject("MessengerHelper").AddComponent<MessengerHelper>();

	public static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();

	public static List<string> permanentMessages = new List<string>();

	public class BroadcastException : Exception
	{
		public BroadcastException(string msg) : base(msg)
		{
		}
	}

	public class ListenerException : Exception
	{
		public ListenerException(string msg) : base(msg)
		{
		}
	}
}
