using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Decal.Adapter.Support;
using Decal.Interop.Core;

namespace Decal.Adapter.Wrappers;

[CLSCompliant(true)]
public class DecalWrapper : MarshalByRefObject, IDisposable
{
	internal static Guid IID_IDISPATCH = new Guid("{00020400-0000-0000-C000-000000000046}");

	internal static Guid IID_IUNKNOWN = new Guid("{00000000-0000-0000-C000-000000000046}");

	private DecalCore myDecal;

	private bool isDisposed;

	[CLSCompliant(false)]
	[EditorBrowsable(EditorBrowsableState.Never)]
	public DecalCore Underlying => myDecal;

	public IntPtr Hwnd => new IntPtr(myDecal.HWND);

	public bool Focus => myDecal.Focus;

	internal DecalWrapper(DecalCore decal)
	{
		myDecal = decal;
	}

	~DecalWrapper()
	{
		Dispose(disposing: false);
	}

	public void Dispose()
	{
		Dispose(disposing: true);
		GC.SuppressFinalize(this);
	}

	private void Dispose(bool disposing)
	{
		if (!isDisposed)
		{
		}
		if (myDecal != null)
		{
			Marshal.ReleaseComObject(myDecal);
			myDecal = null;
		}
		isDisposed = true;
	}

	public object GetObject(string path)
	{
		return GetObject(path, IID_IDISPATCH);
	}

	public object GetObject(string path, string iid)
	{
		return GetObject(path, new Guid(iid));
	}

	public object GetObject(string path, Guid iid)
	{
		try
		{
			return ((IDecalCore)myDecal).get_Object(path, ref iid);
		}
		catch (Exception ex)
		{
			Util.WriteLine("Exception for {0} during get_Object: {1}", path, ex);
			return null;
		}
	}

	public string MapPath(string path)
	{
		return myDecal.MapPath(path);
	}
}
