using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using QC.QsprShared.Trace;

namespace QC.QsprTestObjects.Tests.Base;

public class BranchTreeDialog : Form
{
	private const int FOLDER_IMAGE = 0;

	private const int LOOP_IMAGE = 0;

	private const int ROOT_IMAGE = 1;

	private const int TEST_IMAGE = 2;

	private ImageList imageListOffice2003Small;

	private IContainer components;

	private RootTest root = new RootTest("Root");

	private string currentNodeIndex = "";

	private string selectedNodeName = "";

	private string selectedNodeIndex = "";

	private bool branchByTestName;

	private bool currentNodeFound;

	private BaseTest currentTest;

	private Panel panelBottom;

	private TreeView ultraTreeTTDView;

	private Button btnOk;

	private Panel panelTop;

	private TextBox txtBoxBranchTestName;

	private CheckBox chkBoxBranchOnTestName;

	private Label labelBranchWarning;

	private Button btnCancel;

	public RootTest Root
	{
		set
		{
			root = value;
		}
	}

	public string SelectedNodeName
	{
		get
		{
			return selectedNodeName;
		}
		set
		{
			selectedNodeName = value;
		}
	}

	public string SelectedNodeIndex
	{
		get
		{
			return selectedNodeIndex;
		}
		set
		{
			selectedNodeIndex = value;
		}
	}

	public bool BranchByTestName
	{
		get
		{
			return branchByTestName;
		}
		set
		{
			branchByTestName = value;
		}
	}

