using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using QC.CTDS.DM.DataStructures;
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;

internal class LineDescriptorProcessor : IDescriptorProcessor
{
	private LineOutputGroupDescriptor fdLineGroupDescriptor;

	internal ReportSettings reportSettings;

	internal List<TestRunInfo> testRuns;

	internal LineDescriptorProcessor()
	{
	}

	public OutputGroup ProcessDescriptorData(ReportSettings settings, GroupSetDescriptor groupSetDesc, List<TestRunInfo> testRuns)
	{
		reportSettings = settings;
		fdLineGroupDescriptor = groupSetDesc as LineOutputGroupDescriptor;
		this.testRuns = testRuns;
		if (reportSettings.FailureAnalysis)
		{
			return null;
		}
		if (fdLineGroupDescriptor == null)
		{
			throw new Exception("Unsopported descriptor type");
		}
		OutputGroup outputGroup = new OutputGroup();
		outputGroup.Name = fdLineGroupDescriptor.Name;
		if (fdLineGroupDescriptor.OGDisplayProperties != null && fdLineGroupDescriptor.OGDisplayProperties.Count > 0)
		{
			outputGroup.DisplayProp = fdLineGroupDescriptor.OGDisplayProperties[0];
		}
		testRuns = DataProcessorUtils.GetTestFilteredSourceData(from res in fdLineGroupDescriptor.GetResultSets()
			where !string.IsNullOrEmpty(res.testID) && !string.IsNullOrEmpty(res.resID)
			select res, testRuns);
		if (testRuns.Count > 0)
		{
			if (fdLineGroupDescriptor.Discriminator1.Count > 0 || fdLineGroupDescriptor.Filter.Count > 0)
			{
				List<DiscGroup> chartGroupedItems = DataProcessorUtils.GetFilteredSourceData(fdLineGroupDescriptor, testRuns, fdLineGroupDescriptor.sort);
				if (chartGroupedItems.Count <= 0)
				{
					return outputGroup;
				}
				LinearData[] linearDataArray = new LinearData[chartGroupedItems.Count];
				CommonFunctions.RunLoop(reportSettings.RunInSequentialMode, 0, chartGroupedItems.Count, delegate(int index)
				{
					Stopwatch stopwatch = new Stopwatch();
					stopwatch.Start();
					LinearData linearData2 = new LinearData
					{
						Name = fdLineGroupDescriptor.Name + " : " + chartGroupedItems[index].GrpName
					};
					foreach (LineOutputCommonBinResults commonBinResult in fdLineGroupDescriptor.CommonBinResults)
					{
						if (commonBinResult.Discriminator1.Count > 0 || commonBinResult.Filter.Count > 0)
						{
							List<DiscGroup> filteredSourceData2 = DataProcessorUtils.GetFilteredSourceData(commonBinResult, chartGroupedItems[index].TestItems, commonBinResult.sort);
							if (filteredSourceData2.Count > 0)
							{
								foreach (DiscGroup item in filteredSourceData2)
								{
									string xAxisTitle3 = string.Empty;
									string primaryYAxisTitle3 = string.Empty;
									string secondaryYAxisTitle3 = string.Empty;
									List<LinearSeriesGroup> list3 = ProcessResultDataSet(commonBinResult, item.TestItems, item.GrpName, linearData2.XAxisValues, ref xAxisTitle3, ref primaryYAxisTitle3, ref secondaryYAxisTitle3);
									if (list3.Count > 0)
									{
										linearData2.XAxisTitle = xAxisTitle3;
										linearData2.PrimaryYAxisTitle = primaryYAxisTitle3;
										linearData2.SecondaryYAxisTitle = secondaryYAxisTitle3;
										linearData2.SeriesGroupList.AddRange(list3);
									}
								}
							}
						}
						else
						{
							string xAxisTitle4 = string.Empty;
							string primaryYAxisTitle4 = string.Empty;
							string secondaryYAxisTitle4 = string.Empty;
							List<LinearSeriesGroup> list4 = ProcessResultDataSet(commonBinResult, chartGroupedItems[index].TestItems, "", linearData2.XAxisValues, ref xAxisTitle4, ref primaryYAxisTitle4, ref secondaryYAxisTitle4);
							if (list4.Count > 0)
							{
								linearData2.XAxisTitle = xAxisTitle4;
								linearData2.PrimaryYAxisTitle = primaryYAxisTitle4;
								linearData2.SecondaryYAxisTitle = secondaryYAxisTitle4;
								linearData2.SeriesGroupList.AddRange(list4);
							}
						}
					}
					linearData2.UpdateSeriesColumns();
					linearData2.CalculateAutoLimits();
					linearDataArray[index] = linearData2;
				});
				for (int i = 0; i < linearDataArray.Length; i++)
				{
					if (linearDataArray[i] != null && linearDataArray[i].SeriesGroupList.Count > 0)
					{
						outputGroup.OutputDataList.Add(linearDataArray[i]);
					}
				}
			}
			else
			{
				LinearData linearData = new LinearData();
				linearData.Name = fdLineGroupDescriptor.Name;
				foreach (LineOutputCommonBinResults commonBinResult2 in fdLineGroupDescriptor.CommonBinResults)
				{
					if (commonBinResult2.Discriminator1.Count > 0 || commonBinResult2.Filter.Count > 0)
					{
						List<DiscGroup> filteredSourceData = DataProcessorUtils.GetFilteredSourceData(commonBinResult2, testRuns, commonBinResult2.sort);
						if (filteredSourceData.Count <= 0)
						{
							continue;
						}
						foreach (DiscGroup item2 in filteredSourceData)
						{
							string xAxisTitle = string.Empty;
							string primaryYAxisTitle = string.Empty;
							string secondaryYAxisTitle = string.Empty;
							List<LinearSeriesGroup> list = ProcessResultDataSet(commonBinResult2, item2.TestItems, item2.GrpName, linearData.XAxisValues, ref xAxisTitle, ref primaryYAxisTitle, ref secondaryYAxisTitle);
							if (list.Count > 0)
							{
								linearData.XAxisTitle = xAxisTitle;
								linearData.PrimaryYAxisTitle = primaryYAxisTitle;
								linearData.SecondaryYAxisTitle = secondaryYAxisTitle;
								linearData.SeriesGroupList.AddRange(list);
							}
						}
					}
					else
					{
						string xAxisTitle2 = string.Empty;
						string primaryYAxisTitle2 = string.Empty;
						string secondaryYAxisTitle2 = string.Empty;
						List<LinearSeriesGroup> list2 = ProcessResultDataSet(commonBinResult2, testRuns, "", linearData.XAxisValues, ref xAxisTitle2, ref primaryYAxisTitle2, ref secondaryYAxisTitle2);
						if (list2.Count > 0)
						{
							linearData.XAxisTitle = xAxisTitle2;
							linearData.PrimaryYAxisTitle = primaryYAxisTitle2;
							linearData.SecondaryYAxisTitle = secondaryYAxisTitle2;
							linearData.SeriesGroupList.AddRange(list2);
						}
					}
				}
				linearData.UpdateSeriesColumns();
				linearData.CalculateAutoLimits();
				if (linearData.SeriesGroupList.Count > 0)
				{
					outputGroup.OutputDataList.Add(linearData);
				}
			}
		}
		return outputGroup;
	}

