using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using QC.QsprTestObjects.Parameters;
using QC.QsprTestObjects.Tests.Base;
using QC.QsprTestObjects.Tests.Hive;
using QC.QsprTestObjects.Tests.LabVIEW;
using QC.QsprTestObjects.Tests.Script;

namespace QC.QsprTestObjects.Registry;

[Serializable]
public class QTMTestContainer : INotifyPropertyChanged
{
	private string name = "";

	private Hashtable hashTests = new Hashtable();

	private ArrayList arrayTests = new ArrayList();

	private bool loaded;

	private QTMTargetType targetType;

	private string targetName = "This PC";

	private bool expanded;

	private bool hideTests;

	private QTMTestTypes containerType;

	private bool isDllPathUpdated;

	private string updatedName = "";

	private string displayedPathName = "";

	private string key;

	[XmlIgnore]
	public QTMRegistry theRegistry;

	private bool isSelected;

	private QTMTestContainerUserInterfaceProperties userInterfaceProperties;

	[XmlIgnore]
	public bool IsSelected
	{
		get
		{
			return isSelected;
		}
		set
		{
			isSelected = value;
			RaiseOnPropertyChanged("IsSelected");
		}
	}

	public string Key
	{
		get
		{
			return key;
		}
		set
		{
			key = value;
		}
	}

	public string Name
	{
		get
		{
			if (theRegistry.scheduler.GlobalStatus.SavingTree)
			{
				return name;
			}
			if (!IsDllPathUpdated)
			{
				return name;
			}
			return UpdatedDllPathName;
		}
		set
		{
			name = value;
			if (!IsDllPathUpdated)
			{
				DisplayedPathName = value;
			}
			RaiseOnPropertyChanged("Name");
		}
	}

	[XmlIgnore]
	public string OriginalName => name;

	[XmlElement(Type = typeof(QTMTest))]
	public ArrayList Tests
	{
		get
		{
			return arrayTests;
		}
		set
		{
			arrayTests = value;
		}
	}

	[XmlIgnore]
	public Hashtable HashTests
	{
		get
		{
			return hashTests;
		}
		set
		{
			hashTests = value;
		}
	}

	[XmlIgnore]
	public bool Loaded
	{
		get
		{
			return loaded;
		}
		set
		{
			loaded = value;
		}
	}

	[XmlIgnore]
	public bool IsDllPathUpdated
	{
		get
		{
			return isDllPathUpdated;
		}
		set
		{
			isDllPathUpdated = value;
		}
	}

	[XmlIgnore]
	public string UpdatedDllPathName
	{
		get
		{
			return updatedName;
		}
		set
		{
			updatedName = value;
			if (IsDllPathUpdated)
			{
				DisplayedPathName = value;
			}
		}
	}

	[XmlIgnore]
	public string DisplayedPathName
	{
		get
		{
			if (string.IsNullOrWhiteSpace(displayedPathName))
			{
				displayedPathName = name;
			}
			return displayedPathName;
		}
		set
		{
			displayedPathName = value;
			RaiseOnPropertyChanged("DisplayedPathName");
		}
	}

	public QTMTargetType TargetType
	{
		get
		{
			return targetType;
		}
		set
		{
			targetType = value;
		}
	}

	public string TargetName
	{
		get
		{
			return targetName;
		}
		set
		{
			targetName = value;
		}
	}

	public bool Expanded
	{
		get
		{
			return expanded;
		}
		set
		{
			expanded = value;
			RaiseOnPropertyChanged("Expanded");
		}
	}

	public bool HideTests
	{
		get
		{
			return hideTests;
		}
		set
		{
			hideTests = value;
		}
	}

	public QTMTestTypes ContainerType
	{
		get
		{
			return containerType;
		}
		set
		{
			containerType = value;
		}
	}

	[XmlIgnore]
	public string TestListAsString
	{
		get
		{
			string text = "";
			foreach (QTMTest value in hashTests.Values)
			{
				text = text + value.Name + ",";
			}
			return text;
		}
	}

