using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using QC.QsprShared.Dialogs;
using QC.QsprShared.GlobalData;

namespace QC.QTMApplication;

public class QTMArguments
{
	private const string GLOBAL_VARIABLE = "/gv:";

	private const string CLOSE_AFTER_RUNNING = "/close";

	private const string LOCATION = "/location:";

	private const string NO_SPLASH = "/nosplashscreen";

	private const string SN = "/sn:";

	private const string USAGE = "/usage";

	private const string USAGE2 = "/?";

	private const string USAGE3 = "/help";

	private const string RUN_TREE = "/runtree";

	private const string LOCKED = "/locked";

	private const string QTM_ID = "/id:";

	private const string TOP_MOST = "/topmost";

	private const string MAX_TEST_TIME = "/maxtesttime:";

	private const string SUPPRESS_ALL_MSGS = "/suppressmessages";

	private const string HELP = "/help";

	private const string DEBUG_ARG = "/debug:";

	private const string BG_COLOR = "/bgcolor:";

	private const string HIDE_WINDOW = "/hide";

	private const string MODE = "/mode:";

	private bool locked;

	private string id = "";

	private bool topMost;

	private int maxTestTime = -1;

	private Dictionary<string, string> globalVariables = new Dictionary<string, string>();

	private string filename = "";

	private Point startingLocation = new Point(0, 0);

	private bool closeAfterRunning;

	private string serialNumber = "";

	private bool runTree;

	private bool showSplashScreen = true;

	private string debugArg = string.Empty;

	private string bgColor = string.Empty;

	private bool hideWindow;

	public bool Locked
	{
		get
		{
			return locked;
		}
		set
		{
			locked = value;
		}
	}

	public string ID
	{
		get
		{
			return id;
		}
		set
		{
			id = value;
		}
	}

	public bool TopMost
	{
		get
		{
			return topMost;
		}
		set
		{
			topMost = value;
		}
	}

	public int MaxTestTime
	{
		get
		{
			return maxTestTime;
		}
		set
		{
			maxTestTime = value;
		}
	}

	public Dictionary<string, string> GlobalVariables
	{
		get
		{
			return globalVariables;
		}
		set
		{
			globalVariables = value;
		}
	}

	public string Filename
	{
		get
		{
			return filename;
		}
		set
		{
			filename = value;
		}
	}

	public Point StartingLocation
	{
		get
		{
			return startingLocation;
		}
		set
		{
			startingLocation = value;
		}
	}

	public bool CloseAfterRunning
	{
		get
		{
			return closeAfterRunning;
		}
		set
		{
			closeAfterRunning = value;
		}
	}

	public string SerialNumber
	{
		get
		{
			return serialNumber;
		}
		set
		{
			serialNumber = value;
		}
	}

	public bool RunTree
	{
		get
		{
			return runTree;
		}
		set
		{
			runTree = value;
		}
	}

	public bool ShowSplashScreen
	{
		get
		{
			return showSplashScreen;
		}
		set
		{
			showSplashScreen = value;
		}
	}

	public string DebugArg
	{
		get
		{
			return debugArg;
		}
		set
		{
			debugArg = value;
		}
	}

	public string BGColor
	{
		get
		{
			return bgColor;
		}
		set
		{
			bgColor = value;
		}
	}

	public bool HideWindow => hideWindow;

