using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using QC.GlobalVariableManager;
using QC.QsprShared.Dialogs;
using QC.QsprShared.Trace;
using QC.QTMApplication.Plugin.Common;
using QC.QTMInterface;

namespace QC.QTMApplication;

public class TestResultsColl : UserControl
{
	public delegate void HandleGenericMessageDelegate(string messageType, GenericTestData data, string pluginNames);

	public delegate void HandleGenericParamMessageDelegate(string messageType, string id, string name, string value, string units, string lowerLimit, string upperLimit, string pluginNames);

	public delegate void HandleCustomMessageDelegate(object customData, string pluginNames);

	private struct PlugginData
	{
		public ArrayList messageQueue;

		public TestResultPlugin testResultForm;
	}

	private Hashtable FormedTestResultPluginTypes = new Hashtable();

	private HandleGenericMessageDelegate genericMsgDel;

	private HandleGenericParamMessageDelegate genericParamMsgDel;

	private HandleCustomMessageDelegate customMsgDel;

	private WSpaceConfig wsc = default(WSpaceConfig);

	private TabControl tabControl;

	private TabPage ultraTabPageControl1;

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

	private Container components;

	private Queue mQueue = new Queue();

	internal QSPRMainWindow QSPRWindow { get; set; }

	public ArrayList AllPluginConfigInfo
	{
		get
		{
			ArrayList arrayList = new ArrayList();
			foreach (TestResultPlugin value in FormedTestResultPluginTypes.Values)
			{
				try
				{
					arrayList.Add(value.PlugInConfigInfo);
				}
				catch (NullReferenceException ex)
				{
					TraceMessage.Write(ToString(), "AllPluginConfigInfo.get{} " + ex.ToString(), TraceMessageType.Exception);
				}
			}
			return arrayList;
		}
		set
		{
			ArrayList arrayList = new ArrayList();
			arrayList = value;
			foreach (TestResultPlugin value2 in FormedTestResultPluginTypes.Values)
			{
				ConfigInfo configInfo = new ConfigInfo();
				configInfo.PlugInConfigNames = new ArrayList();
				configInfo.PlugInConfigValues = new ArrayList();
				configInfo.PlugInName = value2.Name;
				value2.PlugInConfigInfo = configInfo;
			}
			foreach (ConfigInfo item in arrayList)
			{
				string text = item.PlugInName;
				if (text == "UltraResults")
				{
					text = "QSPRTestResults";
				}
				TestResultPlugin testResultPlugin2 = (TestResultPlugin)FormedTestResultPluginTypes[text];
				if (testResultPlugin2 != null)
				{
					testResultPlugin2.PlugInConfigInfo = item;
				}
			}
		}
	}

	public TestResultsColl()
	{
		InitializeComponent();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.tabControl = new System.Windows.Forms.TabControl();
		this.ultraTabPageControl1 = new System.Windows.Forms.TabPage();
		this.tabControl.SuspendLayout();
		base.SuspendLayout();
		this.tabControl.AllowDrop = true;
		this.tabControl.Controls.Add(this.ultraTabPageControl1);
		this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
		this.tabControl.Location = new System.Drawing.Point(0, 0);
		this.tabControl.Name = "tabControl";
		this.tabControl.Size = new System.Drawing.Size(624, 266);
		this.tabControl.TabIndex = 0;
		this.ultraTabPageControl1.Text = "No Plugins Found";
		this.ultraTabPageControl1.Location = new System.Drawing.Point(1, 23);
		this.ultraTabPageControl1.Name = "ultraTabPageControl1";
		this.ultraTabPageControl1.Size = new System.Drawing.Size(620, 240);
		base.Controls.Add(this.tabControl);
		base.Name = "TestResultsColl";
		base.Size = new System.Drawing.Size(624, 266);
		base.Load += new System.EventHandler(TestResultsColl_Load);
		this.tabControl.ResumeLayout(false);
		base.ResumeLayout(false);
	}

	public void AddPlugin(object parameter)
	{
		HandleAddPlugIn(parameter);
	}

	private void TestResultsColl_Load(object sender, EventArgs e)
	{
		genericMsgDel = HandleGenericMsg;
		genericParamMsgDel = HandleGenericParamMsg;
		customMsgDel = HandleCustomMsg;
	}

