using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using QC.QsprShared.Config;
using QC.QsprShared.GlobalData;
using QC.QsprTestObjects;
using QC.QTMInterface;

namespace QC.QSPRSchedulerWrapper;

public class QsprPluginManager
{
	private MefPluginLoader pluginLoader;

	private List<DebugWindowPlugIn> debugPlugins;

	private List<TestResultPlugin> testResultPlugins;

	private Queue mQueue = new Queue();

	private Dictionary<BasePlugin, PluginMessenger> messengersByPlugin = new Dictionary<BasePlugin, PluginMessenger>();

	private Scheduler scheduler;

	private GlobalStatus globalStatus
	{
		get
		{
			return scheduler.GlobalStatus;
		}
		set
		{
			scheduler.GlobalStatus = value;
		}
	}

	public QsprPluginManager(QSPRScheduler qsprScheduler, string pluginDirectory)
	{
		scheduler = qsprScheduler._scheduler;
		pluginLoader = new MefPluginLoader();
		pluginLoader.Load(pluginDirectory);
		qsprScheduler.OnRealTimeParamMessage += scheduler_OnRealTimeParamMessage;
		qsprScheduler.OnRealTimeMessage += scheduler_OnRealTimeMessage;
		qsprScheduler.OnRealTimeCustomMessage += scheduler_OnRealTimeCustomMessage;
	}

	private void scheduler_OnRealTimeParamMessage(string messageType, string id, string name = "", string value = "", string units = "", string lowerLimit = "", string upperLimit = "", byte pluginTypes = 7, string pluginNames = "")
	{
		if ((pluginTypes & 2) == 2 && testResultPlugins != null)
		{
			foreach (TestResultPlugin testResultPlugin in testResultPlugins)
			{
				testResultPlugin.OnGenericParamMessage(messageType, id, name, value, units, lowerLimit, upperLimit);
			}
			return;
		}
		if ((pluginTypes & 4) != 4 || debugPlugins == null)
		{
			return;
		}
		foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
		{
			debugPlugin.OnGenericParamMessage(messageType, id, name, value, units, lowerLimit, upperLimit);
		}
	}

	private void scheduler_OnRealTimeMessage(string messageType, GenericTestData data = null, byte pluginTypes = 7, string pluginNames = "")
	{
		if ((pluginTypes & 2) == 2 && testResultPlugins != null)
		{
			foreach (TestResultPlugin testResultPlugin in testResultPlugins)
			{
				testResultPlugin.OnGenericMessage(messageType, data);
			}
			return;
		}
		if ((pluginTypes & 4) != 4 || debugPlugins == null)
		{
			return;
		}
		foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
		{
			debugPlugin.OnGenericMessage(messageType, data);
		}
	}

	private void scheduler_OnRealTimeCustomMessage(object messageData, byte pluginTypes = 7, string pluginNames = "")
	{
		if ((pluginTypes & 2) == 2 && testResultPlugins != null)
		{
			foreach (TestResultPlugin testResultPlugin in testResultPlugins)
			{
				testResultPlugin.OnCustomMessage(messageData);
			}
			return;
		}
		if ((pluginTypes & 4) != 4 || debugPlugins == null)
		{
			return;
		}
		foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
		{
			debugPlugin.OnCustomMessage(messageData);
		}
	}

	private void SendMessageToPlugin(ArrayList messages, BasePlugin plugin)
	{
		messengersByPlugin[plugin].SendMsg(messages);
	}

