using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using QC.QsprShared.Config;
using QC.QsprShared.Trace;
using QC.QsprTestObjects.Loops;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Tests.Base;

namespace QC.QTMApplication;

public class NodeDetailsForm : Form
{
	private BaseTest m_BaseTest;

	private bool m_IncludeProperty;

	private bool m_IncludeParam;

	private bool m_IncludeGlobalValues;

	private bool m_IncludeNotes;

	private bool m_IncludeLoop;

	private string m_Padding = "     ";

	private static string NOTEPAD_PROCESS = "notepad.exe";

	private static string TEMP_FILE_DIRECTORY = QSPRMainWindow.FeatureConfigInfo.TempDirectory;

	private static string TEMP_FILE_NAME = "NodeDetails.txt";

	private sbyte m_IncludePropertyByte = 16;

	private sbyte m_InclureParamByte = 8;

	private sbyte m_includeGlobalValuesByte = 4;

	private sbyte m_IncludeNoteByte = 2;

	private sbyte m_IncludeLoopByte = 1;

	private WorkspaceConfig WConfig;

	private IContainer components;

	private Button btnExportNodeData;

	private TextBox txtNodeDetails;

	private CheckBox chbIncludeParams;

	private CheckBox chbIncludeProperties;

	private CheckBox chbIncludeNotes;

	private CheckBox chbIncludeLoopConditions;

	private CheckBox chbIncludeGlobalValues;

	private Button btnShowInNotepad;

	private void chbIncludeProperties_CheckedChanged(object sender, EventArgs e)
	{
		m_IncludeProperty = ((CheckBox)sender).Checked;
		PopulateNodeData();
	}

	private void chbIncludeParams_CheckedChanged(object sender, EventArgs e)
	{
		m_IncludeParam = ((CheckBox)sender).Checked;
		chbIncludeGlobalValues.Enabled = m_IncludeParam;
		PopulateNodeData();
	}

	private void chbIncludeGlobalValues_CheckChanged(object sender, EventArgs e)
	{
		m_IncludeGlobalValues = ((CheckBox)sender).Checked;
		PopulateNodeData();
	}

	private void chbIncludeNotes_CheckedChanged(object sender, EventArgs e)
	{
		m_IncludeNotes = ((CheckBox)sender).Checked;
		PopulateNodeData();
	}

	private void chbIncludeLoops_CheckedChanged(object sender, EventArgs e)
	{
		m_IncludeLoop = ((CheckBox)sender).Checked;
		PopulateNodeData();
	}

	private void btnExportNodeData_Click(object sender, EventArgs e)
	{
		SaveFileDialog saveFileDialog = new SaveFileDialog();
		saveFileDialog.Filter = "Text file|*.txt";
		saveFileDialog.Title = "Exporting Node Details";
		if (saveFileDialog.ShowDialog() == DialogResult.OK)
		{
			ExportDataToFile(saveFileDialog.FileName);
			MessageBox.Show("Node details exported in " + saveFileDialog.FileName, "Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}
	}

	private void btnNotepad_Click(object sender, EventArgs e)
	{
		OpenDataInNotepad();
	}

	private void NodeDetailsForm_FormClosing(object sender, FormClosingEventArgs e)
	{
		SaveSelectionInConfig();
	}

	public NodeDetailsForm(BaseTest selectedBaseTest, WorkspaceConfig wConfig)
	{
		WConfig = wConfig;
		InitializeComponent();
		m_BaseTest = selectedBaseTest;
		LoadValuesFromConfig();
		chbIncludeProperties.Click += chbIncludeProperties_CheckedChanged;
		chbIncludeParams.Click += chbIncludeParams_CheckedChanged;
		chbIncludeGlobalValues.Click += chbIncludeGlobalValues_CheckChanged;
		chbIncludeNotes.Click += chbIncludeNotes_CheckedChanged;
		chbIncludeLoopConditions.Click += chbIncludeLoops_CheckedChanged;
		PopulateNodeData();
	}

	public NodeDetailsForm(BaseTest selectedBaseTest, bool includeProperties, bool includeParam, bool includeGlobalVal, bool includeNotes, bool includeLoop)
	{
		InitializeComponent();
		m_BaseTest = selectedBaseTest;
		m_IncludeProperty = includeProperties;
		m_IncludeParam = includeParam;
		m_IncludeGlobalValues = includeGlobalVal;
		m_IncludeNotes = includeNotes;
		m_IncludeLoop = includeLoop;
		PopulateNodeData();
	}

	public bool ExportDataToFile(string fileName)
	{
		bool result = false;
		try
		{
			File.WriteAllText(fileName, txtNodeDetails.Text);
			result = true;
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "ExportDataToFile " + ex.Message, TraceMessageType.Exception);
		}
		return result;
	}

	private void PopulateNodeData()
	{
		txtNodeDetails.Text = GetLegendInformation() + GetAllNodesDetails(m_BaseTest);
	}

	private string GetAllNodesDetails(BaseTest nodeBaseTest)
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append(GetSingleNodeDetails(nodeBaseTest));
		if (nodeBaseTest is FolderTest)
		{
			FolderTest folderTest = (FolderTest)nodeBaseTest;
			ObservableCollection<BaseTest> testList = folderTest.TestList;
			foreach (BaseTest item in testList)
			{
				stringBuilder.Append(GetAllNodesDetails(item));
			}
		}
		return stringBuilder.ToString();
	}

