using System;
using System.Collections;
using System.Collections.Generic;
using QC.GlobalVariableManager;
using QC.QsprShared.Config;
using QC.QsprShared.Trace;
using QC.QTMInterface;

namespace QC.QsprShared.GlobalData;

public class GlobalStatus
{
	public enum StatusFlags : byte
	{
		RIGHT_CLICK = 1,
		STOP = 2,
		GO_NO_GO = 4,
		TEST_RUNNING = 8,
		BRANCH_ON_ERROR = 0x10,
		ABORT = 0x20,
		SAVING_TREE = 0x40,
		PAUSE = 0x80
	}

	public enum ExecutionStatus
	{
		Unknown = 0,
		Passed = 1,
		Failed = 2,
		Executing = 4,
		AwaitingFeedback = 5
	}

	public enum TestStatus
	{
		NotSet,
		Passed,
		Failed,
		Exception
	}

	public enum RunningStateChanged
	{
		TestRunning,
		SecondaryQueueRunning,
		Pause,
		Stop,
		Abort
	}

	public delegate void TreeSavingStatusChanged(bool saving);

	public delegate void RunningStatusChanged(RunningStateChanged state);

	public int GLOBAL_STATUS;

	public int SECONDARY_STATUS;

	private bool secondaryQueueRunning;

	private TestTreeSelectedNode tNodeStatus = TestTreeSelectedNode.Empty;

	private RegistrySelectedNode rNodeStatus = RegistrySelectedNode.Empty;

	private string branchOnTestIndex = "";

	private string currentNodeIndex = "";

	private string currentNodePath = "";

	private bool simulatorMode;

	private static bool suppressMessagesMode = false;

	private bool showFailedtests;

	private static bool automationMode = false;

	private static bool regexSupportEnabled = true;

	private TreeRegistrationStatus registrationStatus = new TreeRegistrationStatus();

	private string loopDetails = "";

	private string secondaryLoopDetails = "";

	private string loopDetailsComplete = "";

	private string secondaryLoopDetailsComplete = "";

	private bool isDebugMode;

	private bool isInteractiveMode;

	private TestResult globalTestResult = TestResult.Passed;

	private bool stopOnFirstTestFailure;

	private bool pauseOnTestFailure;

	private ArrayList pluginList = new ArrayList();

	private string listOfFailedTest = "";

	private bool setupTestMode;

	private bool testCaseRunning;

	private bool processingTestDone;

	private bool testMsgHold;

	private bool applicationLoaded;

	private BailOutLogicSelection bailOutLogic;

	private double bailOutLogicFixedNumber;

	private double bailOutLogicFixedPercentage;

	private double bailOutLogicFixedNumberConsective;

	private double bailOutLogicFixedPercentageConsective;

	private bool bailedOut;

	private bool skipTCOnOpenIR;

	private bool skipTCOnOpenSetupIssues;

	private bool skipTCOnOpenAutomationIssues;

	private string skipTCOnIssuesAPIBase = "http://qlap:8777/api/TestCases/GetOpenIssues";

	private int numberOfDaysToLookUpIssues = 30;

	private int totalSelectedTests;

	private int failedTests;

	private int consectiveFailedTests;

	private int totalSelectedTestCases;

	public List<long> FailedTestCases = new List<long>();

	public List<long> ConsectiveFailedTestCases = new List<long>();

	private bool applyDynamicRetry;

	private bool inDynamicRetry;

	private TestResult setupTestStatus = TestResult.Passed;

	private ExecutionStatus interactiveStatus;

	private TestStatus internalRunTestStatus;

	private TestStatus externalRunTestStatus;

	private CurrentTestInfo nextTestInfo;

	private bool processTestResultsPlugIns;

	private bool dvtMode;

	private object root;

	public bool ProcessingTestDone
	{
		get
		{
			return processingTestDone;
		}
		set
		{
			processingTestDone = value;
		}
	}

	public bool InDynamicRetry
	{
		get
		{
			return inDynamicRetry;
		}
		set
		{
			inDynamicRetry = value;
		}
	}

	public bool ApplyDynamicRetry
	{
		get
		{
			return applyDynamicRetry;
		}
		set
		{
			applyDynamicRetry = value;
		}
	}