	internal void LoadPluginSettingsFromTree(PluginConfig pluginConfig)
	{
		ArrayList arrayList = new ArrayList();
		arrayList = pluginConfig.TestResultsConfig;
		foreach (TestResultPlugin testResultPlugin2 in testResultPlugins)
		{
			ConfigInfo configInfo = new ConfigInfo();
			configInfo.PlugInConfigNames = new ArrayList();
			configInfo.PlugInConfigValues = new ArrayList();
			configInfo.PlugInName = testResultPlugin2.Name;
			testResultPlugin2.PlugInConfigInfo = configInfo;
		}
		string PlugInName;
		foreach (ConfigInfo item in arrayList)
		{
			PlugInName = item.PlugInName;
			if (PlugInName == "UltraResults")
			{
				PlugInName = "QSPRTestResults";
			}
			TestResultPlugin testResultPlugin = testResultPlugins.Where((TestResultPlugin x) => x.Caption == PlugInName).FirstOrDefault();
			if (testResultPlugin != null)
			{
				testResultPlugin.PlugInConfigInfo = item;
			}
		}
	}

	public void OnMsg(ArrayList messageQueue, byte pluginTypes, MsgData lastMessage)
	{
		if ((pluginTypes & 4) == 4)
		{
			OnDebugMsg(messageQueue, lastMessage);
		}
		if ((pluginTypes & 2) == 2)
		{
			OnTestResultMsg(messageQueue, lastMessage);
		}
	}

	public void OnTestResultMsg(ArrayList messageQueue, MsgData lastMessage)
	{
		try
		{
			if (scheduler.QTMData.WConfig.TestResultMessageProcessingOption != "Auto Update")
			{
				mQueue.Enqueue(messageQueue);
				if (lastMessage.msgType == TestMsgType.OnUnitEnd || lastMessage.msgType == TestMsgType.OnEndOfDebugTesting)
				{
					ArrayList arrayList = new ArrayList();
					while (mQueue.Count > 0)
					{
						ArrayList arrayList2 = (ArrayList)mQueue.Dequeue();
						foreach (MsgData item in arrayList2)
						{
							if (!item.DoNotSendToPlugins)
							{
								arrayList.Add(item);
							}
						}
					}
					if (arrayList.Count > 0 && testResultPlugins != null)
					{
						foreach (TestResultPlugin testResultPlugin in testResultPlugins)
						{
							if (testResultPlugin.GetType().ToString() == "XmlForDB.XmlForDB")
							{
								scheduler.Env.GlobalVariable.SetGlobalVariableObject("XML4DB_WRITING", "TRUE");
							}
							ArrayList messages = (ArrayList)messageQueue.Clone();
							SendMessageToPlugin(messages, testResultPlugin);
						}
					}
				}
			}
			else if (testResultPlugins != null)
			{
				foreach (TestResultPlugin testResultPlugin2 in testResultPlugins)
				{
					if (testResultPlugin2.GetType().ToString() == "XmlForDB.XmlForDB")
					{
						scheduler.Env.GlobalVariable.SetGlobalVariableObject("XML4DB_WRITING", "TRUE");
					}
					ArrayList messages2 = (ArrayList)messageQueue.Clone();
					SendMessageToPlugin(messages2, testResultPlugin2);
				}
			}
			globalStatus.ProcessTestResultsPlugIns = false;
		}
		catch (Exception)
		{
		}
	}

	public List<TestResultPlugin> LoadTestResultPlugins(List<string> pluginNamesToLoad = null)
	{
		testResultPlugins = pluginLoader.GetExportedValues<TestResultPlugin>(pluginNamesToLoad).ToList();
		foreach (TestResultPlugin testResultPlugin in testResultPlugins)
		{
			messengersByPlugin.Add(testResultPlugin, new PluginMessenger(testResultPlugin));
		}
		return testResultPlugins;
	}

	public void AddTestResultPlugin(TestResultPlugin plugin)
	{
		testResultPlugins.Add(plugin);
		messengersByPlugin.Add(plugin, new PluginMessenger(plugin));
	}