	private string GetLegendInformation()
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append("Legend:" + Environment.NewLine);
		stringBuilder.Append(m_Padding + "Input Parameter Format: <Parameter Name>: <value>, <Unit>, <Description>, (if selected)<GV Set>, <GV Get>" + Environment.NewLine);
		stringBuilder.Append(m_Padding + "Output Parameter Format: <Parameter Name>: <LL>-<UL>, <Unit>, <Description>, (if selected)<GV Set>, <GV Get>, <GV Ll Get>, <GV UL Get>" + Environment.NewLine);
		stringBuilder.Append(m_Padding + "Note: If any of the format value is NULL, it will be shown as a blank space in the format" + Environment.NewLine);
		stringBuilder.Append("=============================================================" + Environment.NewLine);
		return stringBuilder.ToString();
	}

	private string GetSingleNodeDetails(BaseTest nodeBaseTest)
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.AppendLine(GetNodeName(nodeBaseTest));
		if (m_IncludeProperty)
		{
			stringBuilder.Append(GetNodeProperties(nodeBaseTest));
		}
		if (m_IncludeParam)
		{
			stringBuilder.Append(GetNodeParameters(nodeBaseTest));
		}
		if (m_IncludeNotes)
		{
			stringBuilder.Append(GetNodeNotes(nodeBaseTest));
		}
		if (m_IncludeLoop)
		{
			stringBuilder.Append(GetNodeLoopConditions(nodeBaseTest));
		}
		stringBuilder.Append(Environment.NewLine);
		return stringBuilder.ToString();
	}

	private string GetNodeName(BaseTest nodeBaseTest)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (nodeBaseTest is FolderTest)
		{
			stringBuilder.Append("Folder: ");
		}
		else
		{
			stringBuilder.Append("Test: ");
		}
		stringBuilder.Append(nodeBaseTest.Properties.TestName + Environment.NewLine);
		stringBuilder.Append("Selected to Run? " + nodeBaseTest.Properties.CheckStateTest);
		return stringBuilder.ToString();
	}

	private string GetNodeProperties(BaseTest baseTestNode)
	{
		StringBuilder stringBuilder = new StringBuilder();
		SortedDictionary<string, object> sortedDictionary = new SortedDictionary<string, object>();
		PropertyGrid propertyGrid = new PropertyGrid();
		propertyGrid.SelectedObject = baseTestNode.Properties;
		Attribute[] array = new Attribute[propertyGrid.BrowsableAttributes.Count];
		propertyGrid.BrowsableAttributes.CopyTo(array, 0);
		foreach (PropertyDescriptor property in propertyGrid.SelectedTab.GetProperties(propertyGrid.SelectedObject, array))
		{
			sortedDictionary.Add(property.Name, property.GetValue(propertyGrid.SelectedObject));
		}
		foreach (string key in sortedDictionary.Keys)
		{
			stringBuilder.Append(string.Concat(m_Padding, key, ": ", sortedDictionary[key], Environment.NewLine));
		}
		return stringBuilder.ToString();
	}

	private string GetNodeParameters(BaseTest nodeBaseTest)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (ShouldPopulateParams(nodeBaseTest))
		{
			StringBuilder stringBuilder2 = new StringBuilder();
			StringBuilder stringBuilder3 = new StringBuilder();
			foreach (ParameterCls parameter in nodeBaseTest.Properties.Parameters)
			{
				if (parameter.IsInput)
				{
					stringBuilder2.Append(m_Padding + m_Padding + parameter.Name + ": " + parameter.Value + ", " + parameter.Unit + ", " + parameter.Description);
					if (m_IncludeGlobalValues)
					{
						stringBuilder2.Append("," + parameter.GlobalVariableNameOther + ", " + parameter.GlobalVariableName);
					}
					stringBuilder2.Append(Environment.NewLine);
				}
				else if (parameter.IsOutput)
				{
					stringBuilder3.Append(m_Padding + m_Padding + parameter.Name + ": " + parameter.LowerLimit + "-" + parameter.UpperLimit + ", " + parameter.Unit + ", " + parameter.Description);
					if (m_IncludeGlobalValues)
					{
						stringBuilder3.Append("," + parameter.GlobalVariableNameOther + ", " + parameter.GlobalVariableName + ", " + parameter.GlobalVariableForLL + ", " + parameter.GlobalVariableForUL);
					}
					stringBuilder3.Append(Environment.NewLine);
				}
			}
			stringBuilder.Append("Parameters:" + Environment.NewLine);
			stringBuilder.Append(m_Padding + "Input Parameters:" + Environment.NewLine);
			stringBuilder.Append(stringBuilder2);
			stringBuilder.Append(m_Padding + "Output Parameters:" + Environment.NewLine);
			stringBuilder.Append(stringBuilder3);
		}
		return stringBuilder.ToString();
	}

	private bool ShouldPopulateParams(BaseTest nodeBaseTest)
	{
		bool result = true;
		if (nodeBaseTest is FolderTest)
		{
			result = false;
		}
		else if (nodeBaseTest.Properties.Parameters == null)
		{
			result = false;
		}
		return result;
	}

	private string GetNodeNotes(BaseTest baseTestNode)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (!string.IsNullOrEmpty(baseTestNode.Properties.Notes))
		{
			stringBuilder.Append("Notes:" + Environment.NewLine);
			stringBuilder.Append(baseTestNode.Properties.Notes + Environment.NewLine);
		}
		return stringBuilder.ToString();
	}

	private string GetNodeLoopConditions(BaseTest nodeBaseTest)
	{
		StringBuilder stringBuilder = new StringBuilder();
		if (ShouldPopulateLoopConditions(nodeBaseTest))
		{
			stringBuilder.Append("Loop Conditions:" + Environment.NewLine);
			LoopScenarioValues loopScenarioValues = ((nodeBaseTest is FolderTest) ? ((FolderTest)nodeBaseTest).FolderProperties.Scenarios : nodeBaseTest.Properties.Scenarios);
			if (loopScenarioValues != null)
			{
				for (int i = 0; i < loopScenarioValues.LoopConditions.Count; i++)
				{
					LoopCondition loopCondition = (LoopCondition)loopScenarioValues.LoopConditions[i];
					stringBuilder.Append(m_Padding + loopCondition.LoopName + ": " + GetCSVFromArray(loopCondition.LoopValues) + Environment.NewLine);
				}
			}
		}
		return stringBuilder.ToString();
	}

	private bool ShouldPopulateLoopConditions(BaseTest nodeBaseTest)
	{
		bool result = true;
		if (nodeBaseTest is FolderTest)
		{
			if (((FolderTest)nodeBaseTest).Properties.Scenarios == null)
			{
				result = false;
			}
		}
		else if (nodeBaseTest.Properties.Scenarios == null)
		{
			result = false;
		}
		return result;
	}

	private string GetCSVFromArray(ArrayList loopValues)
	{
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < loopValues.Count; i++)
		{
			stringBuilder.Append(string.Concat(loopValues[i], ","));
		}
		return stringBuilder.ToString().TrimEnd(',');
	}

	private void LoadValuesFromConfig()
	{
		bool includeProperty = (chbIncludeProperties.Checked = Convert.ToBoolean(WConfig.NodeDetailsChoice & m_IncludePropertyByte));
		m_IncludeProperty = includeProperty;
		bool includeParam = (chbIncludeParams.Checked = Convert.ToBoolean(WConfig.NodeDetailsChoice & m_InclureParamByte));
		m_IncludeParam = includeParam;
		bool includeGlobalValues = (chbIncludeGlobalValues.Checked = Convert.ToBoolean(WConfig.NodeDetailsChoice & m_includeGlobalValuesByte));
		m_IncludeGlobalValues = includeGlobalValues;
		bool includeNotes = (chbIncludeNotes.Checked = Convert.ToBoolean(WConfig.NodeDetailsChoice & m_IncludeNoteByte));
		m_IncludeNotes = includeNotes;
		bool includeLoop = (chbIncludeLoopConditions.Checked = Convert.ToBoolean(WConfig.NodeDetailsChoice & m_IncludeLoopByte));
		m_IncludeLoop = includeLoop;
		chbIncludeGlobalValues.Enabled = chbIncludeParams.Checked;
		txtNodeDetails.Font = WConfig.Font;
	}

	private void SaveSelectionInConfig()
	{
		sbyte b = 0;
		if (chbIncludeProperties.Checked)
		{
			b += m_IncludePropertyByte;
		}
		if (chbIncludeParams.Checked)
		{
			b += m_InclureParamByte;
		}
		if (chbIncludeGlobalValues.Checked)
		{
			b += m_includeGlobalValuesByte;
		}
		if (chbIncludeNotes.Checked)
		{
			b += m_IncludeNoteByte;
		}
		if (chbIncludeLoopConditions.Checked)
		{
			b += m_IncludeLoopByte;
		}
		WConfig.NodeDetailsChoice = b;
	}

	private void OpenDataInNotepad()
	{
		if (!Directory.Exists(TEMP_FILE_DIRECTORY))
		{
			Directory.CreateDirectory(TEMP_FILE_DIRECTORY);
		}
		if (ExportDataToFile(Path.Combine(TEMP_FILE_DIRECTORY, TEMP_FILE_NAME)))
		{
			Process.Start(NOTEPAD_PROCESS, Path.Combine(TEMP_FILE_DIRECTORY, TEMP_FILE_NAME));
		}
		else
		{
			MessageBox.Show("Error while Launching data in Notepad", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
		}
	}

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

	private void InitializeComponent()
	{
		this.btnExportNodeData = new System.Windows.Forms.Button();
		this.txtNodeDetails = new System.Windows.Forms.TextBox();
		this.chbIncludeParams = new System.Windows.Forms.CheckBox();
		this.chbIncludeProperties = new System.Windows.Forms.CheckBox();
		this.chbIncludeNotes = new System.Windows.Forms.CheckBox();
		this.chbIncludeLoopConditions = new System.Windows.Forms.CheckBox();
		this.chbIncludeGlobalValues = new System.Windows.Forms.CheckBox();
		this.btnShowInNotepad = new System.Windows.Forms.Button();
		base.SuspendLayout();
		this.btnExportNodeData.Location = new System.Drawing.Point(370, 12);
		this.btnExportNodeData.Name = "btnExportNodeData";
		this.btnExportNodeData.Size = new System.Drawing.Size(75, 23);
		this.btnExportNodeData.TabIndex = 0;
		this.btnExportNodeData.Text = "Export...";
		this.btnExportNodeData.UseVisualStyleBackColor = true;
		this.btnExportNodeData.Click += new System.EventHandler(btnExportNodeData_Click);
		this.txtNodeDetails.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.txtNodeDetails.BackColor = System.Drawing.SystemColors.Window;
		this.txtNodeDetails.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.txtNodeDetails.Location = new System.Drawing.Point(2, 70);
		this.txtNodeDetails.Multiline = true;
		this.txtNodeDetails.Name = "txtNodeDetails";
		this.txtNodeDetails.ReadOnly = true;
		this.txtNodeDetails.ScrollBars = System.Windows.Forms.ScrollBars.Both;
		this.txtNodeDetails.Size = new System.Drawing.Size(451, 426);
		this.txtNodeDetails.TabIndex = 1;
		this.chbIncludeParams.AutoSize = true;
		this.chbIncludeParams.Location = new System.Drawing.Point(97, 16);
		this.chbIncludeParams.Name = "chbIncludeParams";
		this.chbIncludeParams.Size = new System.Drawing.Size(79, 17);
		this.chbIncludeParams.TabIndex = 2;
		this.chbIncludeParams.Text = "Parameters";
		this.chbIncludeParams.UseVisualStyleBackColor = true;
		this.chbIncludeProperties.AutoSize = true;
		this.chbIncludeProperties.Location = new System.Drawing.Point(12, 16);
		this.chbIncludeProperties.Name = "chbIncludeProperties";
		this.chbIncludeProperties.Size = new System.Drawing.Size(73, 17);
		this.chbIncludeProperties.TabIndex = 3;
		this.chbIncludeProperties.Text = "Properties";
		this.chbIncludeProperties.UseVisualStyleBackColor = true;
		this.chbIncludeNotes.AutoSize = true;
		this.chbIncludeNotes.Location = new System.Drawing.Point(188, 16);
		this.chbIncludeNotes.Name = "chbIncludeNotes";
		this.chbIncludeNotes.Size = new System.Drawing.Size(54, 17);
		this.chbIncludeNotes.TabIndex = 4;
		this.chbIncludeNotes.Text = "Notes";
		this.chbIncludeNotes.UseVisualStyleBackColor = true;
		this.chbIncludeLoopConditions.AutoSize = true;
		this.chbIncludeLoopConditions.Location = new System.Drawing.Point(254, 16);
		this.chbIncludeLoopConditions.Name = "chbIncludeLoopConditions";
		this.chbIncludeLoopConditions.Size = new System.Drawing.Size(102, 17);
		this.chbIncludeLoopConditions.TabIndex = 5;
		this.chbIncludeLoopConditions.Text = "Loop Conditions";
		this.chbIncludeLoopConditions.UseVisualStyleBackColor = true;
		this.chbIncludeGlobalValues.AutoSize = true;
		this.chbIncludeGlobalValues.Location = new System.Drawing.Point(107, 45);
		this.chbIncludeGlobalValues.Name = "chbIncludeGlobalValues";
		this.chbIncludeGlobalValues.Size = new System.Drawing.Size(91, 17);
		this.chbIncludeGlobalValues.TabIndex = 6;
		this.chbIncludeGlobalValues.Text = "Global Values";
		this.chbIncludeGlobalValues.UseVisualStyleBackColor = true;
		this.btnShowInNotepad.Location = new System.Drawing.Point(346, 41);
		this.btnShowInNotepad.Name = "btnShowInNotepad";
		this.btnShowInNotepad.Size = new System.Drawing.Size(99, 23);
		this.btnShowInNotepad.TabIndex = 7;
		this.btnShowInNotepad.Text = "Show in Notepad";
		this.btnShowInNotepad.UseVisualStyleBackColor = true;
		this.btnShowInNotepad.Click += new System.EventHandler(btnNotepad_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(457, 501);
		base.Controls.Add(this.btnShowInNotepad);
		base.Controls.Add(this.chbIncludeGlobalValues);
		base.Controls.Add(this.chbIncludeLoopConditions);
		base.Controls.Add(this.chbIncludeNotes);
		base.Controls.Add(this.chbIncludeProperties);
		base.Controls.Add(this.chbIncludeParams);
		base.Controls.Add(this.txtNodeDetails);
		base.Controls.Add(this.btnExportNodeData);
		base.MaximizeBox = false;
		this.MaximumSize = new System.Drawing.Size(473, 539);
		base.MinimizeBox = false;
		this.MinimumSize = new System.Drawing.Size(473, 539);
		base.Name = "NodeDetailsForm";
		base.ShowIcon = false;
		base.ShowInTaskbar = false;
		base.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
		this.Text = "Node Details";
		base.FormClosing += new System.Windows.Forms.FormClosingEventHandler(NodeDetailsForm_FormClosing);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