	public int TotalSelectedTestCases
	{
		get
		{
			return totalSelectedTestCases;
		}
		set
		{
			totalSelectedTestCases = value;
		}
	}

	public int TotalSelectedTests
	{
		get
		{
			return totalSelectedTests;
		}
		set
		{
			totalSelectedTests = value;
		}
	}

	public int FailedTests
	{
		get
		{
			return failedTests;
		}
		set
		{
			failedTests = value;
		}
	}

	public int ConsectiveFailedTests
	{
		get
		{
			return consectiveFailedTests;
		}
		set
		{
			consectiveFailedTests = value;
		}
	}

	public bool BailedOut
	{
		get
		{
			return bailedOut;
		}
		set
		{
			bailedOut = value;
		}
	}

	public double BailOutLogicFixedNumber
	{
		get
		{
			return bailOutLogicFixedNumber;
		}
		set
		{
			bailOutLogicFixedNumber = value;
		}
	}

	public double BailOutLogicFixedPercentage
	{
		get
		{
			return bailOutLogicFixedPercentage;
		}
		set
		{
			bailOutLogicFixedPercentage = value;
		}
	}

	public double BailOutLogicFixedNumberConsective
	{
		get
		{
			return bailOutLogicFixedNumberConsective;
		}
		set
		{
			bailOutLogicFixedNumberConsective = value;
		}
	}

	public double BailOutLogicFixedPercentageConsective
	{
		get
		{
			return bailOutLogicFixedPercentageConsective;
		}
		set
		{
			bailOutLogicFixedPercentageConsective = value;
		}
	}

	public BailOutLogicSelection BailOutLogic
	{
		get
		{
			return bailOutLogic;
		}
		set
		{
			bailOutLogic = value;
		}
	}

	public bool SkipTCOnOpenIR
	{
		get
		{
			return skipTCOnOpenIR;
		}
		set
		{
			skipTCOnOpenIR = value;
		}
	}

	public bool SkipTCOnOpenSetupIssues
	{
		get
		{
			return skipTCOnOpenSetupIssues;
		}
		set
		{
			skipTCOnOpenSetupIssues = value;
		}
	}

	public bool SkipTCOnOpenAutomationIssues
	{
		get
		{
			return skipTCOnOpenAutomationIssues;
		}
		set
		{
			skipTCOnOpenAutomationIssues = value;
		}
	}

	public string SkipTCOnIssuesAPIBase
	{
		get
		{
			return skipTCOnIssuesAPIBase;
		}
		set
		{
			skipTCOnIssuesAPIBase = value;
		}
	}

	public int NumberOfDaysToLookUpIssues
	{
		get
		{
			return numberOfDaysToLookUpIssues;
		}
		set
		{
			numberOfDaysToLookUpIssues = value;
		}
	}

	public bool ApplicationLoaded
	{
		get
		{
			return applicationLoaded;
		}
		set
		{
			applicationLoaded = value;
		}
	}

	public bool EndOfUnitTestingHandled { get; set; }

	public string SchedulerID { get; set; }

	public bool TestMsgHold
	{
		get
		{
			return testMsgHold;
		}
		set
		{
			testMsgHold = value;
		}
	}

	public bool SetupTestMode
	{
		get
		{
			return setupTestMode;
		}
		set
		{
			setupTestMode = value;
			if (setupTestMode)
			{
				setupTestStatus = TestResult.Passed;
			}
		}
	}

	public bool TestCaseRunning
	{
		get
		{
			return testCaseRunning;
		}
		set
		{
			testCaseRunning = value;
		}
	}

	public TestResult SetupTestStatus
	{
		get
		{
			return setupTestStatus;
		}
		set
		{
			setupTestStatus = value;
		}
	}

	public bool IsDebugMode
	{
		get
		{
			return isDebugMode;
		}
		set
		{
			isDebugMode = value;
		}
	}

	public ExecutionStatus InteractiveStatus
	{
		get
		{
			return interactiveStatus;
		}
		set
		{
			interactiveStatus = value;
		}
	}

