using System;
using System.Collections.Generic;
using System.Linq;
using QC.CTDS.DM.DataStructures.FormatDescriptorStructure;
using QC.CTDS.DM.DataStructures.InputDataStructure;
using QC.CTDS.DM.DataStructures.LimitDescriptorDataStructure;
using QC.CTDS.DM.DataStructures.OutputDataStructure;
using QC.CTDS.DM.SharedLibrary;

namespace QC.CTDS.DM.DataProcessor;

public static class DataProcessorUtils
{
	internal static List<TestRunInfo> GetTestFilteredSourceData(IEnumerable<ResultDataSet> Results, InputData inputData)
	{
		List<Tuple<string, string>> list = new List<Tuple<string, string>>();
		IEnumerable<TestRunInfo> testRuns = inputData.TestInfoDict.Select((KeyValuePair<string, TestInfo> keyPair) => keyPair.Value).SelectMany((TestInfo testInfo) => testInfo.TestRunsList);
		foreach (ResultDataSet Result in Results)
		{
			Tuple<string, string> item = new Tuple<string, string>(Result.testID, Result.resID);
			list.Add(item);
		}
		return GetTestParamFilteredSourceData(list, testRuns);
	}

	internal static List<TestRunInfo> GetTestParamFilteredSourceData(List<Tuple<string, string>> testParamsList, IEnumerable<TestRunInfo> testRuns)
	{
		List<TestRunInfo> list = new List<TestRunInfo>();
		IEnumerable<TestRunInfo> enumerable = null;
		foreach (Tuple<string, string> testParamPair in testParamsList)
		{
			enumerable = ((!Constants.AllTestsString.Contains(testParamPair.Item1)) ? testRuns.Where((TestRunInfo runinfo) => runinfo.TestName == testParamPair.Item1 && runinfo.ContainsParam(testParamPair.Item2)) : testRuns.Where((TestRunInfo runinfo) => runinfo.ContainsParam(testParamPair.Item2)));
			list.AddRange(enumerable.Except(list));
		}
		return list;
	}

	internal static List<DiscGroup> GetFilteredSourceData(DiscriminatorElement disElement, List<TestRunInfo> testRuns, bool sort = false, bool sortAscending = true)
	{
		Dictionary<string, List<TestRunInfo>> dictionary = new Dictionary<string, List<TestRunInfo>>();
		List<DiscGroup> list = new List<DiscGroup>();
		List<DescriptorItem> list2 = new List<DescriptorItem>();
		if (disElement != null && (disElement.Discriminator1.Count > 0 || disElement.Filter.Count > 0))
		{
			list2.AddRange(disElement.GetDescriptorItems());
			for (int i = 0; i < testRuns.Count; i++)
			{
				bool flag = false;
				if (disElement.Discriminator1.Count > 0)
				{
					for (int j = 0; j < disElement.Discriminator1.Count; j++)
					{
						flag = flag || isMatchData(disElement.Discriminator1[j], testRuns[i]);
					}
				}
				if (disElement.Filter.Count > 0)
				{
					for (int k = 0; k < disElement.Filter.Count; k++)
					{
						flag = flag || isMatchData(disElement.Filter[k], testRuns[i]);
					}
				}
				if (flag)
				{
					string grpName = null;
					string groupName = GetGroupName(testRuns[i], list2, ref grpName);
					if (!dictionary.ContainsKey(groupName))
					{
						List<TestRunInfo> list3 = new List<TestRunInfo>();
						list3.Add(testRuns[i]);
						dictionary.Add(groupName, list3);
						DiscGroup discGroup = new DiscGroup();
						discGroup.GrpNameWithInvisibleDisc = groupName;
						discGroup.GrpName = grpName;
						discGroup.ParamsDict = GetParamItems(testRuns[i], list2);
						discGroup.ParamVisiblityInfo = GetParamVisibilityInfo(testRuns[i], list2);
						DiscGroup item = discGroup;
						list.Add(item);
					}
					else
					{
						dictionary[groupName].Add(testRuns[i]);
					}
				}
			}
			if (sort)
			{
				list = SortDiscrimnatorGroups(list, sortAscending, disElement.GetDescriptorItems());
			}
			foreach (DiscGroup item2 in list)
			{
				item2.TestItems = dictionary[item2.GrpNameWithInvisibleDisc];
			}
		}
		else
		{
			list.Add(new DiscGroup
			{
				GrpName = "",
				TestItems = testRuns
			});
		}
		return list;
	}

