using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Windows.Forms;
using System.Xml.Serialization;
using QC.CTDS.DM.ApplicationsManager;
using QC.QsprShared.Trace;

namespace QC.QsprTestObjects.Tests.Base;

[Serializable]
public class RootTest : FolderTest, ISerializable
{
	public const string ROOT = "Root";

	private RootTestProperties rootProperties = new RootTestProperties();

	[NonSerialized]
	[XmlIgnore]
	public ReportGenerator ReportGenerator;

	[NonSerialized]
	[XmlIgnore]
	public Scheduler Scheduler;

	[XmlIgnore]
	public override RootTest Root
	{
		get
		{
			return this;
		}
		set
		{
		}
	}

	public RootTestProperties RootProperties
	{
		get
		{
			return rootProperties;
		}
		set
		{
			rootProperties = value;
		}
	}

	[XmlIgnore]
	public override TestProperties Properties
	{
		get
		{
			rootProperties.Owner = this;
			return rootProperties;
		}
		set
		{
			rootProperties = (RootTestProperties)value;
			rootProperties.Owner = this;
		}
	}

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

	public RootTest()
	{
	}

	public RootTest(string name)
	{
		rootProperties = new RootTestProperties();
		rootProperties.TestName = name;
		rootProperties.StopOnError = StopOnErrorTypes.None;
		Properties = rootProperties;
		base.name = name;
		Properties.RealName = "ROOT";
		index = Guid.NewGuid().ToString();
		Properties.TestIndexCopy = index;
	}

	protected RootTest(SerializationInfo info, StreamingContext context)
	{
		name = info.GetString("Name");
	}

	public override void GetObjectData(SerializationInfo info, StreamingContext context)
	{
		info.AddValue("Name", name);
	}

	public override void OnPropChanged(PropertyValueChangedEventArgs e)
	{
		rootProperties.OnPropChanged(e);
		if (rootProperties.TestName != name)
		{
			name = rootProperties.TestName;
		}
	}

	public override BaseTest Clone()
	{
		RootTest rootTest = new RootTest();
		rootTest.Scheduler = Scheduler;
		try
		{
			rootTest.Properties = Properties.Clone();
			Copy(rootTest);
		}
		catch (Exception ex)
		{
			Scheduler.TestMsgBroadCaster.SetDebugMessage("Clone Test Root: " + ex.Message);
		}
		return rootTest;
	}

	public override void RemoveRegistrationInformation()
	{
		foreach (BaseTest test in base.TestList)
		{
			test.RemoveRegistrationInformation();
		}
		rootProperties.NodeRID = -1L;
		rootProperties.Server = "";
		rootProperties.Database = "";
		rootProperties.SiteTreeID = -1L;
		rootProperties.RegisteredMachine = "";
		rootProperties.TreeStringID = "-1";
	}

	public override void CreateParent(FolderTest pTest)
	{
		try
		{
			foreach (BaseTest test in base.TestList)
			{
				test.CreateParent(this);
			}
			base.CreateParent(pTest);
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "CreateParent " + Properties.TestName + " " + ex.Message, TraceMessageType.Exception);
		}
	}

	public override bool CanTestRun(TestRuntimeAttributes fAttribute)
	{
		return true;
	}
}
