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

namespace Decal.Interop.Core;

internal sealed class IDecalEvents_EventProvider : IDecalEvents_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]
		{
			38, 245, 98, 163, 3, 130, 119, 74, 158, 55,
			54, 17, 48, 146, 77, 40
		});
		m_ConnectionPointContainer.FindConnectionPoint(ref riid, out ppCP);
		m_ConnectionPoint = ppCP;
		m_aEventSinkHelpers = new ArrayList();
	}

	public void add_InitializeComplete(IDecalEvents_InitializeCompleteEventHandler P_0)
	{
		bool lockTaken = default(bool);
		try
		{
			Monitor.Enter(this, ref lockTaken);
			if (m_ConnectionPoint == null)
			{
				Init();
			}
			IDecalEvents_SinkHelper decalEvents_SinkHelper = new IDecalEvents_SinkHelper();
			int pdwCookie = 0;
			m_ConnectionPoint.Advise(decalEvents_SinkHelper, out pdwCookie);
			decalEvents_SinkHelper.m_dwCookie = pdwCookie;
			decalEvents_SinkHelper.m_InitializeCompleteDelegate = P_0;
			m_aEventSinkHelpers.Add(decalEvents_SinkHelper);
		}
		finally
		{
			if (lockTaken)
			{
				Monitor.Exit(this);
			}
		}
	}

	public void remove_InitializeComplete(IDecalEvents_InitializeCompleteEventHandler P_0)
	{
		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
			{
				IDecalEvents_SinkHelper decalEvents_SinkHelper = (IDecalEvents_SinkHelper)m_aEventSinkHelpers[num];
				if (decalEvents_SinkHelper.m_InitializeCompleteDelegate != null && ((decalEvents_SinkHelper.m_InitializeCompleteDelegate.Equals(P_0) ? 1u : 0u) & 0xFFu) != 0)
				{
					m_aEventSinkHelpers.RemoveAt(num);
					m_ConnectionPoint.Unadvise(decalEvents_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 void add_TerminateComplete(IDecalEvents_TerminateCompleteEventHandler P_0)
	{
		bool lockTaken = default(bool);
		try
		{
			Monitor.Enter(this, ref lockTaken);
			if (m_ConnectionPoint == null)
			{
				Init();
			}
			IDecalEvents_SinkHelper decalEvents_SinkHelper = new IDecalEvents_SinkHelper();
			int pdwCookie = 0;
			m_ConnectionPoint.Advise(decalEvents_SinkHelper, out pdwCookie);
			decalEvents_SinkHelper.m_dwCookie = pdwCookie;
			decalEvents_SinkHelper.m_TerminateCompleteDelegate = P_0;
			m_aEventSinkHelpers.Add(decalEvents_SinkHelper);
		}
		finally
		{
			if (lockTaken)
			{
				Monitor.Exit(this);
			}
		}
	}

	public void remove_TerminateComplete(IDecalEvents_TerminateCompleteEventHandler P_0)
	{
		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
			{
				IDecalEvents_SinkHelper decalEvents_SinkHelper = (IDecalEvents_SinkHelper)m_aEventSinkHelpers[num];
				if (decalEvents_SinkHelper.m_TerminateCompleteDelegate != null && ((decalEvents_SinkHelper.m_TerminateCompleteDelegate.Equals(P_0) ? 1u : 0u) & 0xFFu) != 0)
				{
					m_aEventSinkHelpers.RemoveAt(num);
					m_ConnectionPoint.Unadvise(decalEvents_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 IDecalEvents_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
				{
					IDecalEvents_SinkHelper decalEvents_SinkHelper = (IDecalEvents_SinkHelper)m_aEventSinkHelpers[num];
					m_ConnectionPoint.Unadvise(decalEvents_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);
	}
}