	private static List<DiscGroup> SortDiscrimnatorGroups(List<DiscGroup> disGrpList, bool isAscendingSort, IEnumerable<DescriptorItem> descItems)
	{
		List<DiscGroup> list = new List<DiscGroup>();
		if (disGrpList.Count > 0)
		{
			foreach (DiscGroup disGrp in disGrpList)
			{
				KeyValuePair<string, ParamInfo>[] array = disGrp.ParamsDict.ToArray();
				for (int i = 0; i < array.Length; i++)
				{
					KeyValuePair<string, ParamInfo> keyValuePair = array[i];
					if (keyValuePair.Value == null)
					{
						ParamInfo paramInfo = new ParamInfo();
						paramInfo.StrVal = "";
						ParamInfo value = paramInfo;
						disGrp.ParamsDict[keyValuePair.Key] = value;
					}
				}
			}
			IOrderedEnumerable<DiscGroup> orderedEnumerable = null;
			foreach (DescriptorItem descItem in descItems)
			{
				if (string.IsNullOrEmpty(descItem.Id) || !disGrpList.TrueForAll((DiscGroup disGrp) => disGrp.ParamsDict.ContainsKey(descItem.Id)))
				{
					continue;
				}
				bool flag = disGrpList.TrueForAll((DiscGroup disGrp) => !string.IsNullOrEmpty(disGrp.ParamsDict[descItem.Id].StrVal));
				bool flag2 = disGrpList.TrueForAll(delegate(DiscGroup disGrp)
				{
					if (!double.IsNaN(disGrp.ParamsDict[descItem.Id].Val))
					{
						_ = disGrp.ParamsDict[descItem.Id].Val;
						return true;
					}
					return false;
				});
				if (flag)
				{
					if (disGrpList.Select((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].StrVal).Distinct().Count() != 1)
					{
						orderedEnumerable = ((orderedEnumerable != null) ? ((!isAscendingSort) ? orderedEnumerable.ThenByDescending((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].StrVal) : orderedEnumerable.ThenBy((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].StrVal)) : ((!isAscendingSort) ? disGrpList.OrderByDescending((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].StrVal) : disGrpList.OrderBy((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].StrVal)));
					}
				}
				else if (flag2)
				{
					if (disGrpList.Select((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].Val).Distinct().Count() != 1)
					{
						orderedEnumerable = ((orderedEnumerable != null) ? ((!isAscendingSort) ? orderedEnumerable.ThenByDescending((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].Val) : orderedEnumerable.ThenBy((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].Val)) : ((!isAscendingSort) ? disGrpList.OrderByDescending((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].Val) : disGrpList.OrderBy((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].Val)));
					}
				}
				else if (disGrpList.Select((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].Val).Distinct().Count() != 1)
				{
					orderedEnumerable = ((orderedEnumerable != null) ? ((!isAscendingSort) ? orderedEnumerable.ThenByDescending((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].StrVal).ThenByDescending((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].Val) : orderedEnumerable.ThenBy((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].StrVal).ThenBy((DiscGroup disGrp) => disGrp.ParamsDict[descItem.Id].Val)) : ((!isAscendingSort) ? (from disGrp in disGrpList
						orderby disGrp.ParamsDict[descItem.Id].StrVal descending, disGrp.ParamsDict[descItem.Id].Val descending
						select disGrp) : (from disGrp in disGrpList
						orderby disGrp.ParamsDict[descItem.Id].StrVal, disGrp.ParamsDict[descItem.Id].Val
						select disGrp)));
				}
			}
			if (orderedEnumerable == null)
			{
				list.AddRange(disGrpList);
			}
			else
			{
				list = orderedEnumerable.ToList();
			}
		}
		return list;
	}

