using System;
using System.Collections;
using System.Collections.Generic;

namespace QC.QSPR.Text.CommandLineParser;

public class CommandLineParser
{
	private Dictionary<string, object> commandMap;

	public object this[string param]
	{
		get
		{
			if (commandMap != null)
			{
				return commandMap[param];
			}
			return null;
		}
	}

	public Dictionary<string, object> ParseCommandLineArgument(string commandLine)
	{
		try
		{
			if (!commandLine.StartsWith(" "))
			{
				commandLine = " " + commandLine;
			}
			string[] separator = new string[1] { " -" };
			char[] separator2 = new char[1] { ' ' };
			commandMap = new Dictionary<string, object>();
			string[] array = commandLine.Split(separator, StringSplitOptions.None);
			for (int i = 1; i < array.Length; i++)
			{
				string[] array2 = array[i].Split(separator2, StringSplitOptions.None);
				string key = "";
				string text = "";
				for (int j = 0; j < array2.Length; j++)
				{
					if (j == 0)
					{
						key = array2[j].Trim().ToLower();
						continue;
					}
					if (text.Length > 0)
					{
						text += " ";
					}
					text += array2[j].Trim();
				}
				if (text.Length <= 0)
				{
					text = "True";
				}
				if (commandMap.ContainsKey(key))
				{
					ArrayList arrayList;
					if (commandMap[key] is string)
					{
						arrayList = new ArrayList();
						string value = (string)commandMap[key];
						arrayList.Add(value);
					}
					else
					{
						arrayList = (ArrayList)commandMap[key];
					}
					arrayList.Add(text);
					commandMap.Remove(key);
					commandMap.Add(key, arrayList);
				}
				else
				{
					commandMap.Add(key, text);
				}
			}
			return commandMap;
		}
		catch (Exception ex)
		{
			Console.WriteLine("Parser.Parser.ParseCommandLineArgument(string) causing exception");
			throw ex;
		}
	}

	public Dictionary<string, object> ParseCommandLineArgument(string[] command)
	{
		try
		{
			string commandLine = StringUtils.ConvertTostring(command);
			Dictionary<string, object> dictionary = new Dictionary<string, object>();
			return ParseCommandLineArgument(commandLine);
		}
		catch (Exception ex)
		{
			Console.WriteLine("Parser.Parser.ParseCommandLineArgument(string []) causing exception");
			throw ex;
		}
	}

	public string GetValueAsString(string keyword)
	{
		string result = "";
		if (commandMap.ContainsKey(keyword) && commandMap[keyword] is string)
		{
			result = Convert.ToString(commandMap[keyword]);
		}
		return result;
	}
}
