using System;
using System.Collections.Generic;
using System.Reflection;
using Decal.Adapter.Wrappers;

namespace Decal.Adapter;

[CLSCompliant(true)]
public abstract class PluginBase : Extension, IViewHandler
{
	private PluginHost myHost;

	private EventHandler myGraphicsReset;

	private BindingFlags myBindingFlags;

	private Dictionary<string, ViewWrapper> myViews;

	protected PluginHost Host => myHost;

	public ViewWrapper DefaultView => myViews["Default"];

	public BindingFlags BindingFlags => myBindingFlags;

	[CLSCompliant(false)]
	protected event EventHandler<NetworkMessageEventArgs> ServerDispatch
	{
		add
		{
			if (base.Core.EchoFilter != null)
			{
				base.Core.EchoFilter.ServerDispatch += value;
			}
		}
		remove
		{
			if (base.Core.EchoFilter != null)
			{
				base.Core.EchoFilter.ServerDispatch -= value;
			}
		}
	}

	[CLSCompliant(false)]
	protected event EventHandler<NetworkMessageEventArgs> ClientDispatch
	{
		add
		{
			if (base.Core.EchoFilter != null)
			{
				base.Core.EchoFilter.ClientDispatch += value;
			}
		}
		remove
		{
			if (base.Core.EchoFilter != null)
			{
				base.Core.EchoFilter.ClientDispatch -= value;
			}
		}
	}

	protected event EventHandler GraphicsReset
	{
		add
		{
			myGraphicsReset = (EventHandler)Delegate.Combine(myGraphicsReset, value);
			if (myHost != null)
			{
				myHost.Render.DeviceLost += value;
			}
		}
		remove
		{
			myGraphicsReset = (EventHandler)Delegate.Remove(myGraphicsReset, value);
			if (myHost != null)
			{
				myHost.Render.DeviceLost -= value;
			}
		}
	}

	protected PluginBase()
		: base(DecalExtensionType.Plugin)
	{
		myBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
	}

	internal override void Wireup()
	{
		if (myGraphicsReset != null)
		{
			Host.Render.DeviceLost += myGraphicsReset;
		}
		scanBaseEvents(connect: true);
		myViews = new Dictionary<string, ViewWrapper>();
		PluginHost.LoadViewHandler(this);
	}

	internal override void UnWire()
	{
		if (myGraphicsReset != null)
		{
			Host.Render.DeviceLost -= myGraphicsReset;
		}
		scanBaseEvents(connect: false);
		foreach (ViewWrapper value in myViews.Values)
		{
			value.Dispose();
		}
		myViews.Clear();
	}

	internal void SetHost(PluginHost newHost)
	{
		myHost = newHost;
	}

	private void scanBaseEvents(bool connect)
	{
		Type type = GetType();
		Type type2 = base.Core.GetType();
		if (!Attribute.IsDefined(type, typeof(WireUpBaseEventsAttribute)))
		{
			return;
		}
		MethodInfo[] methods = type.GetMethods(BindingFlags);
		foreach (MethodInfo methodInfo in methods)
		{
			if (!Attribute.IsDefined(methodInfo, typeof(BaseEventAttribute)))
			{
				continue;
			}
			Attribute[] customAttributes = Attribute.GetCustomAttributes(methodInfo, typeof(BaseEventAttribute));
			for (int j = 0; j < customAttributes.Length; j++)
			{
				BaseEventAttribute baseEventAttribute = (BaseEventAttribute)customAttributes[j];
				object obj;
				Type type3;
				if (baseEventAttribute.FilterName == string.Empty)
				{
					obj = this;
					type3 = type;
				}
				else
				{
					PropertyInfo property = type2.GetProperty(baseEventAttribute.FilterName, BindingFlags.Instance | BindingFlags.Public);
					if (property == null)
					{
						continue;
					}
					obj = property.GetValue(base.Core, null);
					type3 = obj.GetType();
				}
				EventInfo eventInfo = null;
				while ((eventInfo = type3.GetEvent(baseEventAttribute.EventName, BindingFlags)) == null && type3 == type)
				{
					type3 = type2;
					obj = base.Core;
				}
				if (!(eventInfo == null))
				{
					if (connect)
					{
						eventInfo.GetAddMethod(nonPublic: true).Invoke(obj, new object[1] { Delegate.CreateDelegate(eventInfo.EventHandlerType, this, methodInfo.Name) });
					}
					else
					{
						eventInfo.GetRemoveMethod(nonPublic: true).Invoke(obj, new object[1] { Delegate.CreateDelegate(eventInfo.EventHandlerType, this, methodInfo.Name) });
					}
				}
			}
		}
	}

	public void LoadView(string name, string resource)
	{
		myViews.Add(name, Host.LoadViewResource(resource, GetType().Assembly));
	}

	public ViewWrapper GetView(string name)
	{
		if (myViews.TryGetValue(name, out var value))
		{
			return value;
		}
		return null;
	}
}