	public bool IsInteractiveMode
	{
		get
		{
			return isInteractiveMode;
		}
		set
		{
			isInteractiveMode = value;
		}
	}

	public TestStatus InternalRunTestStatus
	{
		get
		{
			return internalRunTestStatus;
		}
		set
		{
			internalRunTestStatus = value;
		}
	}

	public TestStatus ExternalRunTestStatus
	{
		get
		{
			return externalRunTestStatus;
		}
		set
		{
			externalRunTestStatus = value;
		}
	}

	public CurrentTestInfo NextTestInfo
	{
		get
		{
			return nextTestInfo;
		}
		set
		{
			nextTestInfo = value;
		}
	}

	public int Status => GLOBAL_STATUS;

	private int CURRENT_STATUS
	{
		get
		{
			if (SecondaryQueueRunning)
			{
				return SECONDARY_STATUS;
			}
			return GLOBAL_STATUS;
		}
		set
		{
			if (SecondaryQueueRunning)
			{
				SECONDARY_STATUS = value;
			}
			else
			{
				GLOBAL_STATUS = value;
			}
		}
	}

	public bool SecondaryQueueRunning
	{
		get
		{
			return secondaryQueueRunning;
		}
		set
		{
			if (value)
			{
				secondaryQueueRunning = value;
				OnRunningStateChanged(RunningStateChanged.SecondaryQueueRunning);
			}
			else
			{
				secondaryQueueRunning = value;
				OnRunningStateChanged(RunningStateChanged.SecondaryQueueRunning);
				GlobalInformation.TestRunningById[SchedulerID] = true;
			}
		}
	}

	public ArrayList PluginList
	{
		get
		{
			return pluginList;
		}
		set
		{
			pluginList = value;
		}
	}

	public TestResult GlobalTestResult
	{
		get
		{
			return globalTestResult;
		}
		set
		{
			globalTestResult = value;
			QTMGlobalVariable.SetGlobalVariable("GONOGO_RESULT", globalTestResult.ToString(), SchedulerID);
		}
	}

	public bool ProcessTestResultsPlugIns
	{
		get
		{
			return processTestResultsPlugIns;
		}
		set
		{
			processTestResultsPlugIns = value;
		}
	}

	public TestTreeSelectedNode TestTreeNodeStatus
	{
		get
		{
			return tNodeStatus;
		}
		set
		{
			try
			{
				TraceMessage.Write("QC.QTMApplication.GlobalStatus", "TestTreeNodeStatus", TraceMessageType.Trace);
				if (QTMData.UpdateToolBar != null)
				{
					QTMData.UpdateToolBar(value);
				}
				tNodeStatus = value;
			}
			catch (Exception ex)
			{
				TraceMessage.Write("QC.QTMApplication.GlobalStatus", "TestTreeNodeStatus " + ex.Message, TraceMessageType.Exception);
			}
		}
	}

	public RegistrySelectedNode RegistryNodeStatus
	{
		get
		{
			return rNodeStatus;
		}
		set
		{
			try
			{
				TraceMessage.Write("QC.QTMApplication.GlobalStatus", "RegistrySelectedNode", TraceMessageType.Trace);
				QTMData.UpdateToolBar(value);
				rNodeStatus = value;
			}
			catch (Exception ex)
			{
				TraceMessage.Write("QC.QTMApplication.GlobalStatus", "RegistrySelectedNode " + ex.Message, TraceMessageType.Exception);
			}
		}
	}

	public bool RightClick
	{
		get
		{
			if ((CURRENT_STATUS & 1) == 1)
			{
				return true;
			}
			return false;
		}
		set
		{
			if (value)
			{
				CURRENT_STATUS |= 1;
			}
			else
			{
				CURRENT_STATUS &= -2;
			}
		}
	}

	public bool DVTMode
	{
		get
		{
			return dvtMode;
		}
		set
		{
			dvtMode = value;
			if (value)
			{
				QTMGlobalVariable.SetGlobalVariable("TreeType", TreeTypes.DVT.ToString(), SchedulerID);
			}
		}
	}

	public bool SimulatorMode
	{
		get
		{
			return simulatorMode;
		}
		set
		{
			simulatorMode = value;
			GlobalInformation.SetGlobalVariable("SimulationMode", value);
		}
	}

