using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Reflection;
using System.Security.Principal;
using System.Windows.Forms;
using System.Xml.Serialization;
using QC.QsprShared.Dialogs;
using QC.QsprTestObjects.Metadata;
using QC.QsprTestObjects.TestConditions;
using QC.QTMSecurity;

namespace QC.QsprTestObjects.Tests.Base;

[XmlInclude(typeof(TestPlanProperties))]
public class RootTestProperties : FolderTestProperties
{
	public class TreeTypeChangedEventArgs : EventArgs
	{
		public TreeTypes NewTreeType;

		public TreeTypes OldTreeType;

		public bool Cancel;
	}

	public delegate void TreeTypeChangedEvent(object sender, TreeTypeChangedEventArgs e);

	public const string ROOT_PROPERTY = "RootProperty";

	private string configFileName = "";

	protected TreeTypes treeType;

	private string comment = "";

	private string createdBy = "";

	private string treeFileName = "";

	private string server = "";

	private string database = "";

	private bool enableDatabase = true;

	private long siteTreeID = -1L;

	private string treeStringID = "-1";

	private string registeredMachine = "";

	private bool forceRegistrationOnPlugInChange;

	private bool forceRegistrationOnLinkedTreeChange = true;

	private bool registeredLinkedTreeOnly = true;

	private bool autoUpdateLinkedTree;

	private string _dynamicLimitsFileExcel;

	private bool refreshConfig;

	private bool stopOnFirstTestFailure;

	private bool pauseOnTestFailure;

	private string testErrorCodeOnAbort = "";

	private bool alwaysPerformMustRunTests = true;

	[ReadOnly(true)]
	[Description("Specifies the location of the tree registration server.")]
	[Category("Registration")]
	public string Server
	{
		get
		{
			return server;
		}
		set
		{
			server = value;
		}
	}

	[Description("Specifies the database where tree is registered.")]
	[Category("Registration")]
	[ReadOnly(true)]
	public string Database
	{
		get
		{
			return database;
		}
		set
		{
			database = value;
		}
	}

	[Category("Registration")]
	[Description("This warns the user for non registered tree.")]
	public bool EnableDatabase
	{
		get
		{
			return enableDatabase;
		}
		set
		{
			if (!value)
			{
				ForceRegistrationOnLinkedTreeChange = false;
				RegisteredLinkedTreeOnly = false;
			}
			enableDatabase = value;
		}
	}

	[Category("Registration")]
	[DefaultValue(false)]
	[Description("When set to true, In GoNoGo tree type, registration will be required when there is change in plugins configurations.")]
	public bool ForceRegistrationOnPlugInChange
	{
		get
		{
			return forceRegistrationOnPlugInChange;
		}
		set
		{
			forceRegistrationOnPlugInChange = value;
		}
	}

	[DefaultValue(true)]
	[Category("Linked Tree")]
	[Description("When set to true, registration will be required when any link is changed.")]
	public bool ForceRegistrationOnLinkedTreeChange
	{
		get
		{
			return forceRegistrationOnLinkedTreeChange;
		}
		set
		{
			forceRegistrationOnLinkedTreeChange = value;
		}
	}

	[Description("When set to true, link trees must be registered trees.")]
	[Category("Linked Tree")]
	[DefaultValue(true)]
	public bool RegisteredLinkedTreeOnly
	{
		get
		{
			return registeredLinkedTreeOnly;
		}
		set
		{
			registeredLinkedTreeOnly = value;
		}
	}

	[Category("Linked Tree")]
	[DefaultValue(false)]
	[Description("When set to true, if a link has changed, linked tree will be updated automatically.")]
	public bool AutoUpdateLinkedTree
	{
		get
		{
			return autoUpdateLinkedTree;
		}
		set
		{
			autoUpdateLinkedTree = value;
		}
	}

	[Editor(typeof(OpenFileUITypeEditor), typeof(UITypeEditor))]
	[DefaultValue("")]
	[Description("Specifies the location of the Tester Configuration file.")]
	[Category("Tester")]
	public string ConfigFileName
	{
		get
		{
			return configFileName;
		}
		set
		{
			configFileName = value;
			if (base.Owner != null)
			{
				(base.Owner as RootTest).Scheduler.Env.GlobalVariable.SetGlobalVariable("TesterConfigFile", value);
			}
		}
	}

	[Category("Tester")]
	[DefaultValue("")]
	[Description("Specifies the location of the Dynamic Limits Excel file.")]
	[Editor(typeof(OpenFileUITypeEditor), typeof(UITypeEditor))]
	public string DynamicLimitsFileExcel
	{
		get
		{
			return _dynamicLimitsFileExcel;
		}
		set
		{
			_dynamicLimitsFileExcel = value;
			DynamicLimitsFileXML = string.Empty;
		}
	}