	[XmlIgnore]
	public Hashtable ListOfTestParameter
	{
		get
		{
			Hashtable hashtable = new Hashtable();
			foreach (QTMTest value2 in hashTests.Values)
			{
				object value = null;
				if (value2.Parameters != null)
				{
					value = value2.Parameters.GetParameterObject(theRegistry.scheduler);
				}
				hashtable.Add(value2.Name, value);
			}
			return hashtable;
		}
	}

	public string Version { get; set; }

	[XmlIgnore]
	public QTMTestContainerUserInterfaceProperties UserInterfaceProperties
	{
		get
		{
			return userInterfaceProperties;
		}
		set
		{
			userInterfaceProperties = value;
		}
	}

	[field: XmlIgnore]
	public event PropertyChangedEventHandler PropertyChanged;

	public QTMTestContainer()
	{
		UserInterfaceProperties = new QTMTestContainerUserInterfaceProperties(this);
	}

	public QTMTestContainer(string name, QTMTestTypes containerType, string targetName, QTMTargetType targetType)
	{
		this.name = name;
		this.containerType = containerType;
		this.targetName = targetName;
		this.targetType = targetType;
		UserInterfaceProperties = new QTMTestContainerUserInterfaceProperties(this);
	}

	public void Add(string testName)
	{
		QTMTest qTMTest = new QTMTest(testName, Name, TargetName);
		qTMTest.theRegistry = theRegistry;
		arrayTests.Add(qTMTest);
		hashTests.Add(qTMTest.Name, qTMTest);
	}

	public void Add(QTMTest qTest)
	{
		qTest.TargetName = TargetName;
		qTest.ContainerName = Name;
		qTest.theRegistry = theRegistry;
		arrayTests.Add(qTest);
		hashTests.Add(qTest.Name, qTest);
	}

	public void DeleteALLTests()
	{
		arrayTests.Clear();
		hashTests.Clear();
	}

	public void CreateHashtable()
	{
		hashTests = new Hashtable();
		foreach (QTMTest arrayTest in arrayTests)
		{
			arrayTest.ContainerName = Name;
			arrayTest.TargetName = TargetName;
			hashTests.Add(arrayTest.Name, arrayTest);
			if (arrayTest.Parameters.Count == 0)
			{
				arrayTest.Parameters = null;
			}
		}
	}

	public QTMTest GetTest(string realName)
	{
		try
		{
			if (hashTests.Contains(realName))
			{
				return (QTMTest)hashTests[realName];
			}
		}
		catch
		{
		}
		return null;
	}

	public void CreateTestList(string listOfTests)
	{
		try
		{
			string[] array = listOfTests.Split(',');
			ArrayList arrayList = new ArrayList();
			for (int i = 0; i < array.GetLength(0) - 1; i++)
			{
				arrayList.Add(array[i].Trim());
			}
			arrayList.Sort();
			Hashtable hashtable = new Hashtable();
			foreach (string item in arrayList)
			{
				QTMTest qTMTest = new QTMTest(item, Name, TargetName);
				qTMTest.ContainerLoadedFirstTime = true;
				hashtable.Add(qTMTest.Name, qTMTest);
			}
			switch (ContainerType)
			{
			case QTMTestTypes.ScriptTest:
			case QTMTestTypes.DotNetTest:
			case QTMTestTypes.DotNetTestEx:
				foreach (QTMTest value in hashtable.Values)
				{
					if (hashTests.Contains(value.Name))
					{
						value.Parameters = GetTest(value.Name).Parameters;
					}
				}
				break;
			}
			DeleteALLTests();
			foreach (string item2 in arrayList)
			{
				Add((QTMTest)hashtable[item2]);
			}
		}
		catch
		{
		}
	}

	public void Reset()
	{
		try
		{
			foreach (QTMTest value in hashTests.Values)
			{
				value.ContainerLoadedFirstTime = false;
			}
		}
		catch
		{
		}
	}

