using System;
using System.Collections;
using System.Reflection;
using System.Windows.Forms;
using QC.QTMDotNetKernelInterface;
using QC.QTMDotNetKernelInterface.Environment;

namespace QC.QTMDotNetKernel.Registry;

public class TestAssembly : MarshalByRefObject
{
	private string name = "";

	private bool hideTests;

	private Assembly assembly;

	private Hashtable classes = new Hashtable();

	private TestRegistry parent;

	public string Name
	{
		get
		{
			return name;
		}
		set
		{
			name = value;
		}
	}

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

	public Assembly Assembly
	{
		get
		{
			return assembly;
		}
		set
		{
			assembly = value;
		}
	}

	public Hashtable Classes
	{
		get
		{
			return classes;
		}
		set
		{
			classes = value;
		}
	}

	public TestRegistry Parent
	{
		get
		{
			return parent;
		}
		set
		{
			parent = value;
		}
	}

	public QSPREnvironment Environment
	{
		get
		{
			return Parent.Environment;
		}
		set
		{
			Parent.Environment = value;
		}
	}

	public TestAssembly()
	{
	}

	public TestAssembly(string name)
	{
		this.name = name;
		assembly = Assembly.LoadFrom(this.name);
		GetAssemblyAttributes(assembly);
		Type[] types = assembly.GetTypes();
		foreach (Type type in types)
		{
			if (type.IsClass)
			{
				AddClass(type);
			}
		}
		ApplyAttributes();
	}

	public void GetAssemblyAttributes(Assembly assembly)
	{
		object[] customAttributes = assembly.GetCustomAttributes(typeof(QTMAssembly), inherit: false);
		object[] array = customAttributes;
		foreach (object obj in array)
		{
			if (obj is QTMAssembly)
			{
				QTMAssembly qTMAssembly = (QTMAssembly)obj;
				hideTests = qTMAssembly.HideTests;
				break;
			}
		}
	}

	public string[] DebugDomain()
	{
		AppDomain currentDomain = AppDomain.CurrentDomain;
		_ = currentDomain.FriendlyName;
		Assembly[] assemblies = currentDomain.GetAssemblies();
		string[] array = new string[assemblies.Length];
		for (int i = 0; i < assemblies.Length; i++)
		{
			array[i] = assemblies[i].FullName;
		}
		return array;
	}

	public bool AddClass(Type type)
	{
		try
		{
			TestClass testClass = new TestClass(type);
			if (!classes.Contains(testClass.FullName))
			{
				classes.Add(testClass.FullName, testClass);
				testClass.Parent = this;
			}
			else
			{
				MessageBox.Show("Duplicate : " + testClass.FullName);
			}
			return true;
		}
		catch (Exception ex)
		{
			MessageBox.Show("Error loading : " + ex.Message);
		}
		return false;
	}

	public void ApplyAttributes()
	{
		try
		{
			foreach (TestClass value in classes.Values)
			{
				value.ApplyAttributes();
			}
		}
		catch
		{
		}
	}

	public void CreateTestSelections(Hashtable listOfTestParameter)
	{
		try
		{
			foreach (string key2 in listOfTestParameter.Keys)
			{
				if (key2.Trim().Length > 0)
				{
					string[] array = key2.Split('.');
					string text2 = array[array.Length - 1];
					string key = key2.Substring(0, key2.Length - text2.Length - 1);
					if (classes.Contains(key))
					{
						TestClass testClass = (TestClass)classes[key];
						testClass.CreateTestSelections(text2, listOfTestParameter[key2]);
					}
				}
			}
		}
		catch
		{
		}
	}
}