	public BranchTreeDialog(BaseTest currentTest)
	{
		InitializeComponent();
		this.currentTest = currentTest;
		currentNodeIndex = currentTest.Index;
		root = this.currentTest.Root;
	}

	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.Tests.Base.BranchTreeDialog));
		this.imageListOffice2003Small = new System.Windows.Forms.ImageList(this.components);
		this.panelBottom = new System.Windows.Forms.Panel();
		this.btnCancel = new System.Windows.Forms.Button();
		this.btnOk = new System.Windows.Forms.Button();
		this.ultraTreeTTDView = new System.Windows.Forms.TreeView();
		this.panelTop = new System.Windows.Forms.Panel();
		this.chkBoxBranchOnTestName = new System.Windows.Forms.CheckBox();
		this.txtBoxBranchTestName = new System.Windows.Forms.TextBox();
		this.labelBranchWarning = new System.Windows.Forms.Label();
		this.panelBottom.SuspendLayout();
		this.panelTop.SuspendLayout();
		base.SuspendLayout();
		this.imageListOffice2003Small.ImageStream = (System.Windows.Forms.ImageListStreamer)resources.GetObject("imageListOffice2003Small.ImageStream");
		this.imageListOffice2003Small.TransparentColor = System.Drawing.Color.Transparent;
		this.imageListOffice2003Small.Images.SetKeyName(0, "folder-closed.png");
		this.imageListOffice2003Small.Images.SetKeyName(1, "Root.png");
		this.imageListOffice2003Small.Images.SetKeyName(2, "TestTreeTest.png");
		this.panelBottom.Controls.Add(this.btnCancel);
		this.panelBottom.Controls.Add(this.btnOk);
		this.panelBottom.Dock = System.Windows.Forms.DockStyle.Bottom;
		this.panelBottom.Location = new System.Drawing.Point(0, 382);
		this.panelBottom.Name = "panelBottom";
		this.panelBottom.Size = new System.Drawing.Size(600, 32);
		this.panelBottom.TabIndex = 3;
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.btnCancel.Location = new System.Drawing.Point(424, 4);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 0;
		this.btnCancel.Text = "Cancel";
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.btnOk.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right;
		this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.btnOk.Location = new System.Drawing.Point(512, 4);
		this.btnOk.Name = "btnOk";
		this.btnOk.Size = new System.Drawing.Size(75, 23);
		this.btnOk.TabIndex = 1;
		this.btnOk.Text = "Ok";
		this.btnOk.Click += new System.EventHandler(btnOk_Click);
		this.ultraTreeTTDView.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.ultraTreeTTDView.BackColor = System.Drawing.Color.White;
		this.ultraTreeTTDView.Enabled = false;
		this.ultraTreeTTDView.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
		this.ultraTreeTTDView.ForeColor = System.Drawing.Color.Black;
		this.ultraTreeTTDView.HideSelection = false;
		this.ultraTreeTTDView.ImageIndex = 0;
		this.ultraTreeTTDView.ImageList = this.imageListOffice2003Small;
		this.ultraTreeTTDView.Indent = 22;
		this.ultraTreeTTDView.Location = new System.Drawing.Point(0, 56);
		this.ultraTreeTTDView.Margin = new System.Windows.Forms.Padding(0);
		this.ultraTreeTTDView.Name = "ultraTreeTTDView";
		this.ultraTreeTTDView.PathSeparator = ".";
		this.ultraTreeTTDView.SelectedImageIndex = 0;
		this.ultraTreeTTDView.Size = new System.Drawing.Size(600, 326);
		this.ultraTreeTTDView.TabIndex = 0;
		this.ultraTreeTTDView.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(ultraTreeTTDView_BeforeSelect);
		this.panelTop.Controls.Add(this.labelBranchWarning);
		this.panelTop.Controls.Add(this.txtBoxBranchTestName);
		this.panelTop.Controls.Add(this.chkBoxBranchOnTestName);
		this.panelTop.Dock = System.Windows.Forms.DockStyle.Top;
		this.panelTop.Location = new System.Drawing.Point(0, 0);
		this.panelTop.Margin = new System.Windows.Forms.Padding(0);
		this.panelTop.Name = "panelTop";
		this.panelTop.Size = new System.Drawing.Size(600, 56);
		this.panelTop.TabIndex = 4;
		this.chkBoxBranchOnTestName.AutoSize = true;
		this.chkBoxBranchOnTestName.Location = new System.Drawing.Point(12, 10);
		this.chkBoxBranchOnTestName.Name = "chkBoxBranchOnTestName";
		this.chkBoxBranchOnTestName.Size = new System.Drawing.Size(133, 17);
		this.chkBoxBranchOnTestName.TabIndex = 0;
		this.chkBoxBranchOnTestName.Text = "Branch on TestName: ";
		this.chkBoxBranchOnTestName.UseVisualStyleBackColor = true;
		this.chkBoxBranchOnTestName.CheckedChanged += new System.EventHandler(chkBoxBranchOnTestName_CheckedChanged);
		this.txtBoxBranchTestName.Enabled = false;
		this.txtBoxBranchTestName.Location = new System.Drawing.Point(151, 7);
		this.txtBoxBranchTestName.Name = "txtBoxBranchTestName";
		this.txtBoxBranchTestName.Size = new System.Drawing.Size(157, 20);
		this.txtBoxBranchTestName.TabIndex = 1;
		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(148, 30);
		this.labelBranchWarning.Name = "labelBranchWarning";
		this.labelBranchWarning.Size = new System.Drawing.Size(359, 13);
		this.labelBranchWarning.TabIndex = 17;
		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);
		base.ClientSize = new System.Drawing.Size(600, 414);
		base.Controls.Add(this.panelTop);
		base.Controls.Add(this.ultraTreeTTDView);
		base.Controls.Add(this.panelBottom);
		base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
		base.Name = "BranchTreeDialog";
		base.ShowIcon = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
		this.Text = "Branch Test Dialog";
		base.Load += new System.EventHandler(BranchTreeDialog_Load);
		this.panelBottom.ResumeLayout(false);
		this.panelTop.ResumeLayout(false);
		this.panelTop.PerformLayout();
		base.ResumeLayout(false);
	}

	private void BranchTreeDialog_Load(object sender, EventArgs e)
	{
		LoadTrees();
		if (currentTest.Properties.BranchOnErrorByName)
		{
			txtBoxBranchTestName.Text = currentTest.Properties.BranchOnErrorName;
			chkBoxBranchOnTestName.Checked = true;
			txtBoxBranchTestName.Enabled = true;
			ultraTreeTTDView.Enabled = false;
		}
		else
		{
			txtBoxBranchTestName.Enabled = false;
			ultraTreeTTDView.Enabled = true;
		}
	}

	public void LoadTrees()
	{
		TraceMessage.Write(ToString(), "LoadTrees", TraceMessageType.Trace);
		try
		{
			TreeNode treeNode = new TreeNode("Root");
			ultraTreeTTDView.BeginUpdate();
			treeNode.Text = root.Properties.TestName;
			treeNode.Tag = root;
			treeNode.ImageIndex = 1;
			ultraTreeTTDView.Nodes.Add(treeNode);
			ConstructTTDTree(treeNode, root);
			ultraTreeTTDView.EndUpdate();
			ultraTreeTTDView.ExpandAll();
			treeNode.ForeColor = Color.Crimson;
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "LoadTrees " + ex.Message, TraceMessageType.Exception);
		}
	}

	private TreeNode GetSelectedNode(TreeNode pNode, string nodeIndex)
	{
		foreach (TreeNode node in pNode.Nodes)
		{
			if (node.Tag is BaseTest && ((BaseTest)node.Tag).Index == nodeIndex.Trim())
			{
				return node;
			}
			if (node.Nodes.Count > 0)
			{
				TreeNode selectedNode = GetSelectedNode(node, nodeIndex.Trim());
				if (selectedNode != null)
				{
					return selectedNode;
				}
			}
		}
		return null;
	}

	private void ConstructTTDTree(TreeNode Parent, FolderTest folderTest)
	{
		try
		{
			foreach (BaseTest test in folderTest.TestList)
			{
				TreeNode treeNode = Parent.Nodes.Add(test.Properties.TestName);
				treeNode.Tag = test;
				if (!currentNodeFound || (test.Properties.LinkNode && test.Properties.IsLinkActive == true))
				{
					treeNode.ForeColor = Color.Crimson;
				}
				if (test.Index == currentNodeIndex)
				{
					currentNodeFound = true;
				}
				if (test is FolderTest)
				{
					if (((FolderTest)test).FolderProperties.Scenarios != null)
					{
						if (((FolderTest)test).FolderProperties.Scenarios.Count > 0)
						{
							treeNode.ImageIndex = 0;
							treeNode.SelectedImageIndex = 0;
						}
					}
					else
					{
						treeNode.ImageIndex = 0;
						treeNode.SelectedImageIndex = 0;
					}
					ConstructTTDTree(treeNode, (FolderTest)test);
				}
				else
				{
					treeNode.ImageIndex = 2;
					treeNode.SelectedImageIndex = 2;
				}
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "ConstructTTDTree " + ex.Message, TraceMessageType.Exception);
		}
	}

	private void btnOk_Click(object sender, EventArgs e)
	{
		if (chkBoxBranchOnTestName.Checked && !string.IsNullOrWhiteSpace(txtBoxBranchTestName.Text))
		{
			branchByTestName = true;
			selectedNodeName = txtBoxBranchTestName.Text;
		}
		else if (ultraTreeTTDView.SelectedNode != null)
		{
			selectedNodeIndex = ((BaseTest)ultraTreeTTDView.SelectedNode.Tag).Index;
			selectedNodeName = ((BaseTest)ultraTreeTTDView.SelectedNode.Tag).Properties.TestName;
		}
		else
		{
			base.DialogResult = DialogResult.Cancel;
		}
		Close();
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	private void ultraTreeTTDView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
	{
		if (e.Node.ForeColor == Color.Crimson)
		{
			e.Cancel = true;
		}
	}

	private void chkBoxBranchOnTestName_CheckedChanged(object sender, EventArgs e)
	{
		if (chkBoxBranchOnTestName.Checked)
		{
			txtBoxBranchTestName.Enabled = true;
			ultraTreeTTDView.SelectedNode = null;
			ultraTreeTTDView.Enabled = false;
			UpdateBranchWarningVisibility();
		}
		else
		{
			txtBoxBranchTestName.Text = "";
			txtBoxBranchTestName.Enabled = false;
			ultraTreeTTDView.Enabled = true;
			labelBranchWarning.Visible = false;
		}
	}

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