using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMApplication.Utilities;
using QC.QTMInterface;
using QC.QTMSecurity;

namespace QC.QTMApplication;

public class TTDPropView : UserControl
{
	private PropertyGrid gridTTDProp;

	private Container components;

	protected OnPropChangedDelegate propChangedDel;

	public OnPropChangedDelegate PropertyChangedDel
	{
		set
		{
			propChangedDel = value;
		}
	}

	public BaseTest CurrentNode
	{
		set
		{
			gridTTDProp.Enabled = true;
			if (QSecurity.LockedQGroup)
			{
				TypeDescriptor.AddAttributes(value.Properties, new ReadOnlyAttribute(isReadOnly: true));
			}
			gridTTDProp.SelectedObject = value.Properties;
			if (!QSecurity.IsEngineer || !QSecurity.IsTechnician)
			{
				gridTTDProp.Enabled = false;
			}
			if (!(value is FolderTest))
			{
				return;
			}
			FolderTest folderTest = (FolderTest)value;
			if (folderTest.FolderProperties.LinkRoot)
			{
				if (!folderTest.Properties.LinkNode)
				{
					LinkedFolderPropView selectedObject = new LinkedFolderPropView(folderTest);
					gridTTDProp.SelectedObject = selectedObject;
				}
				else
				{
					gridTTDProp.Enabled = false;
				}
			}
			ExpandItem("Loop Details", "CurrentLoopCondition");
		}
	}

	public TTDPropView()
	{
		InitializeComponent();
		base.GotFocus += TTDPropView_GotFocus;
		base.LostFocus += TTDPropView_LostFocus;
	}

	private void TTDPropView_LostFocus(object sender, EventArgs e)
	{
		HelpUtility.CurrentFocusedControl = HelpUtility.FocusedControl.None;
	}

	private void TTDPropView_GotFocus(object sender, EventArgs e)
	{
		HelpUtility.CurrentFocusedControl = HelpUtility.FocusedControl.PropertyView;
	}

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

	private void InitializeComponent()
	{
		this.gridTTDProp = new System.Windows.Forms.PropertyGrid();
		base.SuspendLayout();
		this.gridTTDProp.BackColor = System.Drawing.SystemColors.ControlLight;
		this.gridTTDProp.CommandsBackColor = System.Drawing.SystemColors.ControlLight;
		this.gridTTDProp.CommandsVisibleIfAvailable = true;
		this.gridTTDProp.Dock = System.Windows.Forms.DockStyle.Fill;
		this.gridTTDProp.HelpBackColor = System.Drawing.SystemColors.ControlLight;
		this.gridTTDProp.LargeButtons = false;
		this.gridTTDProp.LineColor = System.Drawing.SystemColors.ScrollBar;
		this.gridTTDProp.Location = new System.Drawing.Point(0, 0);
		this.gridTTDProp.Name = "gridTTDProp";
		this.gridTTDProp.Size = new System.Drawing.Size(352, 504);
		this.gridTTDProp.TabIndex = 0;
		this.gridTTDProp.Text = "propertyGrid1";
		this.gridTTDProp.ViewBackColor = System.Drawing.SystemColors.Window;
		this.gridTTDProp.ViewForeColor = System.Drawing.SystemColors.WindowText;
		this.gridTTDProp.PropertyValueChanged += new System.Windows.Forms.PropertyValueChangedEventHandler(gridTTDPropChanged);
		base.Controls.Add(this.gridTTDProp);
		base.Name = "TTDPropView";
		base.Size = new System.Drawing.Size(352, 504);
		base.Load += new System.EventHandler(TTDPropView_Load);
		base.ResumeLayout(false);
	}

	private void TTDPropView_Load(object sender, EventArgs e)
	{
	}

	private void gridTTDPropChanged(object s, PropertyValueChangedEventArgs e)
	{
		_ = e.ChangedItem.Label;
		if (propChangedDel != null)
		{
			propChangedDel(e);
		}
	}

	public void OnMsgFromTest(TestMsgType msgType, string strTestFullPath, TestResult Result)
	{
		switch (msgType)
		{
		case TestMsgType.OnTestResult:
			gridTTDProp.Refresh();
			break;
		case TestMsgType.OnTestStart:
			break;
		}
	}

	private void ExpandItem(string CategoryLabel, string childLabel)
	{
		GridItem selectedGridItem = gridTTDProp.SelectedGridItem;
		while (selectedGridItem.GridItemType != GridItemType.Root)
		{
			selectedGridItem = selectedGridItem.Parent;
		}
		foreach (GridItem gridItem3 in selectedGridItem.GridItems)
		{
			if (!(gridItem3.Label == CategoryLabel))
			{
				continue;
			}
			foreach (GridItem gridItem4 in gridItem3.GridItems)
			{
				if (gridItem4.Label == childLabel)
				{
					gridItem4.Expanded = true;
					break;
				}
			}
		}
	}

	public void EnableControls(bool enable)
	{
		gridTTDProp.Enabled = enable;
	}
}