	private static Dictionary<string, ParamInfo> GetParamItems(TestRunInfo testRunInfo, List<DescriptorItem> matchingParams)
	{
		Dictionary<string, ParamInfo> paramsDict = new Dictionary<string, ParamInfo>();
		matchingParams.ForEach(delegate(DescriptorItem mParam)
		{
			paramsDict[mParam.Id] = testRunInfo.GetParamValue(mParam.Id);
		});
		return paramsDict;
	}

	private static Dictionary<string, bool> GetParamVisibilityInfo(TestRunInfo testRunInfo, List<DescriptorItem> matchingParams)
	{
		Dictionary<string, bool> paramsDict = new Dictionary<string, bool>();
		matchingParams.ForEach(delegate(DescriptorItem mParam)
		{
			paramsDict[mParam.Id] = mParam.IsVisible;
		});
		foreach (DescriptorItem matchingParam in matchingParams)
		{
			ParamInfo paramValue = testRunInfo.GetParamValue(matchingParam.Id);
			if (paramValue != null)
			{
				string text = (string.IsNullOrEmpty(paramValue.StrVal) ? paramValue.Val.ToString() : paramValue.StrVal);
				if (matchingParam.Value == text)
				{
					paramsDict[matchingParam.Id] = matchingParam.IsVisible;
				}
			}
		}
		return paramsDict;
	}

	internal static string GetGroupName(TestRunInfo testRunInfo, List<DescriptorItem> matchingParams, ref string grpName)
	{
		string[] array = new string[matchingParams.Count];
		string[] array2 = new string[matchingParams.Count];
		for (int i = 0; i < matchingParams.Count; i++)
		{
			ParamInfo paramValue = testRunInfo.GetParamValue(matchingParams[i].Id);
			if (paramValue == null)
			{
				continue;
			}
			if (double.IsNaN(paramValue.Val))
			{
				if (matchingParams[i].IsFilter)
				{
					array[i] = matchingParams[i].Id + matchingParams[i].Value;
				}
				else
				{
					array[i] = matchingParams[i].Id + "=" + paramValue.StrVal;
				}
				if (!matchingParams[i].IsVisible)
				{
					continue;
				}
				if (!string.IsNullOrEmpty(matchingParams[i].Name))
				{
					if (matchingParams[i].IsFilter)
					{
						array2[i] = matchingParams[i].Name + matchingParams[i].Value;
					}
					else
					{
						array2[i] = matchingParams[i].Name + "=" + paramValue.StrVal;
					}
				}
				else if (matchingParams[i].IsFilter)
				{
					array2[i] = matchingParams[i].Id + matchingParams[i].Value;
				}
				else
				{
					array2[i] = matchingParams[i].Id + "=" + paramValue.StrVal;
				}
				continue;
			}
			if (matchingParams[i].IsFilter)
			{
				array[i] = matchingParams[i].Id + matchingParams[i].Value;
			}
			else
			{
				array[i] = matchingParams[i].Id + "=" + paramValue.Val;
			}
			if (!matchingParams[i].IsVisible)
			{
				continue;
			}
			if (!string.IsNullOrEmpty(matchingParams[i].Name))
			{
				if (matchingParams[i].IsFilter)
				{
					array2[i] = matchingParams[i].Name + matchingParams[i].Value;
				}
				else
				{
					array2[i] = matchingParams[i].Name + "=" + paramValue.Val;
				}
			}
			else if (matchingParams[i].IsFilter)
			{
				array2[i] = matchingParams[i].Id + matchingParams[i].Value;
			}
			else
			{
				array2[i] = matchingParams[i].Id + "=" + paramValue.Val;
			}
		}
		grpName = string.Join(" : ", array2.Where((string str) => !string.IsNullOrEmpty(str)).Distinct());
		return string.Join(" : ", array.Where((string str) => !string.IsNullOrEmpty(str)).Distinct());
	}

