using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMInterface;

namespace QC.QsprTestObjects.TestConditions;

public class PostConditionDialog : Form
{
	private const int FOLDER_IMAGE = 1;

	private const int FOLDER_SELECTED_IMAGE = 2;

	private const int SCENARIO_IMAGE = 1;

	private const int TEST_IMAGE = 3;

	private const int ROOT_IMAGE = 0;

	private const int SCENARIO_SELECTED_IMAGE = 2;

	private Button btnOk;

	private Label label1;

	private ComboBox ultraComboPostCondition;

	private Button btnCancel;

	private TreeView ultraTreeView;

	private CreateConditionUC createCondition;

	private IContainer components;

	private ConditionMgr postConditionMgr = new ConditionMgr();

	private BaseCondition conditionSelected;

	private BaseCondition conditionToModify;

	private bool ignoreSelectionChange;

	private bool conditionSelectedIsNew;

	private Hashtable nodeCollection = new Hashtable();

	private bool currentNodeFound;

	private string CurrentNodeIndex = "";

	private BaseTest CurrentTest;

	private string dialogTypeStr = "Post Condition";

	private ImageList imageList;

	private FormulaBuilderUC formulaBuilderUC1;

	private Button btnSaveCondition;

	private GroupBox groupBox1;

	private Button btnNewCondition;

	private GroupBox groupBox2;

	private Button btnDeleteCondition;

	private Label label2;

	private RadioButton radioMet;

	private RadioButton radioNotMet;

	private Button btnDeleteAllConditions;

	private ToolTip toolTip1;

	private TextBox txtBoxBranchTestName;

	private Label labelBranchWarning;

	private CheckBox chkBoxBranchByName;

	private CheckBox chkConditionActive;

	private bool ignoreSelection;

	public ConditionMgr PostConditionMgr
	{
		get
		{
			return postConditionMgr;
		}
		set
		{
			postConditionMgr = value;
		}
	}

	public ParameterCollection Parameters
	{
		set
		{
			createCondition.Parameters = value;
		}
	}

