using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;

namespace Decal.Interop.SpellFilter;

internal sealed class _ISpellsEvents_EventProvider : _ISpellsEvents_Event, IDisposable
{
	private IConnectionPointContainer m_ConnectionPointContainer;

	private ArrayList m_aEventSinkHelpers;

	private IConnectionPoint m_ConnectionPoint;

	private void Init()
	{
		IConnectionPoint ppCP = null;
		Guid riid = new Guid(new byte[16]
		{
			250, 226, 166, 118, 218, 48, 66, 68, 167, 95,
			211, 1, 59, 4, 78, 99
		});
		m_ConnectionPointContainer.FindConnectionPoint(ref riid, out ppCP);
		m_ConnectionPoint = ppCP;
		m_aEventSinkHelpers = new ArrayList();
	}

	event _ISpellsEvents_LoginEventHandler _ISpellsEvents_Event.Login
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				_ISpellsEvents_SinkHelper iSpellsEvents_SinkHelper = new _ISpellsEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(iSpellsEvents_SinkHelper, out pdwCookie);
				iSpellsEvents_SinkHelper.m_dwCookie = pdwCookie;
				iSpellsEvents_SinkHelper.m_LoginDelegate = value;
				m_aEventSinkHelpers.Add(iSpellsEvents_SinkHelper);
			}
			finally
			{
				if (lockTaken)
				{
					Monitor.Exit(this);
				}
			}
	
		}
		remove
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_aEventSinkHelpers == null)
				{
					return;
				}
				int count = m_aEventSinkHelpers.Count;
				int num = 0;
				if (0 >= count)
				{
					return;
				}
				do
				{
					_ISpellsEvents_SinkHelper iSpellsEvents_SinkHelper = (_ISpellsEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (iSpellsEvents_SinkHelper.m_LoginDelegate != null && ((iSpellsEvents_SinkHelper.m_LoginDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(iSpellsEvents_SinkHelper.m_dwCookie);
						if (count <= 1)
						{
							Marshal.ReleaseComObject(m_ConnectionPoint);
							m_ConnectionPoint = null;
							m_aEventSinkHelpers = null;
						}
						break;
					}
					num++;
				}
				while (num < count);
			}
			finally
			{
				if (lockTaken)
				{
					Monitor.Exit(this);
				}
			}
	
		}
	}

	event _ISpellsEvents_Spellbook_AddEventHandler _ISpellsEvents_Event.Spellbook_Add
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				_ISpellsEvents_SinkHelper iSpellsEvents_SinkHelper = new _ISpellsEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(iSpellsEvents_SinkHelper, out pdwCookie);
				iSpellsEvents_SinkHelper.m_dwCookie = pdwCookie;
				iSpellsEvents_SinkHelper.m_Spellbook_AddDelegate = value;
				m_aEventSinkHelpers.Add(iSpellsEvents_SinkHelper);
			}
			finally
			{
				if (lockTaken)
				{
					Monitor.Exit(this);
				}
			}
	
		}
		remove
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_aEventSinkHelpers == null)
				{
					return;
				}
				int count = m_aEventSinkHelpers.Count;
				int num = 0;
				if (0 >= count)
				{
					return;
				}
				do
				{
					_ISpellsEvents_SinkHelper iSpellsEvents_SinkHelper = (_ISpellsEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (iSpellsEvents_SinkHelper.m_Spellbook_AddDelegate != null && ((iSpellsEvents_SinkHelper.m_Spellbook_AddDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(iSpellsEvents_SinkHelper.m_dwCookie);
						if (count <= 1)
						{
							Marshal.ReleaseComObject(m_ConnectionPoint);
							m_ConnectionPoint = null;
							m_aEventSinkHelpers = null;
						}
						break;
					}
					num++;
				}
				while (num < count);
			}
			finally
			{
				if (lockTaken)
				{
					Monitor.Exit(this);
				}
			}
	
		}
	}

	event _ISpellsEvents_Spellbook_DeleteEventHandler _ISpellsEvents_Event.Spellbook_Delete
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				_ISpellsEvents_SinkHelper iSpellsEvents_SinkHelper = new _ISpellsEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(iSpellsEvents_SinkHelper, out pdwCookie);
				iSpellsEvents_SinkHelper.m_dwCookie = pdwCookie;
				iSpellsEvents_SinkHelper.m_Spellbook_DeleteDelegate = value;
				m_aEventSinkHelpers.Add(iSpellsEvents_SinkHelper);
			}
			finally
			{
				if (lockTaken)
				{
					Monitor.Exit(this);
				}
			}
	
		}
		remove
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_aEventSinkHelpers == null)
				{
					return;
				}
				int count = m_aEventSinkHelpers.Count;
				int num = 0;
				if (0 >= count)
				{
					return;
				}
				do
				{
					_ISpellsEvents_SinkHelper iSpellsEvents_SinkHelper = (_ISpellsEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (iSpellsEvents_SinkHelper.m_Spellbook_DeleteDelegate != null && ((iSpellsEvents_SinkHelper.m_Spellbook_DeleteDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(iSpellsEvents_SinkHelper.m_dwCookie);
						if (count <= 1)
						{
							Marshal.ReleaseComObject(m_ConnectionPoint);
							m_ConnectionPoint = null;
							m_aEventSinkHelpers = null;
						}
						break;
					}
					num++;
				}
				while (num < count);
			}
			finally
			{
				if (lockTaken)
				{
					Monitor.Exit(this);
				}
			}
	
		}
	}

	public _ISpellsEvents_EventProvider(object P_0)
	{
		//Error decoding local variables: Signature type sequence must have at least one element.
		m_ConnectionPointContainer = (IConnectionPointContainer)P_0;
	}

	public void Finalize()
	{
		bool lockTaken = default(bool);
		try
		{
			Monitor.Enter(this, ref lockTaken);
			if (m_ConnectionPoint == null)
			{
				return;
			}
			int count = m_aEventSinkHelpers.Count;
			int num = 0;
			if (0 < count)
			{
				do
				{
					_ISpellsEvents_SinkHelper iSpellsEvents_SinkHelper = (_ISpellsEvents_SinkHelper)m_aEventSinkHelpers[num];
					m_ConnectionPoint.Unadvise(iSpellsEvents_SinkHelper.m_dwCookie);
					num++;
				}
				while (num < count);
			}
			Marshal.ReleaseComObject(m_ConnectionPoint);
		}
		catch (Exception)
		{
		}
		finally
		{
			if (lockTaken)
			{
				Monitor.Exit(this);
			}
		}
	}

	public void Dispose()
	{
		//Error decoding local variables: Signature type sequence must have at least one element.
		Finalize();
		GC.SuppressFinalize(this);
	}
}