	internal static string GetVisibleGroupName(ParamInfo pInfo, DescriptorItem descItem, bool addUnitName = false)
	{
		string result = string.Empty;
		string empty = string.Empty;
		empty = (string.IsNullOrEmpty(descItem.Name) ? descItem.Id : descItem.Name);
		if (addUnitName && !string.IsNullOrEmpty(pInfo.ParamUnit))
		{
			empty = empty + "(" + pInfo.ParamUnit + ")";
		}
		if (double.IsNaN(pInfo.Val))
		{
			if (descItem.IsVisible)
			{
				result = ((!descItem.IsFilter) ? (empty + "=" + pInfo.StrVal) : (empty + descItem.Value));
			}
		}
		else if (descItem.IsVisible)
		{
			result = ((!descItem.IsFilter) ? (empty + "=" + pInfo.Val) : (empty + descItem.Value));
		}
		return result;
	}

	internal static bool isMatchData(Discriminator discriminator, TestRunInfo runInfo)
	{
		bool flag = true;
		if (discriminator != null)
		{
			if (discriminator.exclude)
			{
				flag = !runInfo.ContainsParam(discriminator.id);
				if (flag)
				{
					if (discriminator.Discriminator1.Count > 0)
					{
						bool flag2 = false;
						for (int i = 0; i < discriminator.Discriminator1.Count; i++)
						{
							flag2 = flag2 || isMatchData(discriminator.Discriminator1[i], runInfo);
						}
						flag = flag && flag2;
					}
					if (discriminator.Filter.Count > 0)
					{
						bool flag3 = false;
						for (int j = 0; j < discriminator.Filter.Count; j++)
						{
							flag3 = flag3 || isMatchData(discriminator.Filter[j], runInfo);
						}
						flag = flag && flag3;
					}
				}
			}
			else if (runInfo.ContainsParam(discriminator.id) || discriminator.id == "EMPTY_DISC")
			{
				if (!string.IsNullOrEmpty(discriminator.disValue))
				{
					int num = 0;
					double result = double.NaN;
					string text = "=";
					string disValue = discriminator.disValue;
					if (disValue.Length >= 3)
					{
						string text2 = disValue.Substring(0, 3);
						if (text2 == "+/-")
						{
							text = "+/-";
							num = 3;
						}
					}
					if (num == 0 && disValue.Length >= 2)
					{
						string text2 = disValue.Substring(0, 2);
						switch (text2)
						{
						case "<=":
						case ">=":
							text = text2;
							num = 2;
							break;
						case "!=":
							text = "<>";
							num = 2;
							break;
						}
					}
					if (num == 0)
					{
						string text2 = disValue.Substring(0, 1);
						switch (text2)
						{
						case "=":
						case ">":
						case "<":
							text = text2;
							num = 1;
							break;
						}
					}
					disValue = disValue.Substring(num).Trim();
					if (double.TryParse(disValue, out result))
					{
						if (text == "=" && result != runInfo.GetParamValue(discriminator.id).Val)
						{
							flag = false;
						}
						else if (text == "<>" && result == runInfo.GetParamValue(discriminator.id).Val)
						{
							flag = false;
						}
						else if (text == ">" && !(runInfo.GetParamValue(discriminator.id).Val > result))
						{
							flag = false;
						}
						else if (text == "<" && !(runInfo.GetParamValue(discriminator.id).Val < result))
						{
							flag = false;
						}
						else if (text == ">=" && !(runInfo.GetParamValue(discriminator.id).Val >= result))
						{
							flag = false;
						}
						else if (text == "<=" && !(runInfo.GetParamValue(discriminator.id).Val <= result))
						{
							flag = false;
						}
						else if (text == "+/-" && Math.Abs(runInfo.GetParamValue(discriminator.id).Val) != result)
						{
							flag = false;
						}
					}
					else if (text == "=" && !(disValue == runInfo.GetParamValue(discriminator.id).StrVal))
					{
						flag = false;
					}
					else if (text == "<>" && !(disValue != runInfo.GetParamValue(discriminator.id).StrVal))
					{
						flag = false;
					}
				}
				if (flag)
				{
					if (discriminator.Discriminator1.Count > 0)
					{
						bool flag4 = false;
						for (int k = 0; k < discriminator.Discriminator1.Count; k++)
						{
							flag4 = flag4 || isMatchData(discriminator.Discriminator1[k], runInfo);
						}
						flag = flag && flag4;
					}
					if (discriminator.Filter.Count > 0)
					{
						bool flag5 = false;
						for (int l = 0; l < discriminator.Filter.Count; l++)
						{
							flag5 = flag5 || isMatchData(discriminator.Filter[l], runInfo);
						}
						flag = flag && flag5;
					}
				}
			}
			else if (discriminator.Optional)
			{
				flag = true;
				if (discriminator.Discriminator1.Count > 0)
				{
					bool flag6 = false;
					for (int m = 0; m < discriminator.Discriminator1.Count; m++)
					{
						flag6 = flag6 || isMatchData(discriminator.Discriminator1[m], runInfo);
					}
					flag = flag && flag6;
				}
				if (discriminator.Filter.Count > 0)
				{
					bool flag7 = false;
					for (int n = 0; n < discriminator.Filter.Count; n++)
					{
						flag7 = flag7 || isMatchData(discriminator.Filter[n], runInfo);
					}
					flag = flag && flag7;
				}
			}
			else
			{
				flag = false;
			}
		}
		return flag;
	}

