using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Xml;
using Decal.Interop.Core;
using Decal.Interop.Inject;
using Decal.Interop.Render;

namespace Decal.Adapter.Wrappers;

[CLSCompliant(true)]
public sealed class PluginHost : HostBase
{
	private IPluginSite2 mySite;

	private IPluginSite myOldSite;

	[CLSCompliant(false)]
	[EditorBrowsable(EditorBrowsableState.Never)]
	public IPluginSite2 Underlying => mySite;

	public DecalWrapper Decal
	{
		get
		{
			if (base.MyDecal == null)
			{
				base.MyDecal = new DecalWrapper(mySite.Decal);
			}
			return base.MyDecal;
		}
	}

	public HooksWrapper Actions => CoreManager.Current.Actions;

	public RenderServiceWrapper Render
	{
		get
		{
			if (base.MyRender == null)
			{
				base.MyRender = new RenderServiceWrapper((RenderService)GetObject("services\\DecalRender.RenderService"));
			}
			return base.MyRender;
		}
	}

	internal PluginHost(PluginSite2 pSite)
	{
		mySite = pSite;
		base.MyDecal = new DecalWrapper(mySite.Decal);
		myOldSite = (IPluginSite)mySite.PluginSite;
	}

	protected override void Dispose(bool disposing)
	{
		try
		{
			_ = base.IsDisposed;
			if (myOldSite != null)
			{
				Marshal.ReleaseComObject(myOldSite);
				myOldSite = null;
			}
			if (mySite != null)
			{
				Marshal.ReleaseComObject(mySite);
				mySite = null;
			}
		}
		finally
		{
			base.Dispose(disposing);
		}
	}

	public object GetObject(string path)
	{
		return ((dynamic)mySite).get_Object(path);
	}

	public int GetKeyboardMapping(string name)
	{
		return myOldSite.QueryKeyboardMap(name);
	}

	internal static void LoadViewHandler(IViewHandler handler)
	{
		if (ViewWrapper.ScanViews(handler))
		{
			ViewWrapper.ScanControls(handler);
			ViewWrapper.ScanReferences(handler);
		}
	}

	public ViewHandler LoadViewHandler(Type handlerType)
	{
		ViewHandler obj = (ViewHandler)Activator.CreateInstance(handlerType, this);
		LoadViewHandler(obj);
		obj.LoadComplete();
		return obj;
	}

	public ViewWrapper LoadViewResource(string resourcePath)
	{
		Assembly callingAssembly = Assembly.GetCallingAssembly();
		return LoadViewResource(resourcePath, callingAssembly);
	}

	public ViewWrapper LoadViewResource(string resourcePath, Assembly resourceAssembly)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		if (null == resourceAssembly)
		{
			throw new ArgumentNullException("resourceAssembly");
		}
		Stream manifestResourceStream = resourceAssembly.GetManifestResourceStream(resourcePath);
		XmlDocument val = new XmlDocument();
		val.Load(manifestResourceStream);
		return LoadView(((XmlNode)val).OuterXml);
	}

	public ViewWrapper LoadView(XmlElement viewSchema)
	{
		if (viewSchema == null)
		{
			throw new ArgumentNullException("viewSchema");
		}
		return LoadView(((XmlNode)viewSchema).OuterXml);
	}

	public ViewWrapper LoadView(string viewSchema)
	{
		View view = myOldSite.LoadView(viewSchema);
		if (view != null)
		{
			return new ViewWrapper(view);
		}
		return null;
	}

	public object ComFilter(string progId)
	{
		return ((dynamic)mySite).get_Object("services\\DecalNet.NetService\\" + progId);
	}
}
