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

namespace Decal.Interop.Inject;

internal sealed class IPagerEvents_EventProvider : IPagerEvents_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]
		{
			97, 235, 153, 116, 146, 105, 33, 78, 138, 85,
			207, 68, 212, 76, 10, 7
		});
		m_ConnectionPointContainer.FindConnectionPoint(ref riid, out ppCP);
		m_ConnectionPoint = ppCP;
		m_aEventSinkHelpers = new ArrayList();
	}

	event IPagerEvents_DestroyEventHandler IPagerEvents_Event.Destroy
	{
		add
		{

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

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				IPagerEvents_SinkHelper pagerEvents_SinkHelper = new IPagerEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(pagerEvents_SinkHelper, out pdwCookie);
				pagerEvents_SinkHelper.m_dwCookie = pdwCookie;
				pagerEvents_SinkHelper.m_ChangeDelegate = value;
				m_aEventSinkHelpers.Add(pagerEvents_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
				{
					IPagerEvents_SinkHelper pagerEvents_SinkHelper = (IPagerEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (pagerEvents_SinkHelper.m_ChangeDelegate != null && ((pagerEvents_SinkHelper.m_ChangeDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(pagerEvents_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 IPagerEvents_GetNextPositionEventHandler IPagerEvents_Event.GetNextPosition
	{
		add
		{

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