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

namespace Decal.Interop.Controls;

internal sealed class ICommandEvents_EventProvider : ICommandEvents_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]
		{
			161, 211, 41, 240, 175, 41, 239, 69, 146, 126,
			114, 159, 237, 216, 116, 3
		});
		m_ConnectionPointContainer.FindConnectionPoint(ref riid, out ppCP);
		m_ConnectionPoint = ppCP;
		m_aEventSinkHelpers = new ArrayList();
	}

	event ICommandEvents_DestroyEventHandler ICommandEvents_Event.Destroy
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				ICommandEvents_SinkHelper commandEvents_SinkHelper = new ICommandEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(commandEvents_SinkHelper, out pdwCookie);
				commandEvents_SinkHelper.m_dwCookie = pdwCookie;
				commandEvents_SinkHelper.m_DestroyDelegate = value;
				m_aEventSinkHelpers.Add(commandEvents_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
				{
					ICommandEvents_SinkHelper commandEvents_SinkHelper = (ICommandEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (commandEvents_SinkHelper.m_DestroyDelegate != null && ((commandEvents_SinkHelper.m_DestroyDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(commandEvents_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 ICommandEvents_HitEventHandler ICommandEvents_Event.Hit
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				ICommandEvents_SinkHelper commandEvents_SinkHelper = new ICommandEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(commandEvents_SinkHelper, out pdwCookie);
				commandEvents_SinkHelper.m_dwCookie = pdwCookie;
				commandEvents_SinkHelper.m_HitDelegate = value;
				m_aEventSinkHelpers.Add(commandEvents_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
				{
					ICommandEvents_SinkHelper commandEvents_SinkHelper = (ICommandEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (commandEvents_SinkHelper.m_HitDelegate != null && ((commandEvents_SinkHelper.m_HitDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(commandEvents_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 ICommandEvents_UnhitEventHandler ICommandEvents_Event.Unhit
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				ICommandEvents_SinkHelper commandEvents_SinkHelper = new ICommandEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(commandEvents_SinkHelper, out pdwCookie);
				commandEvents_SinkHelper.m_dwCookie = pdwCookie;
				commandEvents_SinkHelper.m_UnhitDelegate = value;
				m_aEventSinkHelpers.Add(commandEvents_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
				{
					ICommandEvents_SinkHelper commandEvents_SinkHelper = (ICommandEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (commandEvents_SinkHelper.m_UnhitDelegate != null && ((commandEvents_SinkHelper.m_UnhitDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(commandEvents_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 ICommandEvents_AcceptedEventHandler ICommandEvents_Event.Accepted
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				ICommandEvents_SinkHelper commandEvents_SinkHelper = new ICommandEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(commandEvents_SinkHelper, out pdwCookie);
				commandEvents_SinkHelper.m_dwCookie = pdwCookie;
				commandEvents_SinkHelper.m_AcceptedDelegate = value;
				m_aEventSinkHelpers.Add(commandEvents_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
				{
					ICommandEvents_SinkHelper commandEvents_SinkHelper = (ICommandEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (commandEvents_SinkHelper.m_AcceptedDelegate != null && ((commandEvents_SinkHelper.m_AcceptedDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(commandEvents_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 ICommandEvents_CanceledEventHandler ICommandEvents_Event.Canceled
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				ICommandEvents_SinkHelper commandEvents_SinkHelper = new ICommandEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(commandEvents_SinkHelper, out pdwCookie);
				commandEvents_SinkHelper.m_dwCookie = pdwCookie;
				commandEvents_SinkHelper.m_CanceledDelegate = value;
				m_aEventSinkHelpers.Add(commandEvents_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
				{
					ICommandEvents_SinkHelper commandEvents_SinkHelper = (ICommandEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (commandEvents_SinkHelper.m_CanceledDelegate != null && ((commandEvents_SinkHelper.m_CanceledDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(commandEvents_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 ICommandEvents_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
				{
					ICommandEvents_SinkHelper commandEvents_SinkHelper = (ICommandEvents_SinkHelper)m_aEventSinkHelpers[num];
					m_ConnectionPoint.Unadvise(commandEvents_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);
	}
}
