using System;
using QC.QTMDotNetKernelInterface;

namespace QC.QTMToolKit;

[QTMTestClass]
public class GetEnvironmentInfo
{
	private string errMsg;

	[QTMParameter(ParameterIOType.Output, TestList = "GetEnvironmentVariable,SetEnvironmentVariable")]
	public string ErrorString
	{
		get
		{
			return errMsg;
		}
		set
		{
			errMsg = value;
		}
	}

	[QTMTest(Description = "Retrieves the value of an environment variable from the current process.", CanAddToTestPlan = true)]
	[QTMParameter(ArgumentName = "EnvironmentVariable", DefaultValue = "COMPUTERNAME", Description = "Name of the environment variable such as COMPUTERNAME, WINDIR, etc")]
	[QTMParameter(ArgumentName = "ResultStr", DefaultValue = "", Description = "The value of the environment variable specified by variable, or empty if the environment variable is not found.")]
	public bool GetEnvironmentVariable(string EnvironmentVariable, out string ResultStr)
	{
		ResultStr = "";
		ErrorString = "";
		try
		{
			ResultStr = Environment.GetEnvironmentVariable(EnvironmentVariable);
			if (string.IsNullOrEmpty(ResultStr))
			{
				ErrorString = EnvironmentVariable + " is not found.";
				ResultStr = "";
				return false;
			}
			return true;
		}
		catch (Exception ex)
		{
			ErrorString = ex.Message;
			return false;
		}
	}

	[QTMParameter(ArgumentName = "EnvironmentVariable", Description = "Name of the environment variable to set.")]
	[QTMParameter(ArgumentName = "Value", Description = "The value to assign to the environment variable.")]
	[QTMTest(Description = "Set the value of an environment variable in the current process.", CanAddToTestPlan = true)]
	public bool SetEnvironmentVariable(string EnvironmentVariable, string Value)
	{
		ErrorString = "";
		try
		{
			Environment.SetEnvironmentVariable(EnvironmentVariable, Value);
			return true;
		}
		catch (Exception ex)
		{
			ErrorString = ex.Message;
			return false;
		}
	}

	[QTMTest(Description = "Retrieves the system time.", CanAddToTestPlan = true)]
	public string GetSystemTime()
	{
		DateTime now = DateTime.Now;
		return now.Hour + " :" + now.Minute + ":" + now.Second;
	}

	[QTMTest(Description = "Retrieves the system date.", CanAddToTestPlan = true)]
	public string GetSystemDate()
	{
		DateTime now = DateTime.Now;
		return now.Month + "_" + now.Day + "_" + now.Year;
	}

	[QTMParameter(ArgumentName = "FormatString", DefaultValue = "MM_dd_yyyy_HH_mm_ss_fff", Description = "The format to display the DateTime. See http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.100).aspx for more info.")]
	[QTMParameter(ArgumentName = "DateTimeString", Description = "The formatted string representing the current date and time.")]
	[QTMTest(Description = "Retrieves the current DateTime in a formatted string.", CanAddToTestPlan = true)]
	public bool GetFormattedDateTime(string FormatString, out string DateTimeString)
	{
		DateTime now = DateTime.Now;
		try
		{
			DateTimeString = now.ToString(FormatString);
		}
		catch (FormatException)
		{
			DateTimeString = "Invalid format string";
			return false;
		}
		return true;
	}
}