	public PostConditionDialog(BaseTest currentTest)
	{
		InitializeComponent();
		List<KeyValuePair<string, string>> dataSource = new Dictionary<string, string>
		{
			{ "SkipTheFolder", "Skip The Folder" },
			{ "SkipToNextLoopIteration", "Skip To Next Loop Iteration" },
			{ "SkipToFirstTestAfterTheLoop", "Skip To First Test After The Loop" },
			{ "SkipToNextCategory", "Skip To Next Category" },
			{ "CompleteStop", "Complete Stop" },
			{ "CompleteStopNoRetry", "Complete Stop NoRetry" },
			{ "IgnoreSession", "Ignore Session" },
			{ "RestartTestTree", "Restart Test Tree" },
			{ "RestartTestTreeWithoutMustRuns", "Restart Test Tree Without Must Runs" },
			{ "Branch", "Branch" },
			{ "PauseTestTree", "Pause Test Tree" },
			{ "None", "None" }
		}.ToList();
		ultraComboPostCondition.DataSource = dataSource;
		ultraComboPostCondition.SelectedValue = "None";
		formulaBuilderUC1.AddGridSelectionChangeHandler(conditionGrid_SelectionChange);
		CurrentNodeIndex = currentTest.Index;
		CurrentTest = currentTest;
	}

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

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(QC.QsprTestObjects.TestConditions.PostConditionDialog));
		this.btnOk = new System.Windows.Forms.Button();
		this.label1 = new System.Windows.Forms.Label();
		this.ultraComboPostCondition = new System.Windows.Forms.ComboBox();
		this.btnCancel = new System.Windows.Forms.Button();
		this.ultraTreeView = new System.Windows.Forms.TreeView();
		this.imageList = new System.Windows.Forms.ImageList(this.components);
		this.btnSaveCondition = new System.Windows.Forms.Button();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.btnDeleteAllConditions = new System.Windows.Forms.Button();
		this.btnDeleteCondition = new System.Windows.Forms.Button();
		this.btnNewCondition = new System.Windows.Forms.Button();
		this.createCondition = new QC.QsprTestObjects.TestConditions.CreateConditionUC();
		this.groupBox2 = new System.Windows.Forms.GroupBox();
		this.formulaBuilderUC1 = new QC.QsprTestObjects.TestConditions.FormulaBuilderUC();
		this.label2 = new System.Windows.Forms.Label();
		this.radioMet = new System.Windows.Forms.RadioButton();
		this.radioNotMet = new System.Windows.Forms.RadioButton();
		this.chkConditionActive = new System.Windows.Forms.CheckBox();
		this.txtBoxBranchTestName = new System.Windows.Forms.TextBox();
		this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
		this.chkBoxBranchByName = new System.Windows.Forms.CheckBox();
		this.labelBranchWarning = new System.Windows.Forms.Label();
		this.groupBox1.SuspendLayout();
		this.groupBox2.SuspendLayout();
		base.SuspendLayout();
		this.btnOk.BackColor = System.Drawing.Color.White;
		this.btnOk.Location = new System.Drawing.Point(401, 522);
		this.btnOk.Name = "btnOk";
		this.btnOk.Size = new System.Drawing.Size(75, 23);
		this.btnOk.TabIndex = 3;
		this.btnOk.Text = "Ok";
		this.btnOk.UseVisualStyleBackColor = true;
		this.btnOk.Click += new System.EventHandler(btnOk_Click);
		this.label1.Location = new System.Drawing.Point(9, 522);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(76, 23);
		this.label1.TabIndex = 1;
		this.label1.Text = "Select Action:";
		this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
		this.ultraComboPostCondition.DisplayMember = "Value";
		this.ultraComboPostCondition.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.ultraComboPostCondition.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.ultraComboPostCondition.Location = new System.Drawing.Point(91, 524);
		this.ultraComboPostCondition.Name = "ultraComboPostCondition";
		this.ultraComboPostCondition.Size = new System.Drawing.Size(204, 21);
		this.ultraComboPostCondition.TabIndex = 2;
		this.ultraComboPostCondition.ValueMember = "Key";
		this.ultraComboPostCondition.SelectedValueChanged += new System.EventHandler(ultraComboPostCondition_ValueChanged);
		this.btnCancel.BackColor = System.Drawing.Color.White;
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(320, 522);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.ultraTreeView.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawText;
		this.ultraTreeView.HideSelection = false;
		this.ultraTreeView.ImageIndex = 0;
		this.ultraTreeView.ImageList = this.imageList;
		this.ultraTreeView.Location = new System.Drawing.Point(6, 550);
		this.ultraTreeView.Name = "ultraTreeView";
		this.ultraTreeView.SelectedImageIndex = 0;
		this.ultraTreeView.Size = new System.Drawing.Size(470, 114);
		this.ultraTreeView.TabIndex = 6;
		this.ultraTreeView.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(ultraTreeView_DrawNode);
		this.ultraTreeView.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(ultraTreeView_BeforeSelect);
		this.imageList.ImageStream = (System.Windows.Forms.ImageListStreamer)resources.GetObject("imageList.ImageStream");
		this.imageList.TransparentColor = System.Drawing.Color.Transparent;
		this.imageList.Images.SetKeyName(0, "Root.png");
		this.imageList.Images.SetKeyName(1, "folder-closed.png");
		this.imageList.Images.SetKeyName(2, "folder-open.png");
		this.imageList.Images.SetKeyName(3, "TestTreeTest.png");
		this.imageList.Images.SetKeyName(4, "");
		this.imageList.Images.SetKeyName(5, "");
		this.btnSaveCondition.Enabled = false;
		this.btnSaveCondition.Location = new System.Drawing.Point(367, 241);
		this.btnSaveCondition.Name = "btnSaveCondition";
		this.btnSaveCondition.Size = new System.Drawing.Size(99, 23);
		this.btnSaveCondition.TabIndex = 8;
		this.btnSaveCondition.Text = "Save Condition";
		this.btnSaveCondition.UseVisualStyleBackColor = true;
		this.btnSaveCondition.Click += new System.EventHandler(btnSaveCondition_Click);
		this.groupBox1.Controls.Add(this.btnDeleteAllConditions);
		this.groupBox1.Controls.Add(this.btnDeleteCondition);
		this.groupBox1.Controls.Add(this.btnNewCondition);
		this.groupBox1.Controls.Add(this.createCondition);
		this.groupBox1.Controls.Add(this.btnSaveCondition);
		this.groupBox1.Location = new System.Drawing.Point(4, 1);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(472, 270);
		this.groupBox1.TabIndex = 9;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "Condition Editor";
		this.btnDeleteAllConditions.Location = new System.Drawing.Point(218, 241);
		this.btnDeleteAllConditions.Name = "btnDeleteAllConditions";
		this.btnDeleteAllConditions.Size = new System.Drawing.Size(123, 23);
		this.btnDeleteAllConditions.TabIndex = 11;
		this.btnDeleteAllConditions.Text = "Delete All Conditions";
		this.btnDeleteAllConditions.UseVisualStyleBackColor = true;
		this.btnDeleteAllConditions.Click += new System.EventHandler(btnDeleteAllConditions_Click);
		this.btnDeleteCondition.Enabled = false;
		this.btnDeleteCondition.Location = new System.Drawing.Point(114, 241);
		this.btnDeleteCondition.Name = "btnDeleteCondition";
		this.btnDeleteCondition.Size = new System.Drawing.Size(98, 23);
		this.btnDeleteCondition.TabIndex = 10;
		this.btnDeleteCondition.Text = "Delete Condition";
		this.btnDeleteCondition.UseVisualStyleBackColor = true;
		this.btnDeleteCondition.Click += new System.EventHandler(btnDeleteCondition_Click);
		this.btnNewCondition.Location = new System.Drawing.Point(10, 241);
		this.btnNewCondition.Name = "btnNewCondition";
		this.btnNewCondition.Size = new System.Drawing.Size(98, 23);
		this.btnNewCondition.TabIndex = 9;
		this.btnNewCondition.Text = "New Condition";
		this.btnNewCondition.UseVisualStyleBackColor = true;
		this.btnNewCondition.Click += new System.EventHandler(btnNewCondition_Click);
		this.createCondition.BackColor = System.Drawing.Color.White;
		this.createCondition.Condition = null;
		this.createCondition.Enabled = false;
		this.createCondition.Location = new System.Drawing.Point(6, 14);
		this.createCondition.Name = "createCondition";
		this.createCondition.Size = new System.Drawing.Size(460, 226);
		this.createCondition.TabIndex = 5;
		this.groupBox2.Controls.Add(this.formulaBuilderUC1);
		this.groupBox2.Location = new System.Drawing.Point(4, 277);
		this.groupBox2.Name = "groupBox2";
		this.groupBox2.Size = new System.Drawing.Size(472, 222);
		this.groupBox2.TabIndex = 10;
		this.groupBox2.TabStop = false;
		this.groupBox2.Text = "Formula Editor";
		this.formulaBuilderUC1.BackColor = System.Drawing.SystemColors.Window;
		this.formulaBuilderUC1.Expression = "";
		this.formulaBuilderUC1.Location = new System.Drawing.Point(6, 17);
		this.formulaBuilderUC1.Name = "formulaBuilderUC1";
		this.formulaBuilderUC1.Size = new System.Drawing.Size(460, 199);
		this.formulaBuilderUC1.TabIndex = 7;
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(9, 505);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(166, 13);
		this.label2.TabIndex = 11;
		this.label2.Text = "Action will be taken if condition is:";
		this.radioMet.AutoSize = true;
		this.radioMet.Checked = true;
		this.radioMet.Location = new System.Drawing.Point(181, 503);
		this.radioMet.Name = "radioMet";
		this.radioMet.Size = new System.Drawing.Size(42, 17);
		this.radioMet.TabIndex = 12;
		this.radioMet.TabStop = true;
		this.radioMet.Text = "met";
		this.radioMet.UseVisualStyleBackColor = true;
		this.radioNotMet.AutoSize = true;
		this.radioNotMet.Location = new System.Drawing.Point(229, 503);
		this.radioNotMet.Name = "radioNotMet";
		this.radioNotMet.Size = new System.Drawing.Size(60, 17);
		this.radioNotMet.TabIndex = 13;
		this.radioNotMet.Text = "not met";
		this.radioNotMet.UseVisualStyleBackColor = true;
		this.chkConditionActive.AutoSize = true;
		this.chkConditionActive.Checked = true;
		this.chkConditionActive.CheckState = System.Windows.Forms.CheckState.Checked;
		this.chkConditionActive.Location = new System.Drawing.Point(6, 712);
		this.chkConditionActive.Name = "chkConditionActive";
		this.chkConditionActive.Size = new System.Drawing.Size(103, 17);
		this.chkConditionActive.TabIndex = 8;
		this.chkConditionActive.Text = "Condition Active";
		this.chkConditionActive.UseVisualStyleBackColor = true;
		this.chkConditionActive.CheckedChanged += new System.EventHandler(chkConditionActive_CheckedChanged);
		this.txtBoxBranchTestName.Location = new System.Drawing.Point(151, 671);
		this.txtBoxBranchTestName.Name = "txtBoxBranchTestName";
		this.txtBoxBranchTestName.Size = new System.Drawing.Size(162, 20);
		this.txtBoxBranchTestName.TabIndex = 15;
		this.toolTip1.SetToolTip(this.txtBoxBranchTestName, "Enter a testname to branch to.\r\nThis will branch to the next test that matches this name.");
		this.txtBoxBranchTestName.LostFocus += new System.EventHandler(txtBoxBranchTestName_LostFocus);
		this.toolTip1.AutomaticDelay = 200;
		this.toolTip1.AutoPopDelay = 5000;
		this.toolTip1.InitialDelay = 200;
		this.toolTip1.ReshowDelay = 40;
		this.chkBoxBranchByName.AutoSize = true;
		this.chkBoxBranchByName.Location = new System.Drawing.Point(14, 673);
		this.chkBoxBranchByName.Name = "chkBoxBranchByName";
		this.chkBoxBranchByName.Size = new System.Drawing.Size(131, 17);
		this.chkBoxBranchByName.TabIndex = 17;
		this.chkBoxBranchByName.Text = "Branch To TestName:";
		this.toolTip1.SetToolTip(this.chkBoxBranchByName, "Branch to the next test that matches the name entered.");
		this.chkBoxBranchByName.UseVisualStyleBackColor = true;
		this.chkBoxBranchByName.CheckedChanged += new System.EventHandler(chkBoxBranchByName_CheckedChanged);
		this.labelBranchWarning.AutoSize = true;
		this.labelBranchWarning.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
		this.labelBranchWarning.ForeColor = System.Drawing.Color.Red;
		this.labelBranchWarning.Location = new System.Drawing.Point(127, 696);
		this.labelBranchWarning.Name = "labelBranchWarning";
		this.labelBranchWarning.Size = new System.Drawing.Size(359, 13);
		this.labelBranchWarning.TabIndex = 16;
		this.labelBranchWarning.Text = "Warning: No matching test names found after the current test!";
		this.labelBranchWarning.Visible = false;
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.AutoScroll = true;
		base.AutoScrollMargin = new System.Drawing.Size(0, 3);
		base.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
		this.BackColor = System.Drawing.Color.White;
		base.ClientSize = new System.Drawing.Size(494, 733);
		base.Controls.Add(this.chkBoxBranchByName);
		base.Controls.Add(this.labelBranchWarning);
		base.Controls.Add(this.txtBoxBranchTestName);
		base.Controls.Add(this.chkConditionActive);
		base.Controls.Add(this.radioNotMet);
		base.Controls.Add(this.radioMet);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.groupBox2);
		base.Controls.Add(this.groupBox1);
		base.Controls.Add(this.ultraTreeView);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.ultraComboPostCondition);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.btnOk);
		this.MaximumSize = new System.Drawing.Size(510, 772);
		this.MinimumSize = new System.Drawing.Size(489, 100);
		base.Name = "PostConditionDialog";
		base.ShowIcon = false;
		base.ShowInTaskbar = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "QSPR Test Post Condition";
		base.Load += new System.EventHandler(PostConditionDialog_Load);
		this.groupBox1.ResumeLayout(false);
		this.groupBox2.ResumeLayout(false);
		base.ResumeLayout(false);
		base.PerformLayout();
	}

	private void txtBoxBranchTestName_LostFocus(object sender, EventArgs e)
	{
		UpdateBranchWarningVisibility();
	}

	private void UpdateBranchWarningVisibility()
	{
		BaseTest baseTest = CurrentTest.Root.FindTestFromTestName(txtBoxBranchTestName.Text, isPartialName: false, CurrentTest.Index);
		if (baseTest == null)
		{
			labelBranchWarning.Visible = true;
		}
		else
		{
			labelBranchWarning.Visible = false;
		}
	}

	private void PostConditionDialog_Load(object sender, EventArgs e)
	{
		try
		{
			if (postConditionMgr == null)
			{
				postConditionMgr = new ConditionMgr();
			}
			foreach (BaseCondition condition in postConditionMgr.Conditions)
			{
				formulaBuilderUC1.AddCondition(condition);
			}
			formulaBuilderUC1.Expression = postConditionMgr.Expression;
			chkConditionActive.Checked = postConditionMgr.Enabled;
			radioNotMet.Checked = postConditionMgr.NegateResult;
			ultraComboPostCondition.SelectedValue = postConditionMgr.Action.ToString();
			RootTest root = CurrentTest.Root;
			TreeNode treeNode = ultraTreeView.Nodes.Add("", root.Properties.TestName);
			treeNode.Tag = root;
			treeNode.ForeColor = Color.Red;
			nodeCollection.Add(root.Index, treeNode);
			LoadTree(root, treeNode);
			ResetTestTree(treeNode);
			ultraTreeView.Nodes[0].Expand();
			if (postConditionMgr.Action == PostConditionActions.Branch)
			{
				if (!postConditionMgr.BranchByTestName && nodeCollection.ContainsKey(postConditionMgr.BranchTestIndex))
				{
					TreeNode treeNode2 = nodeCollection[postConditionMgr.BranchTestIndex] as TreeNode;
					ultraTreeView.SelectedNode = treeNode2;
					treeNode2.EnsureVisible();
				}
				else if (postConditionMgr.BranchByTestName)
				{
					chkBoxBranchByName.Checked = true;
					txtBoxBranchTestName.Text = postConditionMgr.BranchOnTestName;
					UpdateBranchWarningVisibility();
				}
				else
				{
					AppMessageDialog.Show(this, "Branch Post Condition test not found.", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
				}
			}
		}
		catch
		{
		}
	}

	private void LoadTree(FolderTest folder, TreeNode parentNode)
	{
		try
		{
			foreach (BaseTest test in folder.TestList)
			{
				TreeNode treeNode = parentNode.Nodes.Add("", test.Properties.TestName);
				treeNode.Tag = test;
				nodeCollection.Add(test.Index, treeNode);
				if (CurrentNodeIndex == test.Index)
				{
					treeNode.ForeColor = Color.Red;
					currentNodeFound = true;
				}
				if (!currentNodeFound)
				{
					treeNode.ForeColor = Color.Red;
				}
				if (test is FolderTest)
				{
					LoadTree((FolderTest)test, treeNode);
				}
				if (test is FolderTest)
				{
					FolderTest folderTest = test as FolderTest;
					if (folderTest.FolderProperties.Expanded)
					{
						treeNode.Expand();
					}
				}
			}
		}
		catch
		{
		}
	}

	private void ResetTestTree(TreeNode ParentNode)
	{
		if (ParentNode.Nodes.Count > 0)
		{
			BaseTest baseTest = (BaseTest)ParentNode.Tag;
			if (baseTest is RootTest)
			{
				ParentNode.ImageIndex = 0;
				ParentNode.SelectedImageIndex = 0;
			}
			else if (((FolderTest)baseTest).FolderProperties.Scenarios != null)
			{
				if (((FolderTest)baseTest).FolderProperties.Scenarios.Count > 0)
				{
					ParentNode.ImageIndex = 1;
					ParentNode.SelectedImageIndex = 2;
				}
				else
				{
					ParentNode.ImageIndex = 1;
					ParentNode.SelectedImageIndex = 2;
				}
			}
			else
			{
				ParentNode.ImageIndex = 1;
				ParentNode.SelectedImageIndex = 2;
			}
		}
		foreach (TreeNode node in ParentNode.Nodes)
		{
			if (node.Nodes.Count > 0)
			{
				BaseTest baseTest2 = (BaseTest)node.Tag;
				if (((FolderTest)baseTest2).FolderProperties.Scenarios != null)
				{
					if (((FolderTest)baseTest2).FolderProperties.Scenarios.Count > 0)
					{
						node.ImageIndex = 1;
						node.SelectedImageIndex = 2;
					}
					else
					{
						node.ImageIndex = 1;
						node.SelectedImageIndex = 2;
					}
				}
				else
				{
					node.ImageIndex = 1;
					node.SelectedImageIndex = 2;
				}
				if (baseTest2.Properties.LinkNode && baseTest2.Properties.IsLinkActive != true)
				{
					node.ForeColor = Color.Red;
				}
				ResetTestTree(node);
			}
			else
			{
				BaseTest baseTest3 = (BaseTest)node.Tag;
				if (baseTest3 is FolderTest)
				{
					node.ImageIndex = 1;
					node.SelectedImageIndex = 2;
				}
				else
				{
					node.ImageIndex = 3;
					node.SelectedImageIndex = 3;
				}
				if (baseTest3.Properties.LinkNode && baseTest3.Properties.IsLinkActive != true)
				{
					node.ForeColor = Color.Red;
				}
			}
		}
	}

	private void chkConditionActive_CheckedChanged(object sender, EventArgs e)
	{
		if (chkConditionActive.Checked)
		{
			radioMet.Enabled = true;
			radioNotMet.Enabled = true;
			label1.ForeColor = SystemColors.ControlText;
		}
		else
		{
			label1.ForeColor = SystemColors.ControlDarkDark;
		}
	}

	private void btnOk_Click(object sender, EventArgs e)
	{
		if (HasConditionChanged())
		{
			switch (AppMessageDialog.Show(conditionSelected.Name + " has changed. Do you want to save?", "QSPR - Message Center", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, DialogResult.Yes, MessageDialogType.Warning))
			{
			case DialogResult.Yes:
				conditionSelectedIsNew = true;
				if (!SaveCondition())
				{
					return;
				}
				break;
			case DialogResult.Cancel:
				return;
			}
		}
		PostConditionActions postConditionActions = PostConditionActions.None;
		postConditionActions = (PostConditionActions)Enum.Parse(postConditionActions.GetType(), ultraComboPostCondition.SelectedValue.ToString());
		if (postConditionActions == PostConditionActions.None && AppMessageDialog.Show("There is no action selected for this Post Condition.", "QSPR - Message Center", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, DialogResult.OK, MessageDialogType.Warning) == DialogResult.Cancel)
		{
			return;
		}
		if (!chkConditionActive.Checked)
		{
			switch (AppMessageDialog.Show(this, dialogTypeStr + " is not enabled. Do you want to enable it?", "QSPR - Message Center", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, DialogResult.No, MessageDialogType.Warning))
			{
			case DialogResult.Yes:
				chkConditionActive.Checked = true;
				break;
			case DialogResult.Cancel:
				return;
			}
		}
		if (postConditionActions == PostConditionActions.Branch)
		{
			if (!chkBoxBranchByName.Checked)
			{
				if (ultraTreeView.SelectedNode == null)
				{
					AppMessageDialog.Show(this, "Test should be selected from the tree view for Branch Post Condition.", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
					return;
				}
				if (ultraTreeView.SelectedNode == ultraTreeView.Nodes[0])
				{
					AppMessageDialog.Show(this, "The tree node to branch to must be after the current test for Branch Post Condition.", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
					return;
				}
			}
			else if (string.IsNullOrWhiteSpace(txtBoxBranchTestName.Text))
			{
				AppMessageDialog.Show(this, "TestName entered cannot be blank for Branch Post Condition.", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
				return;
			}
		}
		if (formulaBuilderUC1.Expression.Trim().Length == 0 && formulaBuilderUC1.NumberOfConditions == 1 && postConditionActions != 0 && chkConditionActive.Checked)
		{
			if (AppMessageDialog.Show("Default expression will be used: " + formulaBuilderUC1.GetCondition(0).Name, "QSPR - Message Center", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, DialogResult.OK, MessageDialogType.Warning) == DialogResult.Cancel)
			{
				return;
			}
			formulaBuilderUC1.Expression = formulaBuilderUC1.GetCondition(0).Name;
		}
		if ((formulaBuilderUC1.Expression.Trim().Length != 0 || postConditionActions != 0 || chkConditionActive.Checked) && !formulaBuilderUC1.CheckSyntax(showSuccessDialog: false))
		{
			return;
		}
		postConditionMgr = new ConditionMgr(formulaBuilderUC1.Expression);
		postConditionMgr.Enabled = chkConditionActive.Checked;
		for (int i = 0; i < formulaBuilderUC1.NumberOfConditions; i++)
		{
			postConditionMgr.AddCondition(formulaBuilderUC1.GetCondition(i));
		}
		postConditionMgr.NegateResult = radioNotMet.Checked;
		postConditionMgr.Action = postConditionActions;
		if (postConditionActions == PostConditionActions.Branch)
		{
			if (!chkBoxBranchByName.Checked && ultraTreeView.SelectedNode != null)
			{
				if (ultraTreeView.SelectedNode.Tag is BaseTest baseTest)
				{
					postConditionMgr.BranchTestIndex = baseTest.Index;
					postConditionMgr.BranchOnTestName = baseTest.Properties.TestName;
				}
			}
			else if (chkBoxBranchByName.Checked && !string.IsNullOrWhiteSpace(txtBoxBranchTestName.Text))
			{
				postConditionMgr.BranchOnTestName = txtBoxBranchTestName.Text;
				postConditionMgr.BranchByTestName = true;
			}
		}
		base.DialogResult = DialogResult.OK;
		CurrentTest.Root.Scheduler.GlobalStatus.RegistrationStatus.ParameterAttributeChanged = true;
		Close();
	}

	private void ultraTreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
	{
		if (!ignoreSelection && e.Node.ForeColor == Color.Red)
		{
			ignoreSelection = true;
			e.Cancel = true;
			ultraTreeView.SelectedNode = null;
			AppMessageDialog.Show(this, "Tests below currently selected test can be selected for Branching.", "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, DialogResult.OK, MessageDialogType.Information);
			ignoreSelection = false;
		}
	}

	private void ultraTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
	{
		if (e.Node != null)
		{
			if (e.Node.IsSelected && !ultraTreeView.Focused)
			{
				Font font = e.Node.NodeFont ?? ultraTreeView.Font;
				e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
				TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, SystemColors.HighlightText, TextFormatFlags.Default);
			}
			else
			{
				e.DrawDefault = true;
			}
		}
	}

	private void ultraComboPostCondition_ValueChanged(object sender, EventArgs e)
	{
		try
		{
			bool flag = false;
			if (ultraComboPostCondition.SelectedValue.ToString() == PostConditionActions.Branch.ToString())
			{
				flag = true;
			}
			ultraTreeView.Enabled = flag;
			chkBoxBranchByName.Enabled = flag;
			if (flag && chkBoxBranchByName.Checked)
			{
				txtBoxBranchTestName.Enabled = true;
			}
			else
			{
				txtBoxBranchTestName.Enabled = false;
			}
		}
		catch
		{
		}
	}

	private void conditionGrid_SelectionChange(object sender, EventArgs e)
	{
		if (ignoreSelectionChange)
		{
			return;
		}
		ignoreSelectionChange = true;
		if (HasConditionChanged())
		{
			switch (AppMessageDialog.Show(conditionSelected.Name + " has changed. Do you want to save?", "QSPR - Message Center", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, DialogResult.Yes, MessageDialogType.Warning))
			{
			case DialogResult.Yes:
				conditionSelectedIsNew = true;
				if (!SaveCondition())
				{
					formulaBuilderUC1.HighlightedCondition = conditionSelected;
					ignoreSelectionChange = false;
					return;
				}
				break;
			case DialogResult.Cancel:
				formulaBuilderUC1.HighlightedCondition = conditionSelected;
				ignoreSelectionChange = false;
				return;
			}
		}
		if (formulaBuilderUC1.SelectedCondition != null)
		{
			btnDeleteCondition.Enabled = true;
			btnSaveCondition.Enabled = true;
			createCondition.Enabled = true;
			conditionSelected = formulaBuilderUC1.SelectedCondition;
			conditionToModify = conditionSelected.Clone();
			createCondition.Condition = conditionToModify;
			conditionSelectedIsNew = false;
		}
		ignoreSelectionChange = false;
	}

	private void btnNewCondition_Click(object sender, EventArgs e)
	{
		ignoreSelectionChange = true;
		if (HasConditionChanged())
		{
			switch (AppMessageDialog.Show(conditionSelected.Name + " has changed. Do you want to save?", "QSPR - Message Center", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, DialogResult.Yes, MessageDialogType.Warning))
			{
			case DialogResult.Yes:
				conditionSelectedIsNew = true;
				if (!SaveCondition())
				{
					ignoreSelectionChange = false;
					return;
				}
				break;
			case DialogResult.Cancel:
				ignoreSelectionChange = false;
				return;
			}
		}
		TestPostCondition testPostCondition = new TestPostCondition();
		if (conditionSelected != null)
		{
			conditionSelected.Copy(testPostCondition);
		}
		else
		{
			testPostCondition.TestConditionType = ConditionType.TestResult;
			testPostCondition.Result = TestResult.Failed;
		}
		int i;
		for (i = 1; formulaBuilderUC1.GetCondition("C" + i) != null; i++)
		{
		}
		testPostCondition.Name = "C" + i;
		btnDeleteCondition.Enabled = true;
		btnSaveCondition.Enabled = true;
		createCondition.Enabled = true;
		conditionSelected = testPostCondition;
		conditionToModify = testPostCondition.Clone();
		formulaBuilderUC1.AddCondition(testPostCondition);
		formulaBuilderUC1.HighlightedCondition = testPostCondition;
		createCondition.Condition = conditionToModify;
		ignoreSelectionChange = false;
		conditionSelectedIsNew = true;
	}

	private void btnSaveCondition_Click(object sender, EventArgs e)
	{
		SaveCondition();
	}

	private void btnDeleteCondition_Click(object sender, EventArgs e)
	{
		ignoreSelectionChange = true;
		if (AppMessageDialog.Show("Are you sure you want to delete " + conditionSelected.Name + "?", "Delete Condition", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, DialogResult.Yes, MessageDialogType.Warning) == DialogResult.Yes)
		{
			formulaBuilderUC1.RemoveCondition(conditionSelected);
			formulaBuilderUC1.HighlightedCondition = null;
			conditionSelected = null;
			conditionToModify = null;
			btnDeleteCondition.Enabled = false;
			btnSaveCondition.Enabled = false;
			createCondition.Enabled = false;
		}
		ignoreSelectionChange = false;
	}

	private void btnDeleteAllConditions_Click(object sender, EventArgs e)
	{
		ignoreSelectionChange = true;
		if (AppMessageDialog.Show("Are you sure you want to delete all conditions?", "Delete Condition", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, DialogResult.Yes, MessageDialogType.Warning) == DialogResult.Yes)
		{
			formulaBuilderUC1.RemoveAllConditions();
			formulaBuilderUC1.HighlightedCondition = null;
			conditionSelected = null;
			conditionToModify = null;
			btnDeleteCondition.Enabled = false;
			btnSaveCondition.Enabled = false;
			createCondition.Enabled = false;
		}
		ignoreSelectionChange = false;
	}

	private bool HasConditionChanged()
	{
		if (conditionSelected == null)
		{
			return false;
		}
		return !conditionSelected.Equals(conditionToModify);
	}

	private bool SaveCondition()
	{
		Regex regex = new Regex("[^a-zA-Z0-9_]");
		if (!conditionSelectedIsNew && AppMessageDialog.Show("Do you want to overwrite condition " + conditionSelected.Name + "?", "QSPR - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Question, DialogResult.Yes, MessageDialogType.Warning) == DialogResult.No)
		{
			return false;
		}
		string text = "";
		if (conditionToModify.Name.Length == 0)
		{
			text = "Please enter a condition name.";
		}
		else if (regex.IsMatch(conditionToModify.Name))
		{
			text = "Condition names can only contain alphanumeric characters and underscores.\nSpaces, tabs, and special characters are not allowed.";
		}
		else
		{
			for (int i = 0; i < formulaBuilderUC1.NumberOfConditions; i++)
			{
				BaseCondition condition = formulaBuilderUC1.GetCondition(i);
				if (!condition.Equals(conditionSelected) && condition.Name.Equals(conditionToModify.Name, StringComparison.OrdinalIgnoreCase))
				{
					text = "A condition with name " + conditionToModify.Name + " already exists.";
				}
			}
		}
		if (text != "")
		{
			AppMessageDialog.Show(this, text, "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
			return false;
		}
		switch (conditionToModify.TestConditionType)
		{
		case ConditionType.TestParameters:
		{
			text = "";
			if (conditionToModify.ParameterName.Trim().Length == 0)
			{
				text += "Parameter selection is required. ";
			}
			ParameterCls selectedParameter = createCondition.GetSelectedParameter();
			if (selectedParameter != null)
			{
				if (conditionToModify.CompareToType == CompareType.Constant && !selectedParameter.IsValueValid(conditionToModify.ParameterValue))
				{
					text += "Parameter value is not valid. ";
				}
				else if (conditionToModify.CompareToType == CompareType.Constant && conditionToModify.ParameterValue.Length == 0)
				{
					DialogResult dialogResult2 = AppMessageDialog.Show(this, "Parameter Value is Empty String. Do you want to continue?", "QSPR - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, DialogResult.Yes, MessageDialogType.Warning);
					if (dialogResult2 == DialogResult.No)
					{
						return false;
					}
				}
				else if (conditionToModify.CompareToType == CompareType.GlobalVariable && conditionToModify.GVToCompareName == "")
				{
					text += "A global variable to compare to is not selected. ";
				}
			}
			if (text != "")
			{
				AppMessageDialog.Show(this, text, "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
				return false;
			}
			conditionSelected.Name = conditionToModify.Name;
			conditionSelected.TestConditionType = conditionToModify.TestConditionType;
			conditionSelected.ParameterName = conditionToModify.ParameterName;
			conditionSelected.ParameterIndex = conditionToModify.ParameterIndex;
			conditionSelected.ParameterSign = conditionToModify.ParameterSign;
			conditionSelected.ParameterValue = conditionToModify.ParameterValue;
			conditionSelected.GVToCompareName = conditionToModify.GVToCompareName;
			conditionSelected.CompareToType = conditionToModify.CompareToType;
			formulaBuilderUC1.UpdateConditionStrings();
			break;
		}
		case ConditionType.TestResult:
			conditionSelected.Name = conditionToModify.Name;
			conditionSelected.TestConditionType = conditionToModify.TestConditionType;
			conditionSelected.Result = conditionToModify.Result;
			formulaBuilderUC1.UpdateConditionStrings();
			break;
		case ConditionType.GlobalVariables:
			text = "";
			if (conditionToModify.GlobalVariableName.Trim().Length == 0)
			{
				text += "Global variable selection is required.";
			}
			if (conditionToModify.CompareToType == CompareType.Constant && conditionToModify.GlobalVariableValue.Length == 0)
			{
				DialogResult dialogResult = AppMessageDialog.Show(this, "Global variable value is Empty String. Do you want to continue?", "QSPR - Message Center", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, DialogResult.Yes, MessageDialogType.Warning);
				if (dialogResult == DialogResult.No)
				{
					return false;
				}
			}
			else if (conditionToModify.CompareToType == CompareType.GlobalVariable && conditionToModify.GVToCompareName == "")
			{
				text += "A global variable to compare to is not selected.";
			}
			else if (conditionToModify.CompareToType == CompareType.LowerLimit || conditionToModify.CompareToType == CompareType.UpperLimit)
			{
				text += "Condition type error. ";
			}
			if (text != "")
			{
				AppMessageDialog.Show(this, text, "QSPR - Message Center", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK, MessageDialogType.Error);
				return false;
			}
			conditionSelected.Name = conditionToModify.Name;
			conditionSelected.TestConditionType = conditionToModify.TestConditionType;
			conditionSelected.GlobalVariableName = conditionToModify.GlobalVariableName;
			conditionSelected.GlobalVariableSign = conditionToModify.GlobalVariableSign;
			conditionSelected.GlobalVariableValue = conditionToModify.GlobalVariableValue;
			conditionSelected.GVToCompareName = conditionToModify.GVToCompareName;
			conditionSelected.CompareToType = conditionToModify.CompareToType;
			formulaBuilderUC1.UpdateConditionStrings();
			break;
		}
		conditionSelectedIsNew = false;
		return true;
	}

	private void chkBoxBranchByName_CheckedChanged(object sender, EventArgs e)
	{
		if (chkBoxBranchByName.Checked)
		{
			ultraTreeView.SelectedNode = null;
			ultraTreeView.Enabled = false;
			txtBoxBranchTestName.Enabled = true;
		}
		else
		{
			ultraTreeView.Enabled = true;
			txtBoxBranchTestName.Enabled = false;
			txtBoxBranchTestName.Text = "";
		}
	}
}