	public void HandleAddPlugIn(object stateInfo)
	{
		TestResultPlugin testResultPlugin = (TestResultPlugin)stateInfo;
		try
		{
			SuspendLayout();
			FormedTestResultPluginTypes.Add(testResultPlugin.Name, testResultPlugin);
			QSPRWindow._scheduler.GlobalStatus.PluginList.Add(testResultPlugin.Name);
			testResultPlugin.OnTestResultsPluginEventHandler += Plugin_OnTestResultsPluginEventHandler;
			if (FormedTestResultPluginTypes.Count == 1)
			{
				ultraTabPageControl1.Controls.Add(testResultPlugin);
			}
			else
			{
				TabPage tabPage = new TabPage();
				tabPage.Controls.Add(testResultPlugin);
				tabControl.Controls.Add(tabPage);
			}
			tabControl.TabPages[tabControl.TabPages.Count - 1].Text = testResultPlugin.Caption;
			tabControl.TabPages[0].Select();
			testResultPlugin.Dock = DockStyle.Fill;
			testResultPlugin.OnConfigInfo(wsc);
			messengersByPlugin.Add(testResultPlugin, new PluginMessenger(testResultPlugin));
			ResumeLayout(performLayout: false);
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show(ex.Message, DialogResult.OK, MessageDialogType.Exception);
		}
	}

	private CallbackResultInfo Plugin_OnTestResultsPluginEventHandler(object sender, TestResultsPluginEventArgs e)
	{
		CallbackResultInfo result = default(CallbackResultInfo);
		try
		{
			switch (e.MessageCategory)
			{
			case CallbackCategory.MENU_MESSAGE:
				result.Result = true;
				break;
			case CallbackCategory.SPECIAL_MESSAGE:
				result = ProcessSpecialMessage(e);
				return result;
			}
		}
		catch (Exception ex)
		{
			result.Message = ex.Message;
			result.Result = false;
		}
		return result;
	}

	private CallbackResultInfo ProcessSpecialMessage(TestResultsPluginEventArgs e)
	{
		CallbackResultInfo result = default(CallbackResultInfo);
		try
		{
			switch (e.Key)
			{
			case "Tree Changed":
				QSPRMainWindow.SWPFTestTreeWindowCollection.TreeChanged = true;
				QSPRWindow._scheduler.GlobalStatus.RegistrationStatus.PluginDataChanged = true;
				result.Result = true;
				break;
			case "Status Bar":
				QSPRMainWindow.StatusBar.PluginMessage = (string)e.Message;
				result.Result = true;
				break;
			case "Logging Complete":
				if (e.Message is BasePlugin)
				{
					QSPRWindow.SendLogDoneMessage((BasePlugin)e.Message);
					result.Result = true;
				}
				else
				{
					result.Result = false;
				}
				break;
			}
		}
		catch (Exception ex)
		{
			result.Message = ex.Message;
			result.Result = false;
		}
		return result;
	}

	public void OnMsg(ArrayList messageQueue, MsgData lastMessage)
	{
		TraceMessage.Write(ToString(), "OnMsg", TraceMessageType.Trace);
		try
		{
			if (QSPRWindow.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)
					{
						foreach (TestResultPlugin value in FormedTestResultPluginTypes.Values)
						{
							PlugginData plugginData = default(PlugginData);
							plugginData.messageQueue = (ArrayList)arrayList.Clone();
							plugginData.testResultForm = value;
							if (value.GetType().ToString() == "XmlForDB.XmlForDB")
							{
								QTMGlobalVariable.SetGlobalVariableObject("XML4DB_WRITING", "TRUE");
							}
							messengersByPlugin[value].SendMsg(plugginData.messageQueue);
						}
					}
				}
			}
			else
			{
				foreach (TestResultPlugin value2 in FormedTestResultPluginTypes.Values)
				{
					PlugginData plugginData2 = default(PlugginData);
					plugginData2.messageQueue = (ArrayList)messageQueue.Clone();
					plugginData2.testResultForm = value2;
					if (value2.GetType().ToString() == "XmlForDB.XmlForDB")
					{
						QTMGlobalVariable.SetGlobalVariableObject("XML4DB_WRITING", "TRUE");
					}
					messengersByPlugin[value2].SendMsg(plugginData2.messageQueue);
				}
			}
			QSPRWindow._scheduler.GlobalStatus.ProcessTestResultsPlugIns = false;
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "OnMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void OnGenericMessage(string messageType, GenericTestData data, string pluginNames)
	{
		BeginInvoke(genericMsgDel, messageType, data, pluginNames);
	}

	public void OnGenericParamMessage(string messageType, string id, string name, string value, string units, string lowerLimit, string upperLimit, string pluginNames)
	{
		BeginInvoke(genericParamMsgDel, messageType, id, name, value, units, lowerLimit, upperLimit, pluginNames);
	}

	public void OnCustomMessage(object messageData, string pluginNames)
	{
		BeginInvoke(customMsgDel, messageData, pluginNames);
	}

