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

namespace Decal.Interop.Net;

internal sealed class IWebRequestEvents_EventProvider : IWebRequestEvents_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]
		{
			220, 56, 250, 238, 178, 21, 50, 66, 181, 190,
			90, 158, 203, 142, 26, 88
		});
		m_ConnectionPointContainer.FindConnectionPoint(ref riid, out ppCP);
		m_ConnectionPoint = ppCP;
		m_aEventSinkHelpers = new ArrayList();
	}

	event IWebRequestEvents_BeginEventHandler IWebRequestEvents_Event.Begin
	{
		add
		{

			bool lockTaken = default(bool);
			try
			{
				Monitor.Enter(this, ref lockTaken);
				if (m_ConnectionPoint == null)
				{
					Init();
				}
				IWebRequestEvents_SinkHelper webRequestEvents_SinkHelper = new IWebRequestEvents_SinkHelper();
				int pdwCookie = 0;
				m_ConnectionPoint.Advise(webRequestEvents_SinkHelper, out pdwCookie);
				webRequestEvents_SinkHelper.m_dwCookie = pdwCookie;
				webRequestEvents_SinkHelper.m_BeginDelegate = value;
				m_aEventSinkHelpers.Add(webRequestEvents_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
				{
					IWebRequestEvents_SinkHelper webRequestEvents_SinkHelper = (IWebRequestEvents_SinkHelper)m_aEventSinkHelpers[num];
					if (webRequestEvents_SinkHelper.m_BeginDelegate != null && ((webRequestEvents_SinkHelper.m_BeginDelegate.Equals(value) ? 1u : 0u) & 0xFFu) != 0)
					{
						m_aEventSinkHelpers.RemoveAt(num);
						m_ConnectionPoint.Unadvise(webRequestEvents_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 IWebRequestEvents_EndEventHandler IWebRequestEvents_Event.End
	{
		add
		{

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