	private List<LinearSeriesGroup> ProcessResultDataSet(LineOutputCommonBinResults binResult, List<TestRunInfo> testRuns, string combinedSeriesGroupString, List<string> xAxisValues, ref string xAxisTitle, ref string primaryYAxisTitle, ref string secondaryYAxisTitle)
	{
		List<LinearSeriesGroup> list = new List<LinearSeriesGroup>();
		List<string> list2 = new List<string>();
		List<string> list3 = new List<string>();
		foreach (ResultDataSet resultSet in binResult.Result.Where((ResultDataSet res) => !string.IsNullOrEmpty(res.resID) && !string.IsNullOrEmpty(res.testID)))
		{
			List<TestRunInfo> testFilteredSourceData = DataProcessorUtils.GetTestFilteredSourceData(new ResultDataSet[1] { resultSet }, testRuns);
			List<TestRunInfo> source = testFilteredSourceData;
			Func<TestRunInfo, bool> predicate = (TestRunInfo runinfo) => runinfo.ContainsParam(resultSet.resID);
			testFilteredSourceData = source.Where(predicate).ToList();
			if ((reportSettings.AllFailureAnalysis && !testFilteredSourceData.Any((TestRunInfo runInfo) => runInfo.HasParamFailed(resultSet.resID))) || testFilteredSourceData.Count <= 0)
			{
				continue;
			}
			LinearSeriesGroup seriesGroup = new LinearSeriesGroup();
			seriesGroup.NumberFormat = resultSet.format;
			seriesGroup.ChartSeriesType = resultSet.ChartSeriesType;
			string text = resultSet.resID;
			TestRunInfo testRunInfo = testFilteredSourceData.Find((TestRunInfo runinfo) => runinfo.ContainsParam(resultSet.resID));
			string paramUnit = testRunInfo.GetParamValue(resultSet.resID).ParamUnit;
			if (resultSet.axisIndex == 2)
			{
				seriesGroup.IsSecondarySeries = true;
				if (!string.IsNullOrEmpty(paramUnit))
				{
					list3.Add(resultSet.resID + "(" + paramUnit + ")");
				}
				else
				{
					list3.Add(resultSet.resID);
				}
			}
			else if (!string.IsNullOrEmpty(paramUnit))
			{
				list2.Add(resultSet.resID + "(" + paramUnit + ")");
			}
			else
			{
				list2.Add(resultSet.resID);
			}
			if (!string.IsNullOrEmpty(resultSet.name))
			{
				text = resultSet.name;
			}
			seriesGroup.ParameterName = text;
			if (!string.IsNullOrEmpty(resultSet.GroupName))
			{
				text = text + "_" + resultSet.GroupName;
			}
			if (string.IsNullOrEmpty(combinedSeriesGroupString))
			{
				seriesGroup.SeriesName = text;
			}
			else if (!string.IsNullOrEmpty(resultSet.name))
			{
				seriesGroup.SeriesName = text + "_" + combinedSeriesGroupString;
			}
			else
			{
				seriesGroup.SeriesName = text + "_" + combinedSeriesGroupString;
			}
			if (!string.IsNullOrEmpty(paramUnit))
			{
				LinearSeriesGroup linearSeriesGroup = seriesGroup;
				linearSeriesGroup.SeriesName = linearSeriesGroup.SeriesName + "(" + paramUnit + ")";
			}
			if (resultSet.Discriminator1.Count <= 0 && resultSet.Filter.Count <= 0)
			{
				continue;
			}
			List<DiscGroup> filteredSourceData = DataProcessorUtils.GetFilteredSourceData(resultSet, testFilteredSourceData, resultSet.sort);
			if (filteredSourceData.Count <= 0)
			{
				continue;
			}
			List<DescriptorItem> descItems = new List<DescriptorItem>(resultSet.GetDescriptorItems());
			descItems = DataProcessorUtils.RemoveDuplicateDescriptorItems(descItems);
			seriesGroup.SeriesTbl = new DataTable();
			TestRunInfo runInfo2 = filteredSourceData[0].TestItems.Find((TestRunInfo s) => s.ContainsParam(resultSet.resID));
			xAxisTitle = GetXAxisConditionName(descItems, runInfo2);
			seriesGroup.SeriesTbl.Columns.Add(xAxisTitle);
			DataRow dataRow = null;
			DataRow dataRow2 = null;
			dataRow = seriesGroup.SeriesTbl.NewRow();
			dataRow[0] = "Min Lim";
			seriesGroup.SeriesTbl.Rows.Add(dataRow);
			dataRow2 = seriesGroup.SeriesTbl.NewRow();
			dataRow2[0] = "Max Lim";
			seriesGroup.SeriesTbl.Rows.Add(dataRow2);
			if (xAxisValues.Count > 0)
			{
				for (int i = 0; i < xAxisValues.Count; i++)
				{
					seriesGroup.SeriesTbl.Columns.Add(xAxisValues[i]);
				}
			}
			List<DataRow> customLimitRows = new List<DataRow>();
			if (reportSettings.CustomLimitTypes != null && (reportSettings.ShowCustomLimits || reportSettings.AutoLimitDescriptor))
			{
				for (int j = 0; j < reportSettings.CustomLimitTypes.Count; j++)
				{
					DataRow dataRow3 = null;
					DataRow dataRow4 = null;
					dataRow3 = seriesGroup.SeriesTbl.NewRow();
					dataRow3[0] = "Min Lim " + reportSettings.CustomLimitTypes[j].Name;
					seriesGroup.SeriesTbl.Rows.Add(dataRow3);
					customLimitRows.Add(dataRow3);
					dataRow4 = seriesGroup.SeriesTbl.NewRow();
					dataRow4[0] = "Max Lim " + reportSettings.CustomLimitTypes[j].Name;
					seriesGroup.SeriesTbl.Rows.Add(dataRow4);
					customLimitRows.Add(dataRow4);
				}
			}
			SortedDictionary<long, List<DataRow>> sortedDictionary = new SortedDictionary<long, List<DataRow>>();
			List<Tuple<DataRow, string, string>> list4 = new List<Tuple<DataRow, string, string>>();
			foreach (DiscGroup item in filteredSourceData)
			{
				double min = double.NaN;
				double max = double.NaN;
				foreach (TestRunInfo testItem in item.TestItems)
				{
					ParamInfo paramValue = testItem.GetParamValue(resultSet.resID);
					OutputParamInfo outputParamInfo = null;
					if (paramValue != null)
					{
						outputParamInfo = ((!(paramValue is OutputParamInfo)) ? new OutputParamInfo(paramValue) : (paramValue as OutputParamInfo));
					}
					if (outputParamInfo == null)
					{
						continue;
					}
					string columnName = CombineColumnValues(descItems, testItem);
					if (!string.IsNullOrEmpty(columnName))
					{
						if (columnName == xAxisTitle)
						{
							columnName += "_Vals";
						}
						if (!seriesGroup.SeriesTbl.Columns.Contains(columnName))
						{
							seriesGroup.SeriesTbl.Columns.Add(columnName);
							xAxisValues.Add(columnName);
						}
					}
					if (!double.IsNaN(outputParamInfo.MinLimit) && dataRow != null && Convert.IsDBNull(dataRow[columnName]))
					{
						seriesGroup.HasMinLimit = true;
						dataRow[columnName] = outputParamInfo.MinLimit.ToString();
						min = outputParamInfo.MinLimit;
					}
					if (!double.IsNaN(outputParamInfo.MaxLimit) && dataRow2 != null && Convert.IsDBNull(dataRow2[columnName]))
					{
						seriesGroup.HasMaxLimit = true;
						dataRow2[columnName] = outputParamInfo.MaxLimit.ToString();
						max = outputParamInfo.MaxLimit;
					}
					if (reportSettings.CustomLimitTypes != null && (reportSettings.ShowCustomLimits || reportSettings.AutoLimitDescriptor))
					{
						int cus;
						for (cus = 0; cus < customLimitRows.Count(); cus++)
						{
							if (outputParamInfo.CustomLimits == null)
							{
								continue;
							}
							string limitIndex = string.Empty;
							if (reportSettings.CustomLimitTypes != null)
							{
								LimitGroupType limitGroupType = reportSettings.CustomLimitTypes.Find((LimitGroupType limtype) => customLimitRows[cus][0].ToString().Contains(limtype.Name));
								if (limitGroupType != null)
								{
									limitIndex = limitGroupType.Index;
								}
							}
							if (outputParamInfo.CustomLimits.LowLimit != null)
							{
								Limit limit = outputParamInfo.CustomLimits.LowLimit.FirstOrDefault((Limit lim) => lim.Index == limitIndex);
								if (limit != null && limit.Val != null && customLimitRows[cus][0].ToString().Contains("Min Lim "))
								{
									customLimitRows[cus][columnName] = limit.Val.ToString();
									seriesGroup.HasCustomLimit = true;
								}
							}
							if (outputParamInfo.CustomLimits.HighLimit != null)
							{
								Limit limit2 = outputParamInfo.CustomLimits.HighLimit.FirstOrDefault((Limit lim) => lim.Index == limitIndex);
								if (limit2 != null && limit2.Val != null && customLimitRows[cus][0].ToString().Contains("Max Lim "))
								{
									customLimitRows[cus][columnName] = limit2.Val.ToString();
									seriesGroup.HasCustomLimit = true;
								}
							}
						}
					}
					string value = ((!double.IsNaN(outputParamInfo.Val)) ? outputParamInfo.Val.ToString() : outputParamInfo.StrVal);
					string empty = string.Empty;
					empty = ((!reportSettings.ShowSerialNumberInChartData) ? testItem.RunId.ToString() : (testItem.UUT_ID + "_" + testItem.RunId));
					DataRow dataRow5;
					if (sortedDictionary.ContainsKey(testItem.RunId))
					{
						dataRow5 = sortedDictionary[testItem.RunId].Find((DataRow dRow) => Convert.IsDBNull(dRow[columnName]));
						if (dataRow5 != null)
						{
							dataRow5[columnName] = value;
						}
						else
						{
							dataRow5 = seriesGroup.SeriesTbl.NewRow();
							dataRow5[columnName] = value;
							dataRow5[0] = empty;
							sortedDictionary[testItem.RunId].Add(dataRow5);
						}
					}
					else
					{
						dataRow5 = seriesGroup.SeriesTbl.NewRow();
						dataRow5[columnName] = value;
						dataRow5[0] = empty;
						List<DataRow> list5 = new List<DataRow>();
						list5.Add(dataRow5);
						sortedDictionary.Add(testItem.RunId, list5);
					}
					if (reportSettings.CustomLimitTypes != null && reportSettings.CustomLimitTypes.Count > 0 && (reportSettings.ShowCustomLimits || reportSettings.AutoLimitDescriptor) && seriesGroup.HasCustomLimit)
					{
						int custLim;
						for (custLim = 0; custLim < reportSettings.CustomLimitTypes.Count; custLim++)
						{
							double result = double.NaN;
							double result2 = double.NaN;
							if (outputParamInfo.CustomLimits != null)
							{
								if (outputParamInfo.CustomLimits.HighLimit != null && outputParamInfo.CustomLimits.HighLimit.Count > 0)
								{
									Limit limit3 = outputParamInfo.CustomLimits.HighLimit.FirstOrDefault((Limit hLim) => hLim.Index == reportSettings.CustomLimitTypes[custLim].Index);
									if (limit3 != null)
									{
										double.TryParse(limit3.Val, out result);
									}
								}
								if (outputParamInfo.CustomLimits.LowLimit != null && outputParamInfo.CustomLimits.LowLimit.Count > 0)
								{
									Limit limit4 = outputParamInfo.CustomLimits.LowLimit.FirstOrDefault((Limit lLim) => lLim.Index == reportSettings.CustomLimitTypes[custLim].Index);
									if (limit4 != null)
									{
										double.TryParse(limit4.Val, out result2);
									}
								}
							}
							if (CommonFunctions.IsViolatingLimits(outputParamInfo.Val, result2, result))
							{
								list4.Add(new Tuple<DataRow, string, string>(dataRow5, columnName, reportSettings.CustomLimitTypes[custLim].Color));
							}
						}
					}
					else if (CommonFunctions.IsViolatingLimits(outputParamInfo.Val, min, max))
					{
						list4.Add(new Tuple<DataRow, string, string>(dataRow5, columnName, ""));
					}
				}
			}
			if (seriesGroup.HasCustomLimit)
			{
				seriesGroup.HasMaxLimit = false;
				seriesGroup.HasMinLimit = false;
				dataRow2?.Delete();
				dataRow?.Delete();
				seriesGroup.SeriesTbl.AcceptChanges();
			}
			if (sortedDictionary.Count > 0)
			{
				foreach (KeyValuePair<long, List<DataRow>> item2 in sortedDictionary)
				{
					item2.Value.ForEach(delegate(DataRow row)
					{
						seriesGroup.SeriesTbl.Rows.Add(row);
					});
				}
			}
			RemoveEmptyRowsFromSeriesGroupTable(seriesGroup.SeriesTbl);
			foreach (Tuple<DataRow, string, string> item3 in list4)
			{
				if (!seriesGroup.DataViolationsDict.ContainsKey(CommonFunctions.CreateCellLocator(seriesGroup.SeriesTbl, item3.Item1, item3.Item2)))
				{
					seriesGroup.DataViolationsDict[CommonFunctions.CreateCellLocator(seriesGroup.SeriesTbl, item3.Item1, item3.Item2)] = item3.Item3;
				}
			}
			if (seriesGroup.SeriesTbl != null && seriesGroup.SeriesTbl.Rows.Count > 0)
			{
				if (seriesGroup.HasMinLimit && seriesGroup.HasMaxLimit)
				{
					seriesGroup.LimitRowsStartIndex = 0;
					seriesGroup.LimitRowsStopIndex = 1;
				}
				else if (seriesGroup.HasMinLimit && !seriesGroup.HasMaxLimit)
				{
					seriesGroup.LimitRowsStartIndex = 0;
					seriesGroup.LimitRowsStopIndex = 0;
				}
				else if (seriesGroup.HasMaxLimit && !seriesGroup.HasMinLimit)
				{
					seriesGroup.LimitRowsStartIndex = 0;
					seriesGroup.LimitRowsStopIndex = 0;
				}
				else if (seriesGroup.HasCustomLimit && customLimitRows != null && customLimitRows.Count > 0)
				{
					seriesGroup.CustomLimitRowsStartIndex = 0;
					int num = -1;
					foreach (DataRow item4 in customLimitRows)
					{
						if (item4.RowState != DataRowState.Detached)
						{
							num++;
						}
					}
					seriesGroup.CustomLimitRowsStopIndex = num;
				}
				if (seriesGroup.LimitRowsStopIndex + 1 < seriesGroup.SeriesTbl.Rows.Count || seriesGroup.CustomLimitRowsStopIndex + 1 < seriesGroup.SeriesTbl.Rows.Count)
				{
					if (seriesGroup.HasCustomLimit)
					{
						seriesGroup.DataRowsStartIndex = seriesGroup.CustomLimitRowsStopIndex + 1;
						seriesGroup.DataRowsStopIndex = seriesGroup.SeriesTbl.Rows.Count - 1;
					}
					else
					{
						seriesGroup.DataRowsStartIndex = seriesGroup.LimitRowsStopIndex + 1;
						seriesGroup.DataRowsStopIndex = seriesGroup.SeriesTbl.Rows.Count - 1;
					}
				}
			}
			AddStatistics(seriesGroup);
			double minVal = seriesGroup.MinYAxisVal;
			double maxVal = seriesGroup.MaxYAxisVal;
			CommonFunctions.RoundSmart(ref minVal, ref maxVal, forceRes: false, 0);
			seriesGroup.MinYAxisVal = minVal;
			seriesGroup.MaxYAxisVal = maxVal;
			double minVal2 = seriesGroup.AutoStatsMinYAxis;
			double maxVal2 = seriesGroup.AutoStatsMaxYAxis;
			CommonFunctions.RoundSmart(ref minVal2, ref maxVal2, forceRes: false, 0);
			seriesGroup.AutoStatsMinYAxis = minVal2;
			seriesGroup.AutoStatsMaxYAxis = maxVal2;
			if (seriesGroup.SeriesTbl != null && seriesGroup.SeriesTbl.Rows.Count > 0)
			{
				list.Add(seriesGroup);
			}
		}
		if (list2.Count > 0)
		{
			primaryYAxisTitle = string.Join(":", list2.Distinct());
		}
		if (list3.Count > 0)
		{
			secondaryYAxisTitle = string.Join(":", list3.Distinct());
		}
		return list;
	}