	public bool Parse(string[] args)
	{
		try
		{
			globalVariables = new Dictionary<string, string>();
			filename = "";
			serialNumber = "";
			runTree = false;
			debugArg = "";
			if (args.Length == 0)
			{
				return true;
			}
			for (int i = 0; i < args.Length; i++)
			{
				try
				{
					if (args[i].ToLower().StartsWith("/debug:"))
					{
						debugArg = args[i].Substring("/debug:".Length);
						continue;
					}
					if (args[i].ToLower().StartsWith("/help"))
					{
						return false;
					}
					if (args[i].ToLower().StartsWith("/suppressmessages"))
					{
						GlobalStatus.SuppressAllMessagesMode = true;
						continue;
					}
					if (args[i].ToLower().StartsWith("/mode:"))
					{
						string text = args[i].Substring("/mode:".Length);
						if (text.Trim().ToUpper() == "AUTO")
						{
							GlobalStatus.SuppressAllMessagesMode = true;
							GlobalStatus.AutomationMode = true;
							GlobalStatus.RegexSupportEnabled = true;
						}
						continue;
					}
					if (args[i].ToLower().StartsWith("/gv:"))
					{
						string text2 = args[i].Substring("/gv:".Length);
						string[] array = text2.Split('=');
						if (array.Length == 2)
						{
							globalVariables.Add(array[0].Trim(), array[1].Trim());
						}
						continue;
					}
					if (args[i].ToLower().StartsWith("/close"))
					{
						closeAfterRunning = true;
						continue;
					}
					if (args[i].ToLower().StartsWith("/location:"))
					{
						string text3 = args[i].Substring("/location:".Length);
						string[] array2 = text3.Split(',');
						if (array2.Length == 2)
						{
							startingLocation = new Point(int.Parse(array2[0]), int.Parse(array2[1]));
						}
						continue;
					}
					if (args[i].ToLower().StartsWith("/sn:"))
					{
						serialNumber = args[i].Substring("/sn:".Length);
						continue;
					}
					if (args[i].ToLower().StartsWith("/runtree"))
					{
						runTree = true;
						continue;
					}
					if (args[i].ToLower().StartsWith("/id:"))
					{
						id = args[i].Substring("/id:".Length);
						continue;
					}
					if (args[i].ToLower().StartsWith("/topmost"))
					{
						topMost = true;
						continue;
					}
					if (args[i].ToLower().StartsWith("/locked"))
					{
						locked = true;
						continue;
					}
					if (args[i].ToLower().StartsWith("/nosplashscreen"))
					{
						showSplashScreen = false;
						continue;
					}
					if (args[i].ToLower().StartsWith("/hide"))
					{
						hideWindow = true;
						continue;
					}
					if (args[i].ToLower().StartsWith("/maxtesttime:"))
					{
						int result = -1;
						if (int.TryParse(args[i].Substring("/maxtesttime:".Length), out result))
						{
							maxTestTime = result;
						}
						continue;
					}
					if (args[i].ToLower().StartsWith("/usage"))
					{
						return false;
					}
					if (args[i].ToLower().StartsWith("/?"))
					{
						return false;
					}
					if (args[i].ToLower().StartsWith("/help"))
					{
						return false;
					}
					if (args[i].ToLower().StartsWith("/bgcolor:"))
					{
						BGColor = args[i].Substring("/bgcolor:".Length);
					}
					else if (i == 0)
					{
						filename = args[i].Trim();
						if (!File.Exists(filename))
						{
							AppMessageDialog.Show(filename + " not found.", "QSPR - Message Center", MessageBoxButtons.OK, DialogResult.OK, MessageDialogType.Error);
							return false;
						}
					}
				}
				catch
				{
				}
			}
			return true;
		}
		catch
		{
		}
		return false;
	}

	public void Usage()
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.AppendLine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\QSPR.exe <FILENAME> /sn:<SERIAL NUMBER> /gv:<NAME>=<VALUE> /location:<X>,<Y> /topmost /locked /id:<QSPR-ID> /maxtesttime:<TIME IN MINUTES> /runtree /close /suppressMessages /bgColor:yellow ");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("<FILENAME> - Name of the test tree file to open. ");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/sn - To specify Serial Number of the unit to test.");
		stringBuilder.AppendLine("<SERIAL NUMBER> - Serial Number of the unit.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/gv: - To specify global variable name and value pair. This switch can be repeated for more gloabl variables.");
		stringBuilder.AppendLine("<NAME>=<VALUE> - Global Variable name and value pair.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/nosplashscreen - Opens QSPR without showing the splash screen.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/location: - To specify the coordinates on screen where mini QSPR should start. Default is '0,0'.");
		stringBuilder.AppendLine("<X>,<Y> - X and Y coordinates.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/runtree - Runs test tree after opening the tree.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/topmost - Keeps the mini QSPR window as top most");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/locked - Locks the mini QSPR location.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/id: - Used for specifying ID to QTM. ID is shown on the left side of Mini QSPR.");
		stringBuilder.AppendLine("<QSPR-ID> - ID for Mini QSPR.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/maxtesttime: - Used for specifying time out for a test tree. Tree is aborted on reaching max test time.");
		stringBuilder.AppendLine("<TIME IN MINUTES> - Test time in minutes.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/suppressMessages - Suppress all messages including exception, error, warning, and information messages.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/bgColor: - To specify the background color. Color name must the color name available in the System.Drawing.Color.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/hide - To hide window.  This is used for automation. ");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/mode: - To set to a particular mode.  /mode:auto will set QSPR to automation mode where all the messages and dialog boxes are suppressed.  ");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/runtree - Use this switch to run the tree.");
		stringBuilder.AppendLine();
		stringBuilder.AppendLine("/close - Use this switch if QSPR needs to be closed after running the tree. This switch should only be used in conjunction with /runtree");
		MessageBox.Show(stringBuilder.ToString(), "QSPR - Usage");
	}

	public QTMArguments()
	{
	}

	public QTMArguments(string[] args)
	{
		Parse(args);
	}
}
