using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Markup;
using QC.QsprShared.Config;
using QC.QsprShared.Trace;
using QC.QTMApplication.Plugin.Common;
using QC.QTMInterface;

namespace QC.QTMApplication;

public partial class QSPRApp : System.Windows.Application, IComponentConnector
{
	private Hashtable instantiatedMenuPluginTypes = new Hashtable();

	private Hashtable instantiatedToolBarPluginTypes = new Hashtable();

	private Hashtable instantiatedTestResultPluginTypes = new Hashtable();

	private Hashtable instantiatedDebugWindowPlugInTypes = new Hashtable();

	private Hashtable instantiatedDocumentPluginTypes = new Hashtable();

	private Hashtable instantiatedTTDPluginTypes = new Hashtable();

	private Hashtable instantiatedParamPluginTypes = new Hashtable();

	private Hashtable createdTTDPlugIns = new Hashtable();

	private Hashtable instantiatedUserWindowPluginTypes = new Hashtable();

	private Hashtable instantiatedTestCasesPluginTypes = new Hashtable();

	public WPFDebugWindowColl DebugWindowCollection = new WPFDebugWindowColl();

	private UserWindowColl UserWindowCollection = new UserWindowColl();

	private TestResultsColl TestResultsCollection = new TestResultsColl();

	internal WPFTestTreeWindowColl WPFTestTreeWindowCollection = new WPFTestTreeWindowColl();

	internal QTMArguments QtmArguments;

	internal WorkspaceConfig WConfig;

	private void QsprApplication_Startup(object sender, StartupEventArgs e)
	{
		System.Windows.Forms.Application.EnableVisualStyles();
		QSPRMainWindow qSPRMainWindow = SetupMainWindow();
		LoadPlugins(qSPRMainWindow._scheduler.Env.SchedulerID, qSPRMainWindow.WConfig.PluginDetails);
		SplashScreen.CloseForm();
		LoadMainWindow(qSPRMainWindow);
	}

	private QSPRMainWindow SetupMainWindow()
	{
		QSPRMainWindow qSPRMainWindow = new QSPRMainWindow();
		qSPRMainWindow.TestResultsCollection = TestResultsCollection;
		qSPRMainWindow.UserWindowCollection = UserWindowCollection;
		qSPRMainWindow.DebugWindowCollection = DebugWindowCollection;
		qSPRMainWindow.WPFTestTreeWindowCollection = WPFTestTreeWindowCollection;
		qSPRMainWindow.WConfig = WConfig;
		qSPRMainWindow.QArguments = QtmArguments;
		TestResultsCollection.QSPRWindow = qSPRMainWindow;
		DebugWindowCollection.QSPRWindow = qSPRMainWindow;
		UserWindowCollection.QSPRWindow = qSPRMainWindow;
		WPFTestTreeWindowCollection.QSPRWindow = qSPRMainWindow;
		WPFTestTreeWindowCollection.ttdWpfTreeView.UpdateWindowName = qSPRMainWindow.UpdateWindowName;
		WPFTestTreeWindowCollection.ttdWpfTreeView.AddRecentFile = qSPRMainWindow.AddRecentFile;
		return qSPRMainWindow;
	}

	private void LoadMainWindow(QSPRMainWindow mainWindow)
	{
		mainWindow.Load();
		if (mainWindow.WConfig.MinimizeAtStartup || mainWindow.QArguments.HideWindow)
		{
			mainWindow.WindowState = WindowState.Minimized;
		}
		mainWindow.Visibility = Visibility.Hidden;
		mainWindow.ShowInTaskbar = false;
		if (!mainWindow.QArguments.HideWindow)
		{
			mainWindow.Visibility = Visibility.Visible;
			mainWindow.ShowInTaskbar = true;
		}
		mainWindow.Show();
	}

	public void LoadPlugins(string schedulerID, ArrayList PluginDetails)
	{
		try
		{
			LoadPluginsHelper(schedulerID, PluginDetails);
		}
		catch (Exception ex)
		{
			TraceMessage.WriteAlways(ToString(), "LoadPlugins " + ex.ToString(), TraceMessageType.Exception);
		}
	}

