using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using QC.QTMInterface;

namespace QC.QTMDotNetKernelInterface;

internal class ExternalInfo
{
	private ExternalInfo()
	{
	}

	public static List<string[]> parseCSV(string fullCSVFileName)
	{
		List<string[]> list = new List<string[]>();
		try
		{
			using StreamReader streamReader = new StreamReader(fullCSVFileName);
			string text;
			while ((text = streamReader.ReadLine()) != null)
			{
				string[] item = text.Split(',');
				list.Add(item);
			}
			return list;
		}
		catch (Exception ex)
		{
			throw new Exception("parseCSV failed, " + ex.Message);
		}
	}

	private static string GetLimit(string limit)
	{
		if (double.TryParse(limit, out var _))
		{
			return limit;
		}
		throw new Exception("Limit " + limit + " is invalid, ");
	}

	private static ArrayList GetParameterList(string testName, string userDefinedTestName, List<string[]> parsedData)
	{
		try
		{
			ArrayList arrayList = new ArrayList();
			foreach (string[] parsedDatum in parsedData)
			{
				if (parsedDatum[0].ToString().Trim() == testName.Trim() && parsedDatum[1].ToString().Trim() == userDefinedTestName.Trim())
				{
					ParamValue paramValue = default(ParamValue);
					ArrayList arrayList2 = new ArrayList();
					arrayList2.Add(GetLimit(parsedDatum[3].ToString().Trim()));
					paramValue.LL = (ArrayList)arrayList2.Clone();
					arrayList2.Clear();
					arrayList2.Add(GetLimit(parsedDatum[4].ToString().Trim()));
					paramValue.UL = (ArrayList)arrayList2.Clone();
					paramValue.ParamName = parsedDatum[2].ToString().Trim();
					arrayList.Add(paramValue);
				}
			}
			return arrayList;
		}
		catch (Exception ex)
		{
			throw new Exception("GetParameterList failed, " + ex.Message);
		}
	}

	public static MsgData GenerateMsgData(string testName, string userDefinedTestName, List<string[]> parsedData)
	{
		try
		{
			MsgData result = default(MsgData);
			result.TestFullPath = testName;
			result.ParamResult = GetParameterList(testName, userDefinedTestName, parsedData);
			return result;
		}
		catch (Exception ex)
		{
			throw new Exception("GenerateMsgData for " + testName + " failed, " + ex.Message);
		}
	}
}
