using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using QC.QsprShared.GlobalData;
using QC.QsprShared.Trace;
using QC.QsprTestObjects.Tests.Base;
using QC.QTMInterface;

namespace QC.QsprTestObjects.Loops;

public class ScenarioHandler
{
	public const string LOOPVALUE_SEP = "__";

	public const string LOOPS_SEP = "_._";

	public const string LOOPSCENARIO_SEP = "_ _";

	public const string SEQUENCE_TESTNAME_SEP = "_;_";

	private Hashtable loopStack = new Hashtable();

	private Hashtable backUpLoopStack = new Hashtable();

	private int backupScenarioID;

	private int currentScenarioID;

	public void ProcessMessage(TestMsgType msgType, TestMessage message, RootTest root)
	{
		try
		{
			switch (msgType)
			{
			case TestMsgType.OnUnitEnd:
				Reset();
				break;
			case TestMsgType.OnLoopStart:
				LoopStart(message, root);
				break;
			case TestMsgType.OnLoopEnd:
				LoopEnd(message, root);
				break;
			case TestMsgType.OnEndOfDebugTesting:
				Reset();
				break;
			case TestMsgType.OnUnitStart:
			case TestMsgType.OnCategoryStart:
			case TestMsgType.OnCategoryEnd:
			case TestMsgType.OnTestStop:
			case TestMsgType.OnDLLLoaded:
			case TestMsgType.OnDebugMsg:
			case TestMsgType.OnStartOfDebugTesting:
				break;
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "ProcessMessage " + ex.Message, TraceMessageType.Exception);
		}
	}

	private void Reset()
	{
		currentScenarioID = 0;
		loopStack.Clear();
	}

	public void Update(GlobalStatus globalStatus)
	{
		if (!globalStatus.SecondaryQueueRunning)
		{
			globalStatus.LoopDetails = GetLoopDetailsWithoutDescription();
			globalStatus.LoopDetailsComplete = GetLoopDetailsComplete();
			backupScenarioID = currentScenarioID;
			backUpLoopStack = (Hashtable)loopStack.Clone();
		}
	}

	private void LoopStart(TestMessage message, RootTest root)
	{
		try
		{
			structLoopValues structLoopValues = (structLoopValues)message.Object;
			ScenarioDetails scenarioDetails = new ScenarioDetails();
			scenarioDetails.LoopString = structLoopValues.sLoopName + "__" + structLoopValues.sLoopValue;
			scenarioDetails.Description = structLoopValues.sDesc;
			scenarioDetails.IsLinkedTree = structLoopValues.isLinkedTree;
			scenarioDetails.IsInputParameter = structLoopValues.isInputParameter;
			if (structLoopValues.bPartOfScenario)
			{
				scenarioDetails.LoopString = ((ScenarioDetails)loopStack[currentScenarioID]).LoopString + "_ _" + scenarioDetails.LoopString;
				scenarioDetails.LoopName = ((ScenarioDetails)loopStack[currentScenarioID]).LoopName + "," + structLoopValues.sLoopName;
				scenarioDetails.LoopValues = ((ScenarioDetails)loopStack[currentScenarioID]).LoopValues + "," + structLoopValues.sLoopValue;
				scenarioDetails.iRowIndex = ((ScenarioDetails)loopStack[currentScenarioID]).iRowIndex;
				loopStack[currentScenarioID] = scenarioDetails;
			}
			else
			{
				scenarioDetails.LoopName = structLoopValues.sLoopName;
				scenarioDetails.LoopValues = structLoopValues.sLoopValue;
				scenarioDetails.iRowIndex = structLoopValues.iRowIndex;
				currentScenarioID++;
				loopStack.Add(currentScenarioID, scenarioDetails);
			}
			root.Scheduler.GlobalStatus.LoopDetails = GetLoopDetailsWithoutDescription();
			root.Scheduler.GlobalStatus.LoopDetailsComplete = GetLoopDetailsComplete();
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "LoopStart " + ex.Message, TraceMessageType.Exception);
		}
	}

	private void LoopEnd(TestMessage message, RootTest root)
	{
		try
		{
			loopStack.Remove(currentScenarioID);
			currentScenarioID--;
			root.Scheduler.GlobalStatus.LoopDetails = GetLoopDetailsWithoutDescription();
			root.Scheduler.GlobalStatus.LoopDetailsComplete = GetLoopDetailsComplete();
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "LoopEnd " + ex.Message, TraceMessageType.Exception);
		}
	}

	private string GetLoopDetailsComplete()
	{
		string text = "";
		try
		{
			for (int i = 1; i < loopStack.Count; i++)
			{
				if (!((ScenarioDetails)loopStack[i]).IsLinkedTree)
				{
					text = text + ((ScenarioDetails)loopStack[i]).LoopString + "_._";
				}
			}
			if (loopStack.ContainsKey(loopStack.Count) && !((ScenarioDetails)loopStack[loopStack.Count]).IsLinkedTree)
			{
				text += ((ScenarioDetails)loopStack[loopStack.Count]).LoopString;
			}
			if (text.EndsWith("_._"))
			{
				text = text.Substring(0, text.Length - "_._".Length);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetLoopDetails " + ex.Message, TraceMessageType.Exception);
		}
		return text;
	}

	private string GetLoopDetailsWithoutDescription()
	{
		string text = "";
		try
		{
			for (int i = 1; i < loopStack.Count; i++)
			{
				if (!((ScenarioDetails)loopStack[i]).IsLinkedTree)
				{
					text = text + ((ScenarioDetails)loopStack[i]).LoopString + "_._";
				}
			}
			if (loopStack.ContainsKey(loopStack.Count) && ((ScenarioDetails)loopStack[loopStack.Count]).Description.Trim() == "" && !((ScenarioDetails)loopStack[loopStack.Count]).IsLinkedTree)
			{
				text += ((ScenarioDetails)loopStack[loopStack.Count]).LoopString;
			}
			if (text.EndsWith("_._"))
			{
				text = text.Substring(0, text.Length - "_._".Length);
			}
		}
		catch (Exception ex)
		{
			TraceMessage.Write(ToString(), "GetLoopDetails " + ex.Message, TraceMessageType.Exception);
		}
		return text;
	}

	public static string GetUniqueLoopName(LoopScenarioValues loopScenario, string currentLoopName)
	{
		if (loopScenario == null || loopScenario.LoopConditions == null || loopScenario.LoopConditions.Count == 0)
		{
			return currentLoopName;
		}
		string loopName = currentLoopName;
		int num = 1;
		bool flag = true;
		IEnumerable<LoopCondition> source = loopScenario.LoopConditions.OfType<LoopCondition>();
		while (flag)
		{
			flag = false;
			if (source.Where((LoopCondition condition) => condition.LoopName.ToLower() == loopName.ToLower()).Count() > 0)
			{
				flag = true;
				loopName = currentLoopName + num;
				num++;
			}
		}
		return loopName;
	}
}