	public bool StopOnFirstTestFailure
	{
		get
		{
			if (!IsDebugMode)
			{
				return stopOnFirstTestFailure;
			}
			return false;
		}
		set
		{
			if (value && pauseOnTestFailure)
			{
				pauseOnTestFailure = false;
			}
			stopOnFirstTestFailure = value;
		}
	}

	public bool PauseOnTestFailure
	{
		get
		{
			if (!IsDebugMode)
			{
				return pauseOnTestFailure;
			}
			return false;
		}
		set
		{
			if (value && stopOnFirstTestFailure)
			{
				stopOnFirstTestFailure = false;
			}
			pauseOnTestFailure = value;
		}
	}

	public static bool SuppressAllMessagesMode
	{
		get
		{
			return suppressMessagesMode;
		}
		set
		{
			if (!value)
			{
				TraceMessage.EndSuppressedMsgLogging();
			}
			suppressMessagesMode = value;
		}
	}

	public static bool AutomationMode
	{
		get
		{
			return automationMode;
		}
		set
		{
			automationMode = value;
			GlobalInformation.SetAutomationModeFlag();
		}
	}

	public static bool RegexSupportEnabled
	{
		get
		{
			return regexSupportEnabled;
		}
		set
		{
			regexSupportEnabled = value;
		}
	}

	public bool ShowFailedTests
	{
		get
		{
			return showFailedtests;
		}
		set
		{
			showFailedtests = value;
		}
	}

	public bool Stop
	{
		get
		{
			if ((CURRENT_STATUS & 2) == 2)
			{
				return true;
			}
			return false;
		}
		set
		{
			TraceMessage.Write("QC.QTMApplication.GlobalStatus", "STOP", TraceMessageType.Trace);
			if (value)
			{
				if (!Stop)
				{
					CURRENT_STATUS ^= 2;
				}
				QTMGlobalVariable.SetGlobalVariable("QSPR_STOP", true.ToString(), SchedulerID);
				QTMGlobalVariable.SetGlobalVariable("QTM_STOP", true.ToString(), SchedulerID);
			}
			else
			{
				if (Stop)
				{
					CURRENT_STATUS ^= 2;
				}
				QTMGlobalVariable.SetGlobalVariable("QSPR_STOP", false.ToString(), SchedulerID);
				QTMGlobalVariable.SetGlobalVariable("QTM_STOP", false.ToString(), SchedulerID);
			}
			OnRunningStateChanged(RunningStateChanged.Stop);
		}
	}

	public bool Pause
	{
		get
		{
			if ((CURRENT_STATUS & 0x80) == 128)
			{
				return true;
			}
			return false;
		}
		set
		{
			TraceMessage.Write("QC.QTMApplication.GlobalStatus", "Pause", TraceMessageType.Trace);
			if (value)
			{
				if (!Pause)
				{
					CURRENT_STATUS ^= 128;
					OnRunningStateChanged(RunningStateChanged.Pause);
				}
			}
			else if (Pause)
			{
				CURRENT_STATUS ^= 128;
				OnRunningStateChanged(RunningStateChanged.Pause);
			}
		}
	}

	public bool Abort
	{
		get
		{
			if ((CURRENT_STATUS & 0x20) == 32)
			{
				return true;
			}
			return false;
		}
		set
		{
			if (value)
			{
				if (!Abort)
				{
					CURRENT_STATUS ^= 32;
				}
				QTMGlobalVariable.SetGlobalVariable("QSPR_ABORT", true.ToString(), SchedulerID);
				QTMGlobalVariable.SetGlobalVariable("QTM_ABORT", true.ToString(), SchedulerID);
				Stop = true;
			}
			else
			{
				if (Abort)
				{
					CURRENT_STATUS ^= 32;
				}
				QTMGlobalVariable.SetGlobalVariable("QSPR_ABORT", false.ToString(), SchedulerID);
				QTMGlobalVariable.SetGlobalVariable("QTM_ABORT", false.ToString(), SchedulerID);
			}
			OnRunningStateChanged(RunningStateChanged.Abort);
		}
	}