	private string CombineColumnValues(List<DescriptorItem> xAxisDiscItems, TestRunInfo runInfo)
	{
		List<string> list = new List<string>();
		if (runInfo != null)
		{
			for (int i = 0; i < xAxisDiscItems.Count; i++)
			{
				if (!xAxisDiscItems[i].IsVisible)
				{
					continue;
				}
				if (xAxisDiscItems[i].IsFilter)
				{
					if (!string.IsNullOrEmpty(xAxisDiscItems[i].Name))
					{
						list.Add(xAxisDiscItems[i].Name + xAxisDiscItems[i].Value);
					}
					else
					{
						list.Add(xAxisDiscItems[i].Id + xAxisDiscItems[i].Value);
					}
				}
				else if (runInfo.ContainsParam(xAxisDiscItems[i].Id))
				{
					ParamInfo paramValue = runInfo.GetParamValue(xAxisDiscItems[i].Id);
					list.Add(double.IsNaN(paramValue.Val) ? paramValue.StrVal : paramValue.Val.ToString());
				}
			}
		}
		return string.Join(":", list);
	}

	private string GetXAxisConditionName(List<DescriptorItem> xAxisDiscItems, TestRunInfo runInfo)
	{
		List<string> list = new List<string>();
		_ = string.Empty;
		if (runInfo != null)
		{
			for (int i = 0; i < xAxisDiscItems.Count; i++)
			{
				_ = string.Empty;
				if (!xAxisDiscItems[i].IsVisible)
				{
					continue;
				}
				if (!string.IsNullOrEmpty(xAxisDiscItems[i].Name))
				{
					if (xAxisDiscItems[i].IsFilter)
					{
						list.Add(xAxisDiscItems[i].Name + xAxisDiscItems[i].Value);
					}
					else
					{
						list.Add(xAxisDiscItems[i].Name);
					}
				}
				else if (runInfo.ContainsParam(xAxisDiscItems[i].Id))
				{
					string text = xAxisDiscItems[i].Id;
					if (!string.IsNullOrEmpty(runInfo.GetParamValue(xAxisDiscItems[i].Id).ParamUnit))
					{
						text = text + "(" + runInfo.GetParamValue(xAxisDiscItems[i].Id).ParamUnit + ")";
					}
					if (xAxisDiscItems[i].IsFilter)
					{
						text += xAxisDiscItems[i].Value;
					}
					list.Add(text);
				}
			}
		}
		return string.Join(":", list);
	}