	public void SendMsgToWindows(object stateInfo)
	{
		TraceMessage.Write(ToString(), "SendMsgToWindows", TraceMessageType.Trace);
		try
		{
			PlugginData plugginData = (PlugginData)stateInfo;
			foreach (MsgData item in plugginData.messageQueue)
			{
				if (item.msgType == TestMsgType.OnUnitEnd)
				{
					TraceMessage.WriteAlways(ToString(), "In SendMsgToWindows(object stateInfo) from Thread pool:" + plugginData.testResultForm.Caption, TraceMessageType.Trace);
				}
			}
			plugginData.testResultForm.OnMsg(plugginData.messageQueue);
		}
		catch (Exception ex)
		{
			TraceMessage.WriteAlways(ToString(), "SendMsgToWindows " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void OnConfigInfo(WSpaceConfig wsc)
	{
		this.wsc = wsc;
		foreach (TestResultPlugin value in FormedTestResultPluginTypes.Values)
		{
			value.OnConfigInfo(wsc);
		}
	}

	public void HandleToolsClick(string menu)
	{
		try
		{
			foreach (TestResultPlugin value in FormedTestResultPluginTypes.Values)
			{
				value.MenuMessages(menu);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleToolsClick " + ex.Message, TraceMessageType.Exception);
		}
	}

	private ArrayList GetPluginRecipients(string pluginNames)
	{
		ArrayList arrayList = new ArrayList();
		try
		{
			string[] array = null;
			if (!string.IsNullOrEmpty(pluginNames))
			{
				array = pluginNames.Split(',');
			}
			foreach (object value in FormedTestResultPluginTypes.Values)
			{
				if (!(value is TestResultPlugin))
				{
					continue;
				}
				try
				{
					TestResultPlugin testResultPlugin = (TestResultPlugin)value;
					if (array != null && array.Length > 0)
					{
						string[] array2 = array;
						foreach (string text in array2)
						{
							if (testResultPlugin.Caption == text)
							{
								arrayList.Add(testResultPlugin);
							}
						}
					}
					else
					{
						arrayList.Add(testResultPlugin);
					}
				}
				catch (Exception ex)
				{
					TraceMessage.Write(ToString(), "GetPluginRecipients " + ex.Message, TraceMessageType.Trace);
				}
			}
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "GetPluginRecipients " + ex2.Message, TraceMessageType.Exception);
		}
		return arrayList;
	}

	public void HandleGenericMsg(string messageType, GenericTestData data, string pluginNames)
	{
		try
		{
			ArrayList pluginRecipients = GetPluginRecipients(pluginNames);
			foreach (object item in pluginRecipients)
			{
				if (item is TestResultPlugin)
				{
					TestResultPlugin testResultPlugin = (TestResultPlugin)item;
					testResultPlugin.OnGenericMessage(messageType, data);
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleGenericMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void HandleGenericParamMsg(string messageType, string id, string name, string value, string units, string lowerLimit, string upperLimit, string pluginNames)
	{
		try
		{
			ArrayList pluginRecipients = GetPluginRecipients(pluginNames);
			foreach (object item in pluginRecipients)
			{
				if (item is TestResultPlugin)
				{
					TestResultPlugin testResultPlugin = (TestResultPlugin)item;
					testResultPlugin.OnGenericParamMessage(messageType, id, name, value, units, lowerLimit, upperLimit);
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleGenericParamMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void HandleCustomMsg(object customData, string pluginNames)
	{
		try
		{
			ArrayList pluginRecipients = GetPluginRecipients(pluginNames);
			foreach (object item in pluginRecipients)
			{
				if (item is TestResultPlugin)
				{
					TestResultPlugin testResultPlugin = (TestResultPlugin)item;
					testResultPlugin.OnCustomMessage(customData);
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleCustomMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void OnDebugMsg(DebugMsgType msgType, object msgInfo)
	{
		foreach (TestResultPlugin value in FormedTestResultPluginTypes.Values)
		{
			if (value.GetDebugMsg)
			{
				value.OnDebugMsg(msgType, msgInfo);
			}
		}
	}

	public bool IsSafeToExit()
	{
		foreach (TestResultPlugin value in FormedTestResultPluginTypes.Values)
		{
			if (!value.IsSafeToExit)
			{
				return false;
			}
		}
		return true;
	}

	public bool Closing()
	{
		foreach (TestResultPlugin value in FormedTestResultPluginTypes.Values)
		{
			try
			{
				if (!value.Closing())
				{
					return false;
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "Closing " + ex.Message, TraceMessageType.Exception);
			}
		}
		return true;
	}
}