	[Category("Tester")]
	[Editor(typeof(OpenFileUITypeEditor), typeof(UITypeEditor))]
	[Browsable(false)]
	[DefaultValue("")]
	[Description("Specifies the location of the Dynamic Limits XML file.")]
	public string DynamicLimitsFileXML { get; set; }

	[Description("Specifies whether to reload the Tester Configuration file each time the GoNoGo runs.")]
	[Category("Tester")]
	[DefaultValue(false)]
	public bool RefreshConfig
	{
		get
		{
			return refreshConfig;
		}
		set
		{
			refreshConfig = value;
		}
	}

	[Description("Only members of the selected QGroups can edit the tree.")]
	[DefaultValue("")]
	[Category("Security")]
	public QGroupsCollection EditQGroups { get; set; }

	[ReadOnly(true)]
	[Category("Registration")]
	[Description("Machine name where the test tree got registered.")]
	public string RegisteredMachine
	{
		get
		{
			return registeredMachine;
		}
		set
		{
			registeredMachine = value;
		}
	}

	[Browsable(false)]
	[DefaultValue(-1)]
	public long SiteTreeID
	{
		get
		{
			return siteTreeID;
		}
		set
		{
			siteTreeID = value;
		}
	}

	[Browsable(false)]
	public string SiteKey
	{
		get
		{
			string result = "domain\\QTM";
			try
			{
				string name = WindowsIdentity.GetCurrent().Name;
				string text = name.Split('\\')[0];
				result = text + "\\QTM";
			}
			catch
			{
			}
			return result;
		}
		set
		{
		}
	}

	[Browsable(false)]
	public override bool ToolTest => false;

	[Browsable(false)]
	public override bool SkipWhenLinked => false;

	[Browsable(false)]
	public override string LoopSetup => "False";

	[Browsable(false)]
	public override StopOnErrorTypes StopOnError => StopOnErrorTypes.None;

	[Browsable(false)]
	public override bool TreeMustRun => false;

	[Browsable(false)]
	public override bool FolderMustRun => false;

	[Browsable(false)]
	public override bool DisableLooping => false;

	[Category("Settings")]
	[Description("Type of testing")]
	public virtual TreeTypes TreeType
	{
		get
		{
			return treeType;
		}
		set
		{
			if (treeType != value)
			{
				TreeTypeChangedEventArgs treeTypeChangedEventArgs = new TreeTypeChangedEventArgs();
				treeTypeChangedEventArgs.NewTreeType = value;
				treeTypeChangedEventArgs.OldTreeType = treeType;
				TreeTypeChangedEventArgs treeTypeChangedEventArgs2 = treeTypeChangedEventArgs;
				treeType = value;
				if (this.OnTreeTypeChanged != null)
				{
					this.OnTreeTypeChanged(this, treeTypeChangedEventArgs2);
				}
				if (treeTypeChangedEventArgs2.Cancel)
				{
					treeType = treeTypeChangedEventArgs2.OldTreeType;
				}
			}
		}
	}

	[DefaultValue(false)]
	[Description("In GoNoGo mode, if any test fails, the testing is stopped.")]
	[Category("Settings")]
	public bool StopOnFirstTestFailure
	{
		get
		{
			return stopOnFirstTestFailure;
		}
		set
		{
			if (value && pauseOnTestFailure)
			{
				DialogResult dialogResult = AppMessageDialog.Show("The root folder cannot have StopOnFirstTestFailure and PauseOnTestFailure set at the same time.\nPauseOnTestFailure will be set to false.", "QSPR - Message Center", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, DialogResult.OK, MessageDialogType.UIWarning);
				if (dialogResult != DialogResult.OK)
				{
					return;
				}
				pauseOnTestFailure = false;
			}
			stopOnFirstTestFailure = value;
			if (base.Owner != null)
			{
				(base.Owner as RootTest).Scheduler.GlobalStatus.StopOnFirstTestFailure = stopOnFirstTestFailure;
			}
		}
	}

	[DefaultValue(false)]
	[Description("In GoNoGo mode, if any test fails, the tree is paused.")]
	[Category("Settings")]
	public bool PauseOnTestFailure
	{
		get
		{
			return pauseOnTestFailure;
		}
		set
		{
			if (value && stopOnFirstTestFailure)
			{
				DialogResult dialogResult = AppMessageDialog.Show("The root folder cannot have StopOnFirstTestFailure and PauseOnTestFailure set at the same time.\nStopOnFirstTestFailure will be set to false.", "QSPR - Message Center", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, DialogResult.OK, MessageDialogType.UIWarning);
				if (dialogResult != DialogResult.OK)
				{
					return;
				}
				stopOnFirstTestFailure = false;
			}
			pauseOnTestFailure = value;
			if (base.Owner != null)
			{
				(base.Owner as RootTest).Scheduler.GlobalStatus.PauseOnTestFailure = pauseOnTestFailure;
			}
		}
	}

