using System;
using System.Collections.Generic;

namespace Decal.Adapter.Messages;

[CLSCompliant(true)]
public class AdapterMessageEventArgs : EventArgs
{
	private EventHandler<AdapterMessageResponseEventArgs> responder;

	private List<object> handlers;

	private bool canAddHandlers;

	internal bool CanAddHandlers
	{
		get
		{
			return canAddHandlers;
		}
		set
		{
			canAddHandlers = value;
		}
	}

	public event EventHandler<AdapterMessageResponseEventArgs> MessageComplete
	{
		add
		{
			responder = (EventHandler<AdapterMessageResponseEventArgs>)Delegate.Combine(responder, value);
		}
		remove
		{
			responder = (EventHandler<AdapterMessageResponseEventArgs>)Delegate.Remove(responder, value);
		}
	}

	internal AdapterMessageEventArgs()
	{
		handlers = new List<object>();
	}

	public void AddHandler(object obj)
	{
		if (!CanAddHandlers)
		{
			throw new InvalidOperationException("Unable to add handlers at this time");
		}
		if (obj != null)
		{
			handlers.Add(obj);
		}
	}

	public void SetComplete(object handler, AdapterMessageResponseEventArgs e)
	{
		handlers.Remove(handler);
		if (handlers.Count == 0)
		{
			e.Complete = true;
		}
		if (responder != null)
		{
			responder(this, e);
		}
	}
}