	public void LoadPluginsHelper(string schedulerID, ArrayList PluginDetails)
	{
		_ = DateTime.Now;
		List<Type> list = new List<Type>();
		list.Add(typeof(BasePlugin));
		list.Add(typeof(UserWindows));
		list.Add(typeof(TestResultPlugin));
		Type[] matchingPluginTypes = PluginHandler.GetMatchingPluginTypes(list, PluginDetails);
		InstantiateUIElements(matchingPluginTypes.Where((Type t) => typeof(TTDPlugIn).IsAssignableFrom(t)).ToArray(), instantiatedTTDPluginTypes, typeof(TTDPlugIn), instantiate: true, schedulerID);
		InstantiateUIElements(matchingPluginTypes.Where((Type t) => typeof(ParamViewPlugIn).IsAssignableFrom(t)).ToArray(), instantiatedParamPluginTypes, typeof(ParamViewPlugIn), instantiate: true, schedulerID);
		InstantiateUIElements(matchingPluginTypes.Where((Type t) => typeof(TestResultPlugin).IsAssignableFrom(t)).ToArray(), instantiatedTestResultPluginTypes, typeof(TestResultPlugin), instantiate: true, schedulerID);
		InstantiateUIElements(matchingPluginTypes.Where((Type t) => typeof(DebugWindowPlugIn).IsAssignableFrom(t)).ToArray(), instantiatedDebugWindowPlugInTypes, typeof(DebugWindowPlugIn), instantiate: true, schedulerID);
		InstantiateUIElements(matchingPluginTypes.Where((Type t) => typeof(UserWindows).IsAssignableFrom(t)).ToArray(), instantiatedUserWindowPluginTypes, typeof(UserWindows), instantiate: true, schedulerID);
		InstantiateUIElements(matchingPluginTypes.Where((Type t) => typeof(TestCasesPlugin).IsAssignableFrom(t)).ToArray(), instantiatedTestCasesPluginTypes, typeof(TestCasesPlugin), instantiate: true, schedulerID);
	}

	private void InstantiateUIElements(Type[] providerTypes, Hashtable keys, Type type, bool instantiate, string schedulerID)
	{
		foreach (Type type2 in providerTypes)
		{
			try
			{
				if (keys.ContainsKey(type2))
				{
					continue;
				}
				keys.Add(type2, null);
				object obj;
				if (instantiate)
				{
					TraceMessage.WriteAlways(ToString(), "Creating instance of type " + type2.FullName, TraceMessageType.Trace);
					obj = PluginHandler.CreateInstance(type2);
					if (obj is BasePlugin)
					{
						(obj as BasePlugin).SchedulerID = schedulerID;
					}
					else
					{
						(obj as UserWindows).SchedulerID = schedulerID;
					}
				}
				else
				{
					obj = type2;
				}
				if (type == typeof(TTDPlugIn))
				{
					UpdateTTDWindow(obj);
				}
				if (type == typeof(ParamViewPlugIn))
				{
					UpdateParamViewWindow(obj);
				}
				else if (type == typeof(TestResultPlugin))
				{
					UpdateTestResultWindow(obj);
				}
				else if (type == typeof(DebugWindowPlugIn))
				{
					UpdateDebugWindow(obj);
				}
				else if (type == typeof(UserWindows))
				{
					UpdateUserWindow(obj);
				}
				else if (type == typeof(TestCasesPlugin))
				{
					UpdateTestCasesWindow(obj);
				}
			}
			catch (Exception ex)
			{
				TraceMessage.WriteAlways(ToString(), "InstantiateUIElements " + ex.ToString(), TraceMessageType.Exception);
			}
		}
	}

	private void UpdateTestResultWindow(object parameter)
	{
		TestResultsCollection.AddPlugin(parameter);
	}

	private void UpdateTTDWindow(object parameter)
	{
		TTDPlugIn dWin = (TTDPlugIn)parameter;
		WPFTestTreeWindowCollection.AddTTDPlugIn(dWin);
	}

	private void UpdateParamViewWindow(object parameter)
	{
		ParamViewPlugIn dWin = (ParamViewPlugIn)parameter;
		WPFTestTreeWindowCollection.AddParamPlugIn(dWin);
	}

	private void UpdateTestCasesWindow(object parameter)
	{
		TestCasesPlugin dWin = (TestCasesPlugin)parameter;
		WPFTestTreeWindowCollection.AddTestCasesPlugIn(dWin);
	}

	private void UpdateDebugWindow(object parameter)
	{
		DebugWindowPlugIn dWin = (DebugWindowPlugIn)parameter;
		DebugWindowCollection.AddDebugWindowPlugIn(dWin);
	}

	private void UpdateUserWindow(object parameter)
	{
		UserWindows dWin = (UserWindows)parameter;
		UserWindowCollection.AddDebugWindowPlugIn(dWin);
	}
}