	[DefaultValue("")]
	[Description("The test error code on abort string will be set when the test tree is aborted and no error code has already been set.")]
	[Category("Settings")]
	public string TestErrorCodeOnAbort
	{
		get
		{
			return testErrorCodeOnAbort;
		}
		set
		{
			testErrorCodeOnAbort = value;
		}
	}

	[Description("Always execute tests that are marked as TreeMustRun even if a tree is aborted or stopped")]
	[DefaultValue(true)]
	[Category("Settings")]
	public bool AlwaysPerformMustRunTests
	{
		get
		{
			return alwaysPerformMustRunTests;
		}
		set
		{
			alwaysPerformMustRunTests = value;
		}
	}

	[DefaultValue(0)]
	[Category("Settings")]
	[Description("Specifies the length of time in seconds before the tree times out and is stopped.\nA zero value indicates that tree will not time out.")]
	public int TreeTimeOut
	{
		get
		{
			return base.TestTimeOut;
		}
		set
		{
			base.TestTimeOut = value;
		}
	}

	[Browsable(false)]
	public override bool IsSetupFolder
	{
		get
		{
			return false;
		}
		set
		{
		}
	}

	[Browsable(false)]
	public override bool IsCleanupFolder
	{
		get
		{
			return false;
		}
		set
		{
		}
	}

	[Browsable(false)]
	public override float WaitForLogTimeout
	{
		get
		{
			return 60f;
		}
		set
		{
		}
	}

	[Browsable(false)]
	[DefaultValue("")]
	public string CreatedBy
	{
		get
		{
			return createdBy;
		}
		set
		{
			createdBy = value;
		}
	}

	[DefaultValue("")]
	[Browsable(false)]
	[XmlIgnore]
	public string TreeFileName
	{
		get
		{
			return treeFileName;
		}
		set
		{
			treeFileName = value;
			if (base.Owner != null)
			{
				(base.Owner as RootTest).Scheduler.Env.GlobalVariable.SetGlobalVariable("TreeFileName", value);
			}
		}
	}

	[ReadOnly(true)]
	[Description("Specifies the TreeID for the registered tree.")]
	[Category("Registration")]
	public long TreeID => nodeRID;

	public string TreeStringID
	{
		get
		{
			if (nodeRID != -1)
			{
				return treeStringID.ToString();
			}
			return "-1";
		}
		set
		{
			treeStringID = value;
			if (base.Owner != null)
			{
				(base.Owner as RootTest).Scheduler.Env.GlobalVariable.SetGlobalVariable("TreeStringID", treeStringID);
			}
			if (!value.Equals("-1"))
			{
				enableDatabase = true;
			}
		}
	}

	[XmlAttribute("NodeType")]
	[Browsable(false)]
	public override string NodeType
	{
		get
		{
			return "RootProperty";
		}
		set
		{
			base.NodeType = value;
		}
	}

	[Browsable(false)]
	[XmlIgnore]
	public override string PreConditionEnabled
	{
		get
		{
			return base.PreConditionEnabled;
		}
		set
		{
			base.PreConditionEnabled = value;
		}
	}

	[Browsable(false)]
	[XmlIgnore]
	public override ConditionMgr PreCondition
	{
		get
		{
			return null;
		}
		set
		{
		}
	}

	[Browsable(false)]
	[XmlIgnore]
	public override string MetadataEnabled
	{
		get
		{
			return base.MetadataEnabled;
		}
		set
		{
			base.MetadataEnabled = value;
		}
	}

	[XmlIgnore]
	[Browsable(false)]
	public override MetadataItems Metadata
	{
		get
		{
			return null;
		}
		set
		{
		}
	}

	[XmlIgnore]
	[Browsable(false)]
	public override int TestTimeOut
	{
		get
		{
			return TreeTimeOut;
		}
		set
		{
			TreeTimeOut = value;
		}
	}

	[Browsable(false)]
	public bool LogSkippedResults => TreeType == TreeTypes.TestPlan;

	[Category("Settings")]
	[Description("The DLL version loaded must be equal or greater than the DLL version the test was created with.")]
	public virtual bool EnforceTestDllVersions { get; set; }

	public event TreeTypeChangedEvent OnTreeTypeChanged;

	public RootTestProperties()
	{
		if (EditQGroups == null)
		{
			EditQGroups = new QGroupsCollection();
		}
	}

	public RootTestProperties(RootTestProperties original)
	{
		PropertyInfo[] properties = GetType().GetProperties();
		foreach (PropertyInfo propertyInfo in properties)
		{
			PropertyInfo property = original.GetType().GetProperty(propertyInfo.Name);
			if (property != null && property.CanWrite)
			{
				object value = propertyInfo.GetValue(original, null);
				propertyInfo.SetValue(this, value, null);
			}
		}
	}
}
