using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using QC.GlobalVariableManager;
using QC.QsprShared.Config;
using QC.QsprShared.Dialogs;
using QC.QsprShared.Trace;
using QC.QsprShared.Utilities;
using QC.QTMInterface;
using QsprMiscShared.Utilities;

namespace QC.QTMApplication;

public partial class WPFDebugWindowColl : System.Windows.Controls.UserControl, IComponentConnector
{
	public delegate void HandleCreateWinDelegate(object stateInfo);

	public delegate void HandlePrintWinDelegate(object stateInfo);

	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);

	public delegate void HandleTPL2WinDelegate(string windowName, Tpl2Command command, Tpl2Info info);

	public delegate void HandleHTMLWinDelegate(string windowName, HTMLCommand command, string info);

	public delegate void HandleClearDebugMsgDelegate(string windowName);

	public delegate void HandleSaveDebugMsgDelegate(string windowName, bool appendText);

	public delegate void HandleClearAllDebugMsgDelegate();

	public delegate void HandleSaveAllDebugMsgDelegate(bool appendText);

	public delegate void HandleSetDebugWindowInFocusDelegate(string debugWindowName);

	public delegate void CreatingNewWindowEventHandler(object sender, EventArgs e);

	public delegate void CreatedNewWindowEventHandler(object sender, EventArgs e);

	public delegate void DebugCollDelegate(object sender, PluginMenuEventsArgs e);

	private const string DEBUG_DOCKING_FNAME = "DebugDocking.config";

	public const string MAXIMIZE = "Maximize";

	public const string RESTORE = "Restore";

	public const string TAB_GROUP = "Tab Group";

	public const string HORIZ_SPLIT = "Horizontal Split";

	public const string VERT_SPLIT = "Vertical Split";

	public const string SLIDING_GROUP = "Sliding Group";

	private Dictionary<string, DebugWindowPlugIn> winCollection = new Dictionary<string, DebugWindowPlugIn>(StringComparer.Ordinal);

	private HandleCreateWinDelegate del;

	private HandlePrintWinDelegate printDel;

	private HandleGenericMessageDelegate genericMsgDel;

	private HandleGenericParamMessageDelegate genericParamMsgDel;

	private HandleCustomMessageDelegate customMsgDel;

	private HandleTPL2WinDelegate tpl2Del;

	private HandleHTMLWinDelegate htmlDel;

	private HandleClearDebugMsgDelegate clearDelegate;

	private HandleSaveDebugMsgDelegate saveDelegate;

	private HandleClearAllDebugMsgDelegate clearAllDelegate;

	private HandleSaveAllDebugMsgDelegate saveAllDelegate;

	private HandleSetDebugWindowInFocusDelegate setDebugWindowInFocusDelegate;

	private string TEMP_DEBUG_WINDOW = "Temp";

	private WSpaceConfig wsc;

	private static bool maximized = false;

	private DebugWinCollectionUISettings debugWinCollUISettings = new DebugWinCollectionUISettings();

	private static List<DockConfig> dockedWindowCollection = new List<DockConfig>();

	private DebugWindowsGroupings currentGrouping;

	public DebugWindowsGroupings DebugGrouping
	{
		get
		{
			return currentGrouping;
		}
		set
		{
			currentGrouping = value;
			ArrangeDebugWindows();
		}
	}

	public bool IsDebugWindowsMaximized => maximized;

	public static List<DockConfig> DockedWindowCollection
	{
		get
		{
			return dockedWindowCollection;
		}
		set
		{
			if (value == null || value.Count <= 0)
			{
				return;
			}
			foreach (DockConfig item in value)
			{
				AddToCollection(item);
			}
		}
	}

	public QSPRMainWindow QSPRWindow { get; set; }

	public event CreatingNewWindowEventHandler CreatingNewWindowEvent;

	public event CreatedNewWindowEventHandler CreatedNewWindowEvent;

	public event DebugCollDelegate OnDebugCollEvent;

	public WPFDebugWindowColl()
	{
		InitializeComponent();
		LoadLayout();
		ArrangeDebugWindows();
	}

	private void LoadLayout()
	{
		string dockingFilePath = GetDockingFilePath();
		if (File.Exists(dockingFilePath))
		{
			debugWinCollUISettings = QTMUtility.Deserialize<DebugWinCollectionUISettings>(GetDockingFilePath());
			if (debugWinCollUISettings == null)
			{
				debugWinCollUISettings = new DebugWinCollectionUISettings();
			}
			currentGrouping = debugWinCollUISettings.DebugWinGrouping;
		}
	}

	private void WPFDebugWinColl_Loaded(object sender, RoutedEventArgs e)
	{
		del = HandleDebugMsg;
		genericMsgDel = HandleGenericMsg;
		genericParamMsgDel = HandleGenericParamMsg;
		customMsgDel = HandleCustomMsg;
		tpl2Del = HandleTPL2Msg;
		htmlDel = HandleHTMLMsg;
		clearDelegate = HandleClearDebugMsg;
		saveDelegate = HandleSaveDebugMsg;
		clearAllDelegate = HandleClearAllDebugMsg;
		saveAllDelegate = HandleSaveAllDebugMsg;
		setDebugWindowInFocusDelegate = HandleSetDebugWindowInFocusMsg;
	}

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

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

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

	public void OnTPL2WindowMessage(string windowName, Tpl2Command command, Tpl2Info info)
	{
		this.BeginInvoke(delegate
		{
			tpl2Del(windowName, command, info);
		});
	}

	public void OnHTMLWindowMessage(string windowName, HTMLCommand command, string info)
	{
		this.BeginInvoke(delegate
		{
			htmlDel(windowName, command, info);
		});
	}

	public void OnClearMessage(string strWindowName)
	{
		this.BeginInvoke(delegate
		{
			clearDelegate(strWindowName);
		});
	}

	public void OnSaveMessage(string strWindowName, bool appendText)
	{
		this.BeginInvoke(delegate
		{
			saveDelegate(strWindowName, appendText);
		});
	}

	public void OnClearAllMessage()
	{
		this.BeginInvoke(delegate
		{
			clearAllDelegate();
		});
	}

	public void OnSaveAllMessages(bool appendText)
	{
		this.BeginInvoke(delegate
		{
			saveAllDelegate(appendText);
		});
	}

	public void OnSetDebugWindowInFocus(string debugWindowName)
	{
		this.BeginInvoke(delegate
		{
			setDebugWindowInFocusDelegate(debugWindowName);
		});
	}

	public void OnTextOut(string strWindowName, string strWindowText, TraceLevel traceLevel, bool NoEndOfLine)
	{
		QTMGlobalVariable.GetGlobalVariableObject("CurrentTestDetails", out var gvValue);
		if (((gvValue == null || !((MsgData)gvValue).LoggingEnabled) && (!(strWindowName == TEMP_DEBUG_WINDOW) || !QSPRWindow.WConfig.ShowDebugMessages)) || traceLevel > QSPRWindow.WConfig.DebugTraceLevel || QSPRWindow.WConfig.DebugTraceLevel == TraceLevel.Off || traceLevel == TraceLevel.Off)
		{
			return;
		}
		if (QSPRWindow.WConfig.SeperateTraceLevelWindows)
		{
			switch (traceLevel)
			{
			case TraceLevel.Error:
				strWindowName += " - Error";
				break;
			case TraceLevel.Warning:
				strWindowName += " - Warning";
				break;
			case TraceLevel.Info:
				strWindowName += " - Info";
				break;
			}
		}
		if (!NoEndOfLine)
		{
			strWindowText += "\r\n";
		}
		DebugInfo ti = new DebugInfo(strWindowName, strWindowText, traceLevel);
		this.BeginInvoke(delegate
		{
			del(ti);
		});
	}

	public void AddDebugWindowPlugIn(DebugWindowPlugIn dWin)
	{
		try
		{
			winCollection.Add(dWin.Caption, dWin);
			QSPRWindow._scheduler.GlobalStatus.PluginList.Add(dWin.Caption);
			CreateWindow(dWin);
			dWin.OnConfigInfo(wsc);
		}
		catch (Exception ex)
		{
			AppMessageDialog.Show(ex.Message, DialogResult.OK, MessageDialogType.Exception);
		}
	}

	private void CreateWindow(DebugWindowPlugIn dWin)
	{
		try
		{
			QTMUtility.WriteLogMsg("In CreateWindow method before doing the begin Invoke", QTMUtility.IssueType.ThreadIssue);
			QTMUtility.WriteLogMsg("Current Thread ID: " + Thread.CurrentThread.ManagedThreadId, QTMUtility.IssueType.ThreadIssue);
			QTMUtility.WriteLogMsg("Current Thread aparment state: " + Thread.CurrentThread.GetApartmentState(), QTMUtility.IssueType.ThreadIssue);
			QTMUtility.WriteLogMsg("Current stackTrace: " + Environment.StackTrace, QTMUtility.IssueType.ThreadIssue);
			if (this.InvokeRequired())
			{
				this.BeginInvoke(delegate
				{
					CreateWindow(dWin);
				});
			}
			else
			{
				dWin.Dock = DockStyle.Fill;
				AddNewDebugWindow(dWin, dWin.Caption);
			}
		}
		catch (Exception ex)
		{
			QTMUtility.WriteLogMsg(string.Concat("Exception in CreateWindow; Error: ", ex.Message, ex.InnerException, ex.Source), QTMUtility.IssueType.ThreadIssue);
			throw new Exception(string.Concat("Create Window failed, ", ex.Message, ex.InnerException, ex.Source));
		}
	}

	public void OnConfigInfo(WSpaceConfig wsc)
	{
		this.wsc = wsc;
		foreach (DebugWindowPlugIn value in winCollection.Values)
		{
			try
			{
				value.OnConfigInfo(wsc);
			}
			catch
			{
			}
		}
	}

	public void OpenFile(string fileName)
	{
		try
		{
			if (!File.Exists(fileName))
			{
				AppMessageDialog.Show("File could not be found.", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, DialogResult.OK, MessageDialogType.Warning);
				return;
			}
			try
			{
				foreach (DebugWindowPlugIn value in winCollection.Values)
				{
					value.OpenFile(fileName);
				}
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "OpenFile " + ex.Message, TraceMessageType.Exception);
			}
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "Open file for DebugWinColl failed, " + ex2.Message, TraceMessageType.Exception);
		}
	}

	public void OnMsg(ArrayList messageQueue)
	{
		TraceMessage.Write(ToString(), "OnMsg", TraceMessageType.Trace);
		try
		{
			foreach (DebugWindowPlugIn value in winCollection.Values)
			{
				value.OnMsg(messageQueue);
			}
			if (!QSPRWindow.WConfig.CreateDebugWindowsOnLoading || string.IsNullOrEmpty(QSPRWindow.WConfig.ListofDebugWinNames))
			{
				return;
			}
			foreach (MsgData item in messageQueue)
			{
				if (item.msgType == TestMsgType.OnDLLLoaded)
				{
					string[] array = QSPRWindow.WConfig.ListofDebugWinNames.Split(',');
					foreach (string winName in array)
					{
						HandleDebugMsg(new DebugInfo(winName, "", TraceLevel.Verbose));
					}
					break;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "OnMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void HandleGenericMsg(string messageType, GenericTestData data, string pluginNames)
	{
		try
		{
			List<DebugWindowPlugIn> pluginRecipients = GetPluginRecipients(pluginNames);
			foreach (DebugWindowPlugIn item in pluginRecipients)
			{
				item.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
		{
			List<DebugWindowPlugIn> pluginRecipients = GetPluginRecipients(pluginNames);
			foreach (DebugWindowPlugIn item in pluginRecipients)
			{
				item.OnGenericParamMessage(messageType, id, name, value, units, lowerLimit, upperLimit);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleGenericParamMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	private List<DebugWindowPlugIn> GetPluginRecipients(string pluginNames)
	{
		List<DebugWindowPlugIn> list = new List<DebugWindowPlugIn>();
		try
		{
			string[] array = null;
			if (!string.IsNullOrEmpty(pluginNames))
			{
				array = pluginNames.Split(',');
			}
			foreach (DebugWindowPlugIn value in winCollection.Values)
			{
				try
				{
					if (array != null && array.Length > 0)
					{
						string[] array2 = array;
						foreach (string text in array2)
						{
							if (value.Caption == text)
							{
								list.Add(value);
							}
						}
					}
					else
					{
						list.Add(value);
					}
				}
				catch (Exception ex)
				{
					TraceMessage.Write(ToString(), "GetPluginRecipients " + ex.Message, TraceMessageType.Trace);
				}
			}
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "GetPluginRecipients " + ex2.Message, TraceMessageType.Exception);
		}
		return list;
	}

	public void HandleCustomMsg(object customData, string pluginNames)
	{
		try
		{
			List<DebugWindowPlugIn> pluginRecipients = GetPluginRecipients(pluginNames);
			foreach (DebugWindowPlugIn item in pluginRecipients)
			{
				item.OnCustomMessage(customData);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleCustomMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void HandleTPL2Msg(string windowName, Tpl2Command command, Tpl2Info commandInfo)
	{
		try
		{
			foreach (DebugWindowPlugIn value in winCollection.Values)
			{
				try
				{
					value.OnTpl2Message(windowName, command, commandInfo);
				}
				catch (Exception ex)
				{
					TraceMessage.Write(ToString(), "HandleTPL2Msg " + ex.Message, TraceMessageType.Trace);
				}
			}
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "HandleTPL2Msg " + ex2.Message, TraceMessageType.Exception);
		}
	}

	public void HandleHTMLMsg(string windowName, HTMLCommand command, string commandInfo)
	{
		try
		{
			foreach (DebugWindowPlugIn value in winCollection.Values)
			{
				try
				{
					value.OnHTMLMessage(windowName, command, commandInfo);
				}
				catch (Exception ex)
				{
					TraceMessage.Write(ToString(), "HandleHTMLMsg " + ex.Message, TraceMessageType.Trace);
				}
			}
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "HandleHTMLMsg " + ex2.Message, TraceMessageType.Exception);
		}
	}

	public void HandleDebugMsg(object stateInfo)
	{
		DebugInfo debugInfo = (DebugInfo)stateInfo;
		try
		{
			foreach (DebugWindowPlugIn value in winCollection.Values)
			{
				try
				{
					value.DebugText(debugInfo.strWindowName, debugInfo.strWindowText);
				}
				catch (Exception ex)
				{
					TraceMessage.Write(ToString(), "HandleDebugMsg " + ex.Message, TraceMessageType.Trace);
				}
			}
			DebugWin debugWin = null;
			if (!winCollection.ContainsKey(debugInfo.strWindowName))
			{
				debugWin = new DebugWin(QSPRWindow.WConfig);
				debugWin.WindowName = debugInfo.strWindowName;
				winCollection.Add(debugInfo.strWindowName, debugWin);
				debugWin.OnConfigInfo(wsc);
				OnCreatingNewWindow();
				CreateWindow(debugWin);
				OnCreatedNewWindow();
			}
			else
			{
				debugWin = (DebugWin)winCollection[debugInfo.strWindowName];
			}
			debugWin.OnTextOut(debugInfo.strWindowName, debugInfo.strWindowText);
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "HandleDebugMsg " + ex2.Message, TraceMessageType.Exception);
		}
	}

	protected void OnCreatingNewWindow()
	{
		if (this.CreatingNewWindowEvent != null)
		{
			this.CreatingNewWindowEvent(this, EventArgs.Empty);
		}
	}

	protected void OnCreatedNewWindow()
	{
		if (this.CreatedNewWindowEvent != null)
		{
			this.CreatedNewWindowEvent(this, EventArgs.Empty);
		}
	}

	public void OnPrintMessage(string strWindowName, PrintMsgOptions IsHeader, string[] Headers)
	{
	}

	public void HandleClearDebugMsg(string windowName)
	{
		try
		{
			winCollection[windowName]?.MenuMessages("Clear Debug Windows");
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleClearDebugMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void HandleSaveDebugMsg(string windowName, bool appendText)
	{
		try
		{
			DebugWindowPlugIn debugWindowPlugIn = winCollection[windowName];
			if (debugWindowPlugIn != null)
			{
				debugWindowPlugIn.AppendText = appendText;
				debugWindowPlugIn.MenuMessages("Save Debug Windows");
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleSaveDebugMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void HandleClearAllDebugMsg()
	{
		try
		{
			HandleToolsClick("Clear Debug Windows");
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleClearAllDebugMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void HandleSaveAllDebugMsg(bool appendText)
	{
		try
		{
			foreach (DebugWindowPlugIn value in winCollection.Values)
			{
				try
				{
					value.AppendText = appendText;
					value.MenuMessages("Save Debug Windows");
				}
				catch (Exception ex)
				{
					TraceMessage.Write(ToString(), "HandleToolsClick " + ex.Message, TraceMessageType.Exception);
				}
			}
		}
		catch (Exception ex2)
		{
			TraceMessage.Write(ToString(), "HandleSaveAllDebugMsg " + ex2.Message, TraceMessageType.Exception);
		}
	}

	public void HandleSetDebugWindowInFocusMsg(string debugWindowName)
	{
		try
		{
			if (currentGrouping == DebugWindowsGroupings.Tabbed && DebugWinDockPanel.Children[0] is System.Windows.Controls.TabControl tabControl)
			{
				foreach (TabItem item in (IEnumerable)tabControl.Items)
				{
					if (item.Header.ToString() == debugWindowName)
					{
						item.IsSelected = true;
						break;
					}
				}
			}
			DebugWindowPlugIn debugWindowPlugIn = winCollection[debugWindowName];
			debugWindowPlugIn.Focus();
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "HandleSetDebugWindowInFocusMsg " + ex.Message, TraceMessageType.Exception);
		}
	}

	public void HandleToolsClick(string menu)
	{
		foreach (DebugWindowPlugIn value in winCollection.Values)
		{
			try
			{
				value.MenuMessages(menu);
			}
			catch (Exception ex)
			{
				TraceMessage.Write(ToString(), "HandleToolsClick " + ex.Message, TraceMessageType.Exception);
			}
		}
		switch (menu)
		{
		case "Exit":
			SaveDockingConfiguration();
			break;
		case "Save":
			GetDockedWinCollection();
			break;
		case "New":
		case "Open":
			if (QSPRWindow.WConfig.CreateDebugWindowsOnLoading && !string.IsNullOrEmpty(QSPRWindow.WConfig.ListofDebugWinNames))
			{
				string[] array = QSPRWindow.WConfig.ListofDebugWinNames.Split(',');
				foreach (string winName in array)
				{
					HandleDebugMsg(new DebugInfo(winName, "", TraceLevel.Verbose));
				}
			}
			break;
		}
	}

	private void SaveDockingConfiguration()
	{
		debugWinCollUISettings.DebugWinGrouping = currentGrouping;
		QTMUtility.Serialize(GetDockingFilePath(), debugWinCollUISettings);
	}

	private string GetDockingFilePath()
	{
		return Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), Path.Combine(QSPRMainWindow.FeatureConfigInfo.ConfigDirectory, "WPFDebugDocking.config"));
	}

	private void GetDockedWinCollection()
	{
	}

	public void ResizeDebugWindows(int width, int height)
	{
	}

	public void ShowDebugLayout(int width, int height, string debugWinName)
	{
	}

	public void HideDebugLayout()
	{
	}

	private static void AddToCollection(DockConfig dConfig)
	{
		try
		{
			int debugWindowIndex = GetDebugWindowIndex(dConfig.WindowName);
			if (debugWindowIndex != -1)
			{
				dockedWindowCollection[debugWindowIndex].Copy(dConfig);
			}
			else
			{
				dockedWindowCollection.Add(dConfig);
			}
		}
		catch (Exception ex)
		{
			throw new Exception("AddToCollection failed, " + ex.Message);
		}
	}

	private static int GetDebugWindowIndex(string windowName)
	{
		for (int i = 0; i < dockedWindowCollection.Count; i++)
		{
			DockConfig dockConfig = dockedWindowCollection[i];
			if (dockConfig.WindowName == windowName)
			{
				return i;
			}
		}
		return -1;
	}

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

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

	private void ArrangeDebugWindows()
	{
		switch (currentGrouping)
		{
		case DebugWindowsGroupings.Tabbed:
			ArrangeDebugWindowsInTabbedGrouping();
			break;
		case DebugWindowsGroupings.Horizontal:
			ArrangeDebugWindowsInHorizontalGrouping();
			break;
		case DebugWindowsGroupings.Vertical:
			ArrangeDebugWindowsInVerticalGrouping();
			break;
		}
	}

	private void ArrangeDebugWindowsInTabbedGrouping()
	{
		System.Windows.Controls.TabControl tabControl = new System.Windows.Controls.TabControl();
		tabControl.TabStripPlacement = Dock.Bottom;
		foreach (KeyValuePair<string, DebugWindowPlugIn> item in winCollection)
		{
			AddDebugWindowInTabbedGrouping(tabControl, item.Value, item.Key);
		}
		DebugWinDockPanel.Children.Clear();
		DebugWinDockPanel.Children.Add(tabControl);
	}

	private void AddDebugWindowInTabbedGrouping(System.Windows.Controls.TabControl tabCtrl, DebugWindowPlugIn debugWindowPlugin, string caption)
	{
		TabItem tabItem = new TabItem();
		tabItem.Header = caption;
		TabItem tabItem2 = tabItem;
		QTMUtility.LoadPluginContainer(tabItem2, debugWindowPlugin);
		tabCtrl.Items.Add(tabItem2);
	}

	private void ArrangeDebugWindowsInHorizontalGrouping()
	{
		Grid grid = new Grid();
		foreach (KeyValuePair<string, DebugWindowPlugIn> item in winCollection)
		{
			AddDebugWindowInHorizontalGrouping(grid, item.Value, item.Key);
		}
		DebugWinDockPanel.Children.Clear();
		DebugWinDockPanel.Children.Add(grid);
	}

	private void AddDebugWindowInHorizontalGrouping(Grid gridCtrl, DebugWindowPlugIn debugWindowPlugin, string caption)
	{
		HeaderedContentControl headeredContentControl = new HeaderedContentControl();
		headeredContentControl.Header = caption;
		HeaderedContentControl headeredContentControl2 = headeredContentControl;
		QTMUtility.LoadPluginContainer(headeredContentControl2, debugWindowPlugin);
		gridCtrl.Children.Add(headeredContentControl2);
		gridCtrl.RowDefinitions.Add(new RowDefinition
		{
			Height = new GridLength(1.0, GridUnitType.Star)
		});
		headeredContentControl2.SetValue(Grid.RowProperty, gridCtrl.RowDefinitions.Count - 1);
		GridSplitter gridSplitter = new GridSplitter();
		gridSplitter.Height = 3.0;
		gridSplitter.VerticalAlignment = VerticalAlignment.Stretch;
		gridSplitter.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
		GridSplitter gridSplitter2 = gridSplitter;
		gridCtrl.RowDefinitions.Add(new RowDefinition
		{
			Height = new GridLength(3.0)
		});
		gridCtrl.Children.Add(gridSplitter2);
		gridSplitter2.SetValue(Grid.RowProperty, gridCtrl.RowDefinitions.Count - 1);
	}

	private void ArrangeDebugWindowsInVerticalGrouping()
	{
		Grid grid = new Grid();
		foreach (KeyValuePair<string, DebugWindowPlugIn> item in winCollection)
		{
			AddDebugWindowInVeritcalGrouping(grid, item.Value, item.Key);
		}
		DebugWinDockPanel.Children.Clear();
		DebugWinDockPanel.Children.Add(grid);
	}

	private void AddDebugWindowInVeritcalGrouping(Grid gridCtrl, DebugWindowPlugIn debugWindowPlugin, string caption)
	{
		HeaderedContentControl headeredContentControl = new HeaderedContentControl();
		headeredContentControl.Header = caption;
		HeaderedContentControl headeredContentControl2 = headeredContentControl;
		QTMUtility.LoadPluginContainer(headeredContentControl2, debugWindowPlugin);
		gridCtrl.Children.Add(headeredContentControl2);
		gridCtrl.ColumnDefinitions.Add(new ColumnDefinition
		{
			Width = new GridLength(1.0, GridUnitType.Star)
		});
		headeredContentControl2.SetValue(Grid.ColumnProperty, gridCtrl.ColumnDefinitions.Count - 1);
		GridSplitter gridSplitter = new GridSplitter();
		gridSplitter.Width = 3.0;
		gridSplitter.VerticalAlignment = VerticalAlignment.Stretch;
		gridSplitter.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
		GridSplitter gridSplitter2 = gridSplitter;
		gridCtrl.ColumnDefinitions.Add(new ColumnDefinition
		{
			Width = new GridLength(3.0)
		});
		gridCtrl.Children.Add(gridSplitter2);
		gridSplitter2.SetValue(Grid.ColumnProperty, gridCtrl.ColumnDefinitions.Count - 1);
	}

	private void AddNewDebugWindow(DebugWindowPlugIn plugin, string caption)
	{
		switch (currentGrouping)
		{
		case DebugWindowsGroupings.Tabbed:
			if (DebugWinDockPanel.Children[0] is System.Windows.Controls.TabControl tabCtrl)
			{
				AddDebugWindowInTabbedGrouping(tabCtrl, plugin, caption);
			}
			break;
		case DebugWindowsGroupings.Horizontal:
			if (DebugWinDockPanel.Children[0] is Grid gridCtrl2)
			{
				AddDebugWindowInHorizontalGrouping(gridCtrl2, plugin, caption);
			}
			break;
		case DebugWindowsGroupings.Vertical:
			if (DebugWinDockPanel.Children[0] is Grid gridCtrl)
			{
				AddDebugWindowInVeritcalGrouping(gridCtrl, plugin, caption);
			}
			break;
		}
	}
}