	public bool SavingTree
	{
		get
		{
			if ((GLOBAL_STATUS & 0x40) == 64)
			{
				return true;
			}
			return false;
		}
		set
		{
			if (value != SavingTree)
			{
				GLOBAL_STATUS ^= 64;
				OnTreeSavingStateChanged(value);
			}
		}
	}

	public bool BranchOnError
	{
		get
		{
			if ((CURRENT_STATUS & 0x10) == 16)
			{
				return true;
			}
			return false;
		}
		set
		{
			if (value)
			{
				if (!BranchOnError)
				{
					CURRENT_STATUS ^= 16;
				}
			}
			else if (BranchOnError)
			{
				CURRENT_STATUS ^= 16;
				BranchOnTestIndex = "";
			}
		}
	}

	public bool TestRunning
	{
		get
		{
			if ((CURRENT_STATUS & 8) == 8)
			{
				return true;
			}
			return false;
		}
		set
		{
			if (value)
			{
				CURRENT_STATUS |= 8;
				GlobalInformation.TestRunningById[SchedulerID] = true;
			}
			else
			{
				CURRENT_STATUS &= -9;
				GlobalInformation.TestRunningById[SchedulerID] = false;
			}
			OnRunningStateChanged(RunningStateChanged.TestRunning);
		}
	}

	public TreeRegistrationStatus RegistrationStatus
	{
		get
		{
			return registrationStatus;
		}
		set
		{
			registrationStatus = value;
		}
	}

	public string BranchOnTestIndex
	{
		get
		{
			return branchOnTestIndex;
		}
		set
		{
			branchOnTestIndex = value;
		}
	}

	public string CurrentNodeIndex
	{
		get
		{
			return currentNodeIndex;
		}
		set
		{
			currentNodeIndex = value;
		}
	}

	public object RootTest
	{
		get
		{
			return root;
		}
		set
		{
			root = value;
		}
	}

	public string TreeName { get; set; }

	public string CurrentNodePath
	{
		get
		{
			return currentNodePath;
		}
		set
		{
			currentNodePath = value;
		}
	}

	public string LoopDetails
	{
		get
		{
			if (SecondaryQueueRunning)
			{
				return secondaryLoopDetails;
			}
			return loopDetails;
		}
		set
		{
			if (SecondaryQueueRunning)
			{
				secondaryLoopDetails = value;
			}
			else
			{
				loopDetails = value;
			}
		}
	}

	public string LoopDetailsComplete
	{
		get
		{
			if (SecondaryQueueRunning)
			{
				return secondaryLoopDetailsComplete;
			}
			return loopDetailsComplete;
		}
		set
		{
			if (SecondaryQueueRunning)
			{
				secondaryLoopDetailsComplete = value;
			}
			else
			{
				loopDetailsComplete = value;
			}
		}
	}

	public string ListOfFailedTests
	{
		get
		{
			return listOfFailedTest;
		}
		set
		{
			listOfFailedTest = value;
		}
	}

	public event TreeSavingStatusChanged TreeSavingStatusChangedEvent;

	public event RunningStatusChanged RunningStatusChangedEvent;

	public void Reset()
	{
		CURRENT_STATUS = 0;
		QTMGlobalVariable.SetGlobalVariable("QSPR_STOP", false.ToString(), SchedulerID);
		QTMGlobalVariable.SetGlobalVariable("QSPR_ABORT", false.ToString(), SchedulerID);
		QTMGlobalVariable.SetGlobalVariable("QTM_ABORT", false.ToString(), SchedulerID);
		QTMGlobalVariable.SetGlobalVariable("QTM_STOP", false.ToString(), SchedulerID);
		Pause = false;
		Stop = false;
		TestCaseRunning = false;
		if (!SecondaryQueueRunning)
		{
			ListOfFailedTests = "";
		}
	}

	private void OnTreeSavingStateChanged(bool saving)
	{
		if (this.TreeSavingStatusChangedEvent != null)
		{
			this.TreeSavingStatusChangedEvent(saving);
		}
	}

	private void OnRunningStateChanged(RunningStateChanged state)
	{
		if (this.RunningStatusChangedEvent != null)
		{
			this.RunningStatusChangedEvent(state);
		}
	}
}
