using System;
using System.Threading;
using QC.QTMDotNetKernelInterface;

namespace QC.QTMToolKit.Tools;

[QTMTestClass]
public class QTMTools
{
	private string globalVariableName = "";

	private string globalVariableValue = "";

	private string globalVariableGetValue = "";

	private int duration = 0;

	private int stopAbortCheckInterval = 0;

	private string configFilePath;

	[QTMParameter(ParameterIOType.Input, TestList = "SetGlobalVariable,GetGlobalVariable", Description = "Name of the Global Variable.")]
	public string Name
	{
		get
		{
			return globalVariableName;
		}
		set
		{
			globalVariableName = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "SetGlobalVariable", Description = "Value of the Global Variable.")]
	public string Value
	{
		get
		{
			return globalVariableValue;
		}
		set
		{
			globalVariableValue = value;
		}
	}

	[QTMParameter(ParameterIOType.Output, TestList = "GetGlobalVariable", Description = "Value of the Global Variable.")]
	public string CurrentValue
	{
		get
		{
			return globalVariableGetValue;
		}
		set
		{
			globalVariableGetValue = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, UnitOfMeasure = "msec", DefaultValue = "0", TestList = "Sleep", Description = "The number of milliSeconds for which thead is blocked.")]
	public int Duration
	{
		get
		{
			return duration;
		}
		set
		{
			duration = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, UnitOfMeasure = "msec", DefaultValue = "1000", TestList = "Sleep", Description = "Interval in milliSeconds for checking STOP or ABORT message.")]
	public int StopAbortCheckInterval
	{
		get
		{
			return stopAbortCheckInterval;
		}
		set
		{
			stopAbortCheckInterval = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, DefaultValue = "c:\\qtm\\Configs", TestList = "SetConfigFilePath", Description = " Overrides the Config file path set in the Root Folder", UnitOfMeasure = " ", Constraints = " ", DisplayHint = DisplayHints.FILE, NumberOfElements = 1)]
	public string ConfigFilePath
	{
		get
		{
			return configFilePath;
		}
		set
		{
			configFilePath = value;
		}
	}

	public bool SetGlobalVariable()
	{
		return GlobalVariable.SetGlobalVariable(globalVariableName, globalVariableValue);
	}

	public bool GetGlobalVariable()
	{
		return GlobalVariable.GetGlobalVariable(globalVariableName, out globalVariableGetValue);
	}

	[QTMTest("Sleeps for the length od Duration parameter. Checks for Stop/Abort message at every stopAbortCheckInterval parameter value")]
	public bool Sleep()
	{
		int num = Duration / StopAbortCheckInterval;
		int millisecondsTimeout = Duration % StopAbortCheckInterval;
		for (int i = 0; i < num; i++)
		{
			Thread.Sleep(StopAbortCheckInterval);
			if (QTMInfo.CheckForAnyStopAbortMsg)
			{
				break;
			}
		}
		if (!QTMInfo.CheckForAnyStopAbortMsg)
		{
			Thread.Sleep(millisecondsTimeout);
		}
		return true;
	}

	[QTMTest("Sets an output parameter 'Error Code' with value from the input Parameter 'erroCode'")]
	public bool SetErrorCode(string ErrorCodeValue, string Description, out string ErrorCode, out string ErrorDescription)
	{
		ErrorCode = ErrorCodeValue;
		ErrorDescription = Description;
		return true;
	}

	public bool SetConfigFilePath()
	{
		return GlobalVariable.SetGlobalVariable("ConfigFilePath", ConfigFilePath);
	}

	[QTMTest("Saves debug messages for specified Debug window names or save all debug messages if debugWindowNames is blank.")]
	[QTMParameter(ArgumentName = "clearAfterSave", Description = "Clears the Debug messages from the window after saving")]
	[QTMParameter(ArgumentName = "appendText", DefaultValue = "True", Description = "Append text to an existing file, if the file does not existed, it is created. ")]
	[QTMParameter(ArgumentName = "debugWindowNames", Description = " Debug Window Names separated by comma.  Leave blank to save all debug messages.")]
	public bool SaveDebugMessages(string debugWindowNames, bool appendText, bool clearAfterSave)
	{
		try
		{
			if (string.IsNullOrEmpty(debugWindowNames))
			{
				DebugMessage.SaveAll(appendText);
				if (clearAfterSave)
				{
					DebugMessage.ClearAll();
				}
			}
			else
			{
				string[] array = debugWindowNames.Split(',');
				string[] array2 = array;
				foreach (string debugWindowName in array2)
				{
					DebugMessage.Save(debugWindowName, appendText);
					if (clearAfterSave)
					{
						DebugMessage.Clear(debugWindowName);
					}
				}
			}
			return true;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("SaveDebugMessages", $" Save Debug messages failed for {debugWindowNames} {ex.Message}.");
			return false;
		}
	}

	[QTMTest("Clear debug messages for specified Debug window names or clear all debug messages if debug Window Names are blank.")]
	[QTMParameter(ArgumentName = "debugWindowNames", Description = " Debug Window Names separated by comma.  Leave blank to clear all debug messages.")]
	public bool ClearDebugMessages(string debugWindowNames)
	{
		try
		{
			if (string.IsNullOrEmpty(debugWindowNames))
			{
				DebugMessage.ClearAll();
			}
			else
			{
				string[] array = debugWindowNames.Split(',');
				string[] array2 = array;
				foreach (string debugWindowName in array2)
				{
					DebugMessage.Clear(debugWindowName);
				}
			}
			return true;
		}
		catch (Exception ex)
		{
			DebugMessage.Write("ClearDebugMessages", $" Clear Debug messages failed for {debugWindowNames} {ex.Message}.");
			return false;
		}
	}
}