	private void RemoveEmptyRowsFromSeriesGroupTable(DataTable dataTable)
	{
		if (dataTable == null)
		{
			return;
		}
		List<DataRow> list = new List<DataRow>();
		for (int i = 0; i < dataTable.Rows.Count; i++)
		{
			bool flag = true;
			for (int j = 1; j < dataTable.Columns.Count; j++)
			{
				if (!Convert.IsDBNull(dataTable.Rows[i][j]) && !string.IsNullOrEmpty(dataTable.Rows[i][j].ToString()))
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				list.Add(dataTable.Rows[i]);
			}
		}
		list.ForEach(delegate(DataRow dataRow)
		{
			dataTable.Rows.Remove(dataRow);
		});
	}

	private void AddStatistics(LinearSeriesGroup seriesGroup)
	{
		DataTable dataTable = seriesGroup.SeriesTbl;
		if (dataTable == null || dataTable.Rows.Count <= 0)
		{
			return;
		}
		DataRow dataRow = dataTable.NewRow();
		dataTable.Rows.Add(dataRow);
		dataRow[0] = seriesGroup.SeriesName;
		seriesGroup.StatisticsRowsStartIndex = dataTable.Rows.Count - 1;
		DataRow dataRow2 = null;
		DataRow dataRow3 = null;
		DataRow dataRow4 = null;
		DataRow dataRow5 = null;
		DataRow dataRow6 = null;
		DataRow dataRow7 = null;
		DataRow dataRow8 = null;
		DataRow dataRow9 = null;
		DataRow dataRow10 = null;
		DataRow dataRow11 = null;
		List<DataRow> list = new List<DataRow>();
		List<DataRow> list2 = new List<DataRow>();
		if (!reportSettings.SuppressStatisticsPlot)
		{
			dataRow2 = dataTable.NewRow();
			dataTable.Rows.Add(dataRow2);
			dataRow2[0] = "StdDev";
			if (seriesGroup.HasMaxLimit)
			{
				dataRow3 = dataTable.NewRow();
				dataTable.Rows.Add(dataRow3);
				dataRow3[0] = "Cpk_Hi";
			}
			if (seriesGroup.HasMinLimit)
			{
				dataRow4 = dataTable.NewRow();
				dataTable.Rows.Add(dataRow4);
				dataRow4[0] = "Cpk_Lo";
			}
			if (reportSettings.CustomLimitTypes != null && (reportSettings.ShowCustomLimits || reportSettings.AutoLimitDescriptor) && seriesGroup.HasCustomLimit)
			{
				for (int i = seriesGroup.CustomLimitRowsStartIndex; i <= seriesGroup.CustomLimitRowsStopIndex; i++)
				{
					string text = seriesGroup.SeriesTbl.Rows[i][0].ToString();
					if (text.IndexOf("Max Lim ") > -1)
					{
						string highLimitName = text.Substring(text.IndexOf("Max Lim ") + 8);
						if (reportSettings.CustomLimitTypes.Exists((LimitGroupType lim) => lim.Name.Contains(highLimitName)))
						{
							DataRow dataRow12 = dataTable.NewRow();
							dataTable.Rows.Add(dataRow12);
							dataRow12[0] = "Cpk_Hi " + highLimitName;
							list.Add(dataRow12);
						}
					}
					if (text.IndexOf("Min Lim ") > -1)
					{
						string lowLimitName = text.Substring(text.IndexOf("Min Lim ") + 8);
						if (reportSettings.CustomLimitTypes.Exists((LimitGroupType lim) => lim.Name.Contains(lowLimitName)))
						{
							DataRow dataRow13 = dataTable.NewRow();
							dataTable.Rows.Add(dataRow13);
							dataRow13[0] = "Cpk_Lo " + lowLimitName;
							list2.Add(dataRow13);
						}
					}
				}
			}
			dataRow5 = dataTable.NewRow();
			dataTable.Rows.Add(dataRow5);
			dataRow5[0] = "Median";
			dataRow6 = dataTable.NewRow();
			dataTable.Rows.Add(dataRow6);
			dataRow6[0] = "Avg+3StDev";
			dataRow7 = dataTable.NewRow();
			dataTable.Rows.Add(dataRow7);
			dataRow7[0] = "Max";
			dataRow8 = dataTable.NewRow();
			dataTable.Rows.Add(dataRow8);
			dataRow8[0] = "Min";
			dataRow9 = dataTable.NewRow();
			dataTable.Rows.Add(dataRow9);
			dataRow9[0] = "Avg-3StDev";
			dataRow10 = dataTable.NewRow();
			dataTable.Rows.Add(dataRow10);
			dataRow10[0] = "Samples";
			dataRow11 = dataTable.NewRow();
			dataTable.Rows.Add(dataRow11);
			dataRow11[0] = "Yield(%)";
		}
		seriesGroup.StatisticsRowsStopIndex = dataTable.Rows.Count - 1;
		List<double> list3 = null;
		List<double> list4 = null;
		List<double> list5 = null;
		double minLimit = double.NegativeInfinity;
		double maxLimit = double.PositiveInfinity;
		for (int j = 1; j < dataTable.Columns.Count; j++)
		{
			minLimit = double.NegativeInfinity;
			maxLimit = double.PositiveInfinity;
			list3 = new List<double>();
			list4 = new List<double>();
			list5 = new List<double>();
			if (seriesGroup.LimitRowsStartIndex != -1 && seriesGroup.LimitRowsStopIndex != -1)
			{
				for (int k = seriesGroup.LimitRowsStartIndex; k <= seriesGroup.LimitRowsStopIndex; k++)
				{
					if (Convert.IsDBNull(dataTable.Rows[k][j]) || string.IsNullOrEmpty(dataTable.Rows[k][j].ToString()))
					{
						continue;
					}
					if (dataTable.Rows[k][0].ToString() == "Min Lim")
					{
						double result = double.NaN;
						if (double.TryParse(dataTable.Rows[k][j].ToString(), out result))
						{
							minLimit = result;
						}
					}
					else if (dataTable.Rows[k][0].ToString() == "Max Lim")
					{
						double result2 = double.NaN;
						if (double.TryParse(dataTable.Rows[k][j].ToString(), out result2))
						{
							maxLimit = result2;
						}
					}
				}
			}
			List<Tuple<string, double, double>> list6 = new List<Tuple<string, double, double>>();
			if (reportSettings.CustomLimitTypes != null && (reportSettings.ShowCustomLimits || reportSettings.AutoLimitDescriptor) && seriesGroup.HasCustomLimit)
			{
				int limitRowIndex;
				for (limitRowIndex = seriesGroup.CustomLimitRowsStartIndex; limitRowIndex <= seriesGroup.CustomLimitRowsStopIndex; limitRowIndex++)
				{
					double num = double.NaN;
					double num2 = double.NaN;
					if (Convert.IsDBNull(dataTable.Rows[limitRowIndex][j]) || string.IsNullOrEmpty(dataTable.Rows[limitRowIndex][j].ToString()))
					{
						continue;
					}
					LimitGroupType limitGroupType = reportSettings.CustomLimitTypes.Find((LimitGroupType lim) => dataTable.Rows[limitRowIndex][0].ToString().Contains(lim.Name) && dataTable.Rows[limitRowIndex][0].ToString().Contains("Min Lim "));
					LimitGroupType limitGroupType2 = reportSettings.CustomLimitTypes.Find((LimitGroupType lim) => dataTable.Rows[limitRowIndex][0].ToString().Contains(lim.Name) && dataTable.Rows[limitRowIndex][0].ToString().Contains("Max Lim "));
					if (limitGroupType != null)
					{
						double result3 = double.NaN;
						if (double.TryParse(dataTable.Rows[limitRowIndex][j].ToString(), out result3))
						{
							num = result3;
							list5.Add(num);
						}
					}
					else if (limitGroupType2 != null)
					{
						double result4 = double.NaN;
						if (double.TryParse(dataTable.Rows[limitRowIndex][j].ToString(), out result4))
						{
							num2 = result4;
							list4.Add(num2);
						}
					}
					list6.Add(new Tuple<string, double, double>(dataTable.Rows[limitRowIndex][0].ToString(), num, num2));
				}
			}
			if (reportSettings.ShowLimitUnits)
			{
				if ((reportSettings.ShowCustomLimits || reportSettings.AutoLimitDescriptor) && seriesGroup.HasCustomLimit)
				{
					if (list5 != null && list5.Count > 0 && !double.IsNegativeInfinity(list5.Min()) && (double.IsNegativeInfinity(seriesGroup.MinYAxisVal) || list5.Min() < seriesGroup.MinYAxisVal))
					{
						seriesGroup.MinYAxisVal = list5.Min();
					}
					if (list4 != null && list4.Count > 0 && !double.IsPositiveInfinity(list4.Max()) && (double.IsPositiveInfinity(seriesGroup.MaxYAxisVal) || list4.Max() > seriesGroup.MaxYAxisVal))
					{
						seriesGroup.MaxYAxisVal = list4.Max();
					}
				}
				else
				{
					if (seriesGroup.HasMinLimit && !double.IsNegativeInfinity(minLimit) && (double.IsNegativeInfinity(seriesGroup.MinYAxisVal) || minLimit < seriesGroup.MinYAxisVal))
					{
						seriesGroup.MinYAxisVal = minLimit;
					}
					if (seriesGroup.HasMaxLimit && !double.IsPositiveInfinity(maxLimit) && (double.IsPositiveInfinity(seriesGroup.MaxYAxisVal) || maxLimit > seriesGroup.MaxYAxisVal))
					{
						seriesGroup.MaxYAxisVal = maxLimit;
					}
				}
			}
			if (seriesGroup.DataRowsStartIndex != -1 && seriesGroup.DataRowsStopIndex != -1)
			{
				for (int l = seriesGroup.DataRowsStartIndex; l <= seriesGroup.DataRowsStopIndex; l++)
				{
					if (!Convert.IsDBNull(dataTable.Rows[l][j]) && !string.IsNullOrEmpty(dataTable.Rows[l][j].ToString()))
					{
						double result5 = double.NaN;
						if (double.TryParse(dataTable.Rows[l][j].ToString(), out result5))
						{
							list3.Add(result5);
						}
					}
				}
			}
			if (list3.Count <= 0)
			{
				continue;
			}
			list3.Sort();
			double num3 = list3.Avg();
			dataRow[j] = num3;
			double num4 = list3[0];
			double num5 = list3[list3.Count - 1];
			if (double.IsNegativeInfinity(seriesGroup.MinYAxisVal) || num4 < seriesGroup.MinYAxisVal)
			{
				seriesGroup.MinYAxisVal = num4;
			}
			if (double.IsPositiveInfinity(seriesGroup.MaxYAxisVal) || num5 > seriesGroup.MaxYAxisVal)
			{
				seriesGroup.MaxYAxisVal = num5;
			}
			if (reportSettings.SuppressStatisticsPlot)
			{
				continue;
			}
			if (double.IsNegativeInfinity(seriesGroup.AutoStatsMinYAxis) || seriesGroup.MinYAxisVal < seriesGroup.AutoStatsMinYAxis)
			{
				seriesGroup.AutoStatsMinYAxis = seriesGroup.MinYAxisVal;
			}
			if (double.IsPositiveInfinity(seriesGroup.AutoStatsMaxYAxis) || seriesGroup.MaxYAxisVal > seriesGroup.AutoStatsMaxYAxis)
			{
				seriesGroup.AutoStatsMaxYAxis = seriesGroup.MaxYAxisVal;
			}
			double num6 = list3.StandardDeviation();
			double num7 = 0.0;
			double num8 = 0.0;
			dataRow2[j] = num6;
			if (num6 == 0.0)
			{
				if (seriesGroup.HasMaxLimit)
				{
					dataRow3[j] = double.NaN;
				}
				if (seriesGroup.HasMinLimit)
				{
					dataRow4[j] = double.NaN;
				}
			}
			else
			{
				if (seriesGroup.HasMaxLimit && maxLimit != double.PositiveInfinity)
				{
					dataRow3[j] = Math.Abs(num3 - maxLimit) / (3.0 * num6);
				}
				if (seriesGroup.HasMinLimit && minLimit != double.NegativeInfinity)
				{
					dataRow4[j] = Math.Abs(num3 - minLimit) / (3.0 * num6);
				}
			}
			string lLimType = string.Empty;
			string hLimType = string.Empty;
			foreach (Tuple<string, double, double> item in list6)
			{
				lLimType = string.Empty;
				hLimType = string.Empty;
				if (item != null && !string.IsNullOrEmpty(item.Item1))
				{
					if (item.Item1.IndexOf("Min Lim ") > -1)
					{
						lLimType = item.Item1.Substring(item.Item1.IndexOf("Min Lim ") + 8);
					}
					if (item.Item1.IndexOf("Max Lim ") > -1)
					{
						hLimType = item.Item1.Substring(item.Item1.IndexOf("Max Lim ") + 8);
					}
				}
				if (list2 != null && !string.IsNullOrEmpty(lLimType))
				{
					DataRow dataRow14 = list2.Find((DataRow lw) => lw[0].ToString().Contains(lLimType));
					if (dataRow14 != null)
					{
						if (num6 == 0.0)
						{
							dataRow14[j] = double.NaN;
						}
						else if (item != null && item.Item2 != double.PositiveInfinity && !string.IsNullOrEmpty(lLimType))
						{
							dataRow14[j] = Math.Abs(num3 - item.Item2) / (3.0 * num6);
						}
					}
				}
				if (list == null || string.IsNullOrEmpty(hLimType))
				{
					continue;
				}
				DataRow dataRow15 = list.Find((DataRow hg) => hg[0].ToString().Contains(hLimType));
				if (dataRow15 != null)
				{
					if (num6 == 0.0)
					{
						dataRow15[j] = double.NaN;
					}
					else if (item != null && item.Item3 != double.PositiveInfinity && !string.IsNullOrEmpty(hLimType))
					{
						dataRow15[j] = Math.Abs(num3 - item.Item3) / (3.0 * num6);
					}
				}
			}
			num8 = num3 - 3.0 * num6;
			num7 = num3 + 3.0 * num6;
			dataRow6[j] = num7;
			dataRow9[j] = num8;
			if (double.IsNegativeInfinity(seriesGroup.AutoStatsMinYAxis) || num8 < seriesGroup.AutoStatsMinYAxis)
			{
				seriesGroup.AutoStatsMinYAxis = num8;
			}
			if (double.IsPositiveInfinity(seriesGroup.AutoStatsMaxYAxis) || num7 > seriesGroup.AutoStatsMaxYAxis)
			{
				seriesGroup.AutoStatsMaxYAxis = num7;
			}
			dataRow5[j] = list3.Median();
			dataRow7[j] = num5;
			dataRow8[j] = num4;
			dataRow10[j] = list3.Count;
			int num9 = list3.Where((double dp) => dp >= minLimit && dp <= maxLimit).Count();
			if (num9 > 0)
			{
				dataRow11[j] = (double)num9 / (double)list3.Count * 100.0;
			}
			else
			{
				dataRow11[j] = 0;
			}
		}
	}
}
