using System;
using Decal.Adapter.Messages;

namespace Decal.Adapter;

/// <summary>
///
/// </summary>
[CLSCompliant(true)]
public abstract class Extension : MarshalByRefObject
{
	private DecalExtensionType myExtensionType;

	private CoreManager myLifetime;

	private string myPath;

	private EventHandler<DirectoryResolveEventArgs> directoryResolve;

	public DecalExtensionType ExtensionType => myExtensionType;

	public string Path
	{
		get
		{
			return myPath;
		}
		internal set
		{
			myPath = value;
		}
	}

	protected CoreManager Core
	{
		get
		{
			if (myLifetime == null)
			{
				myLifetime = CoreManager.Current;
			}
			return myLifetime;
		}
	}

	public virtual object ComOM => null;

	public virtual string ReferenceName => GetType().FullName;

	[CLSCompliant(false)]
	protected event EventHandler<DirectoryResolveEventArgs> DirectoryResolve
	{
		add
		{
			directoryResolve = (EventHandler<DirectoryResolveEventArgs>)Delegate.Combine(directoryResolve, value);
		}
		remove
		{
			directoryResolve = (EventHandler<DirectoryResolveEventArgs>)Delegate.Remove(directoryResolve, value);
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<WindowMessageEventArgs> WindowMessage
	{
		add
		{
			Core.WindowMessage += value;
		}
		remove
		{
			Core.WindowMessage -= value;
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<ChatClickInterceptEventArgs> ChatNameClicked
	{
		add
		{
			Core.ChatNameClicked += value;
		}
		remove
		{
			Core.ChatNameClicked -= value;
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<MessageProcessedEventArgs> MessageProcessed
	{
		add
		{
			Core.MessageProcessed += value;
		}
		remove
		{
			Core.MessageProcessed -= value;
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<RegionChange3DEventArgs> RegionChange3D
	{
		add
		{
			Core.RegionChange3D += value;
		}
		remove
		{
			Core.RegionChange3D -= value;
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<ChatTextInterceptEventArgs> ChatBoxMessage
	{
		add
		{
			Core.ChatBoxMessage += value;
		}
		remove
		{
			Core.ChatBoxMessage -= value;
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<ItemSelectedEventArgs> ItemSelected
	{
		add
		{
			Core.ItemSelected += value;
		}
		remove
		{
			Core.ItemSelected -= value;
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<ItemDestroyedEventArgs> ItemDestroyed
	{
		add
		{
			Core.ItemDestroyed += value;
		}
		remove
		{
			Core.ItemDestroyed -= value;
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<ChatParserInterceptEventArgs> CommandLineText
	{
		add
		{
			Core.CommandLineText += value;
		}
		remove
		{
			Core.CommandLineText -= value;
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<ContainerOpenedEventArgs> ContainerOpened
	{
		add
		{
			Core.ContainerOpened += value;
		}
		remove
		{
			Core.ContainerOpened -= value;
		}
	}

	protected event EventHandler<AdapterMessageEventArgs> AdapterMessage
	{
		add
		{
			Core.ExtensionMessage += value;
		}
		remove
		{
			Core.ExtensionMessage -= value;
		}
	}

	internal Extension(DecalExtensionType type)
	{
		myExtensionType = type;
	}

	internal void standardEvent(ExtensionEvents evtToFire)
	{
		switch (evtToFire)
		{
		case ExtensionEvents.Startup:
			Startup();
			break;
		case ExtensionEvents.Shutdown:
			Shutdown();
			break;
		case ExtensionEvents.InternalWireup:
			Wireup();
			break;
		case ExtensionEvents.InternalUnwire:
			UnWire();
			break;
		}
	}

	internal object ResolvePath(string path)
	{
		DirectoryResolveEventArgs e = new DirectoryResolveEventArgs(path);
		if (directoryResolve != null)
		{
			try
			{
				directoryResolve(this, e);
				return e.Result;
			}
			catch
			{
			}
		}
		return null;
	}

	internal virtual void Wireup()
	{
	}

	internal virtual void UnWire()
	{
	}

	protected abstract void Startup();

	protected abstract void Shutdown();

	protected void SendAdapterMessage(AdapterMessageEventArgs args)
	{
		Core.FireExtensionMessage(this, args);
	}
}