	public string GetTestDescription(string testName)
	{
		if (!loaded)
		{
			return "";
		}
		if (containerType == QTMTestTypes.DotNetTest)
		{
			return theRegistry.scheduler.KernelMgr.GetDotNetTestDescription(Name, testName);
		}
		if (containerType == QTMTestTypes.DotNetTestEx)
		{
			return theRegistry.scheduler.KernelMgr.GetDotNetTestDescription(Name, testName);
		}
		if (containerType == QTMTestTypes.HiveTest)
		{
			return theRegistry.scheduler.KernelMgr.GetHiveTestDescription(testName);
		}
		return "";
	}

	public TestProperties GetTestProperties(string testName)
	{
		if (containerType == QTMTestTypes.HiveTest)
		{
			return theRegistry.scheduler.KernelMgr.GetHiveTestProperties(testName);
		}
		if (containerType == QTMTestTypes.DotNetTest)
		{
			return theRegistry.scheduler.KernelMgr.GetDotNetTestProperties(Name, testName);
		}
		if (containerType == QTMTestTypes.DotNetTestEx)
		{
			return theRegistry.scheduler.KernelMgr.GetDotNetTestProperties(Name, testName);
		}
		return new TestProperties();
	}

	public static string GetContainerStringName(QTMTestTypes containerType)
	{
		try
		{
			switch (containerType)
			{
			case QTMTestTypes.CTSTest:
				return "DLL";
			case QTMTestTypes.ScriptTest:
				return "Script";
			case QTMTestTypes.LabVIEWTest:
				return "VI";
			case QTMTestTypes.DotNetTest:
				return "DLL";
			case QTMTestTypes.DotNetTestEx:
				return "DLL";
			case QTMTestTypes.HiveTest:
				return "DLL";
			}
		}
		catch
		{
		}
		return "";
	}

	public void Options()
	{
		switch (containerType)
		{
		case QTMTestTypes.ScriptTest:
			ScriptOptions();
			break;
		case QTMTestTypes.HiveTest:
			HIVEOptions();
			break;
		case QTMTestTypes.LabVIEWTest:
			LabViewOptions();
			break;
		case QTMTestTypes.DotNetTest:
			DotNetOptions(isExclusive: false);
			break;
		case QTMTestTypes.DotNetTestEx:
			DotNetOptions(isExclusive: true);
			break;
		}
	}

	private void ScriptOptions()
	{
		if (arrayTests.Count > 0)
		{
			QTMTest qTMTest = (QTMTest)arrayTests[0];
			if (ScriptTest.Options(theRegistry, Name, qTMTest.Parameters, out var paramNew))
			{
				qTMTest.Parameters = paramNew;
				theRegistry.scheduler.GlobalStatus.RegistrationStatus.TreeChanged = true;
			}
		}
	}

	private void HIVEOptions()
	{
		HiveTest.Options();
	}

	private void LabViewOptions()
	{
		LabVIEWTest.Options();
	}

	private void DotNetOptions(bool isExclusive)
	{
		Hashtable listOfTestParameter = ListOfTestParameter;
		bool flag = false;
		if (!((!isExclusive) ? theRegistry.scheduler.KernelMgr.DotNetOptions(Name, ref listOfTestParameter) : theRegistry.scheduler.KernelMgr.DotNetExOptions(Name, ref listOfTestParameter)))
		{
			return;
		}
		arrayTests = new ArrayList();
		hashTests = new Hashtable();
		ArrayList arrayList = new ArrayList();
		foreach (string key in listOfTestParameter.Keys)
		{
			arrayList.Add(key);
		}
		arrayList.Sort();
		foreach (string item in arrayList)
		{
			if (item.Trim().Length > 0)
			{
				QTMTest qTMTest = new QTMTest(item);
				qTMTest.Parameters = new ParameterCollection(listOfTestParameter[item]);
				Add(qTMTest);
			}
		}
	}

	internal void UpdateDllVersion()
	{
		if (File.Exists(Name))
		{
			FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(Name);
			if (!System.Version.TryParse(versionInfo.FileVersion, out var result))
			{
				result = new Version();
			}
			Version = result.ToString();
		}
	}

	protected void RaiseOnPropertyChanged(string propertyName)
	{
		if (this.PropertyChanged != null)
		{
			this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}
	}
}
