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

namespace Decal.Interop.Controls;

internal sealed class ICheckboxEvents_EventProvider : ICheckboxEvents_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]
		{
			138, 178, 108, 213, 173, 155, 177, 64, 160, 17,
			112, 244, 102, 220, 143, 5
		});
		m_ConnectionPointContainer.FindConnectionPoint(ref riid, out ppCP);
		m_ConnectionPoint = ppCP;
		m_aEventSinkHelpers = new ArrayList();
	}

	event ICheckboxEvents_DestroyEventHandler ICheckboxEvents_Event.Destroy
	{
		add
		{

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

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