using Decal.Adapter.NetParser;
using Decal.Adapter.Wrappers;
using Decal.Interop.Core;
using Decal.Interop.Net;

namespace Decal.Adapter;

public sealed class FilterProxy : INetworkFilter2, IDecalDirectory
{
	private NetServiceHost myService;

	private FilterBase myFilter;

	internal FilterProxy(FilterBase toWrap)
	{
		myFilter = toWrap;
	}

	void INetworkFilter2.DispatchClient(IMessage2 msg)
	{
		Message message = null;
		message = ((!(msg is MessageWrapper messageWrapper)) ? new Message(msg, MessageDirection.Inbound) : messageWrapper.Wrapped);
		myFilter.fireNetwork(message, Server: false);
	}

	void INetworkFilter2.DispatchServer(IMessage2 msg)
	{
		Message message = null;
		message = ((!(msg is MessageWrapper messageWrapper)) ? new Message(msg, MessageDirection.Inbound) : messageWrapper.Wrapped);
		myFilter.fireNetwork(message, Server: true);
	}

	void INetworkFilter2.Initialize(NetService pService)
	{
		myService = new NetServiceHost(pService);
		myFilter.SetHost(myService);
		myFilter.standardEvent(ExtensionEvents.InternalWireup);
		myFilter.standardEvent(ExtensionEvents.Startup);
	}

	void INetworkFilter2.Terminate()
	{
		myFilter.standardEvent(ExtensionEvents.Shutdown);
		myService.Dispose();
	}

	object IDecalDirectory.Lookup(string strName)
	{
		return myFilter.ResolvePath(strName);
	}
}