	internal static List<TestRunInfo> GetTestFilteredSourceData(IEnumerable<ResultDataSet> Results, List<TestRunInfo> testRuns)
	{
		List<Tuple<string, string>> list = new List<Tuple<string, string>>();
		foreach (ResultDataSet Result in Results)
		{
			Tuple<string, string> item = new Tuple<string, string>(Result.testID, Result.resID);
			list.Add(item);
		}
		return GetTestParamFilteredSourceData(list, testRuns);
	}

	internal static List<TestRunInfo> GetTestFilteredSourceData(IEnumerable<OutputGroupLimitDescriptorResult> Results, List<TestRunInfo> testRuns)
	{
		List<Tuple<string, string>> list = new List<Tuple<string, string>>();
		foreach (OutputGroupLimitDescriptorResult Result in Results)
		{
			Tuple<string, string> item = new Tuple<string, string>(Result.testID, Result.resID);
			list.Add(item);
		}
		return GetTestParamFilteredSourceData(list, testRuns);
	}

	internal static List<TestRunInfo> GetTestFilteredSourceData(IEnumerable<OutputGroupLimitDescriptorResult> Results, InputData inputData)
	{
		IEnumerable<TestRunInfo> testRuns = inputData.TestInfoDict.Select((KeyValuePair<string, TestInfo> keyPair) => keyPair.Value).SelectMany((TestInfo testInfo) => testInfo.TestRunsList);
		List<Tuple<string, string>> list = new List<Tuple<string, string>>();
		foreach (OutputGroupLimitDescriptorResult Result in Results)
		{
			Tuple<string, string> item = new Tuple<string, string>(Result.testID, Result.resID);
			list.Add(item);
		}
		return GetTestParamFilteredSourceData(list, testRuns);
	}

	internal static List<TestRunInfo> GetTestFilteredSourceData(IEnumerable<ScatteredChartSeries> scatteredSeries, List<TestRunInfo> testRuns)
	{
		List<Tuple<string, string>> list = new List<Tuple<string, string>>();
		foreach (ScatteredChartSeries item2 in scatteredSeries)
		{
			if (item2.DepVar != null)
			{
				Tuple<string, string> item = new Tuple<string, string>(item2.testID, item2.DepVar.IdVariable);
				list.Add(item);
			}
		}
		return GetTestParamFilteredSourceData(list, testRuns);
	}

	internal static List<DescriptorItem> RemoveDuplicateDescriptorItems(List<DescriptorItem> descItems)
	{
		List<DescriptorItem> list = new List<DescriptorItem>();
		List<string> list2 = new List<string>();
		for (int i = 0; i < descItems.Count; i++)
		{
			if (!list2.Contains(descItems[i].Id))
			{
				list.Add(descItems[i]);
				list2.Add(descItems[i].Id);
			}
		}
		return list;
	}
}
