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

namespace Decal.Interop.Render;

internal sealed class IRenderServiceEvents_EventProvider : IRenderServiceEvents_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]
		{
			45, 245, 73, 60, 255, 85, 216, 66, 153, 176,
			187, 24, 94, 154, 104, 79
		});
		m_ConnectionPointContainer.FindConnectionPoint(ref riid, out ppCP);
		m_ConnectionPoint = ppCP;
		m_aEventSinkHelpers = new ArrayList();
	}

	event IRenderServiceEvents_DeviceLostEventHandler IRenderServiceEvents_Event.DeviceLost
	{
		add
		{

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