	public void OnDebugMsg(ArrayList messageQueue, MsgData lastMessage)
	{
		try
		{
			if (scheduler.QTMData.WConfig.DebugWindowUpdateOption != "Auto Update")
			{
				mQueue.Enqueue(messageQueue);
				if (lastMessage.msgType != TestMsgType.OnUnitEnd && lastMessage.msgType != TestMsgType.OnEndOfDebugTesting)
				{
					return;
				}
				ArrayList arrayList = new ArrayList();
				while (mQueue.Count > 0)
				{
					ArrayList arrayList2 = (ArrayList)mQueue.Dequeue();
					foreach (MsgData item in arrayList2)
					{
						if (!item.DoNotSendToPlugins)
						{
							arrayList.Add(item);
						}
					}
				}
				if (arrayList.Count <= 0 || debugPlugins == null)
				{
					return;
				}
				{
					foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
					{
						ArrayList messages = (ArrayList)messageQueue.Clone();
						SendMessageToPlugin(messages, debugPlugin);
					}
					return;
				}
			}
			if (debugPlugins == null)
			{
				return;
			}
			foreach (DebugWindowPlugIn debugPlugin2 in debugPlugins)
			{
				ArrayList messages2 = (ArrayList)messageQueue.Clone();
				SendMessageToPlugin(messages2, debugPlugin2);
			}
		}
		catch (Exception)
		{
		}
	}

	public List<DebugWindowPlugIn> LoadDebugPlugins(List<string> pluginNamesToLoad = null)
	{
		debugPlugins = pluginLoader.GetExportedValues<DebugWindowPlugIn>(pluginNamesToLoad).ToList();
		foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
		{
			messengersByPlugin.Add(debugPlugin, new PluginMessenger(debugPlugin));
		}
		return debugPlugins;
	}

	public void AddDebugPlugin(DebugWindowPlugIn plugin)
	{
		debugPlugins.Add(plugin);
		messengersByPlugin.Add(plugin, new PluginMessenger(plugin));
	}

	public void HandleTPL2Msg(string windowName, Tpl2Command command, Tpl2Info commandInfo)
	{
		try
		{
			foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
			{
				try
				{
					debugPlugin.OnTpl2Message(windowName, command, commandInfo);
				}
				catch (Exception)
				{
				}
			}
		}
		catch (Exception)
		{
		}
	}

	public void HandleHTMLMsg(string windowName, HTMLCommand command, string commandInfo)
	{
		try
		{
			foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
			{
				try
				{
					debugPlugin.OnHTMLMessage(windowName, command, commandInfo);
				}
				catch (Exception)
				{
				}
			}
		}
		catch (Exception)
		{
		}
	}

	public void HandleClearDebugMsg(string windowName)
	{
		try
		{
			debugPlugins.Where((DebugWindowPlugIn x) => x.Caption == windowName).FirstOrDefault()?.MenuMessages("Clear Debug Windows");
		}
		catch (Exception)
		{
		}
	}

	public void HandleSaveDebugMsg(string windowName, bool appendText)
	{
		try
		{
			DebugWindowPlugIn debugWindowPlugIn = debugPlugins.Where((DebugWindowPlugIn x) => x.Caption == windowName).FirstOrDefault();
			if (debugWindowPlugIn != null)
			{
				debugWindowPlugIn.AppendText = appendText;
				debugWindowPlugIn.MenuMessages("Save Debug Windows");
			}
		}
		catch (Exception)
		{
		}
	}

	public void HandleClearAllDebugMsg()
	{
		try
		{
			foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
			{
				try
				{
					debugPlugin.MenuMessages("Clear Debug Windows");
				}
				catch (Exception)
				{
				}
			}
		}
		catch (Exception)
		{
		}
	}

	public void HandleSaveAllDebugMsg(bool appendText)
	{
		try
		{
			foreach (DebugWindowPlugIn debugPlugin in debugPlugins)
			{
				try
				{
					debugPlugin.AppendText = appendText;
					debugPlugin.MenuMessages("Save Debug Windows");
				}
				catch (Exception)
				{
				}
			}
		}
		catch (Exception)
		{
		}
	}
}
