using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;
using QC.CTDS.DM.SharedLibrary;

namespace QC.CTDS.DM.DataStructures.FormatDescriptorStructure;

[Serializable]
public class ScatterOutputGroupDescriptor : ChartOutputGroupDescriptor
{
	private ObservableCollection<ScatteredChartSeries> series = new ObservableCollection<ScatteredChartSeries>();

	private string _xName;

	private string _yName;

	private string _secYName;

	[XmlElement("Series", Form = XmlSchemaForm.Unqualified)]
	public ObservableCollection<ScatteredChartSeries> Series
	{
		get
		{
			return series;
		}
		set
		{
			if (series != value)
			{
				series = value;
				OnPropertyChanged("Series");
			}
		}
	}

	[XmlAttribute]
	public string xName
	{
		get
		{
			return _xName;
		}
		set
		{
			if (!(_xName == value))
			{
				_xName = value;
				OnPropertyChanged("xName");
			}
		}
	}

	[XmlAttribute]
	public string yName
	{
		get
		{
			return _yName;
		}
		set
		{
			if (!(_yName == value))
			{
				_yName = value;
				OnPropertyChanged("yName");
			}
		}
	}

	[XmlAttribute]
	public string secYName
	{
		get
		{
			return _secYName;
		}
		set
		{
			if (!(_secYName == value))
			{
				_secYName = value;
				OnPropertyChanged("secYName");
			}
		}
	}

	public override string this[string columnName]
	{
		get
		{
			string result = null;
			if (columnName == "ScatteredChartName" && string.IsNullOrEmpty(base.Name))
			{
				result = "Scattered chart name cannot be null or empty";
			}
			if (columnName == "xName" && string.IsNullOrEmpty(xName))
			{
				result = "X Axis name cannot be null or empty";
			}
			if (columnName == "yName" && string.IsNullOrEmpty(yName))
			{
				result = "Y Axis name cannot be null or empty";
			}
			return result;
		}
	}

	public ScatterOutputGroupDescriptor()
	{
		Series.CollectionChanged += EstablishParentChildRelationOnCollectionChanged;
		Series.CollectionChanged += Series_CollectionChanged;
	}

	private void Series_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		OnPropertyChanged("Series");
	}

	public void CreateNewSeries()
	{
		ScatteredChartSeries scatteredChartSeries = new ScatteredChartSeries();
		Series.Add(scatteredChartSeries);
		scatteredChartSeries.testID = "Series " + Series.IndexOf(scatteredChartSeries);
		scatteredChartSeries.DepVar = new Series_Var
		{
			IdVariable = "Parameter"
		};
		scatteredChartSeries.IndVar = new Series_Var
		{
			IdVariable = "Loop condition"
		};
		scatteredChartSeries.IsSelected = true;
	}

	public override List<string> GetTestNames()
	{
		List<string> testNames = new List<string>();
		Series.ForEach(delegate(ScatteredChartSeries seriesVal)
		{
			testNames.Add(seriesVal.testID);
		});
		return testNames.Distinct().ToList();
	}

	internal override void ApplyFileGrouping(IEnumerable<string> groupNames)
	{
		List<ScatteredChartSeries> list = new List<ScatteredChartSeries>();
		foreach (ScatteredChartSeries item in Series.Where((ScatteredChartSeries ser) => !Constants.AllTestsString.Contains(ser.testID)))
		{
			foreach (string groupName in groupNames)
			{
				ScatteredChartSeries scatteredChartSeries = new ScatteredChartSeries(item);
				scatteredChartSeries.testID = scatteredChartSeries.testID + "_" + groupName;
				scatteredChartSeries.GroupName = groupName;
				list.Add(scatteredChartSeries);
			}
		}
		Series.RemoveAll((ScatteredChartSeries ser) => !Constants.AllTestsString.Contains(ser.testID));
		list.ForEach(delegate(ScatteredChartSeries series)
		{
			Series.Add(series);
		});
	}

	internal override void RemoveFileGrouping(IEnumerable<string> groupNames)
	{
		HashSet<string> hashSet = new HashSet<string>();
		List<ScatteredChartSeries> list = new List<ScatteredChartSeries>();
		foreach (ScatteredChartSeries item in Series.Where((ScatteredChartSeries res) => !Constants.AllTestsString.Contains(res.testID)))
		{
			foreach (string groupName in groupNames)
			{
				if (item.testID.EndsWith("_" + groupName))
				{
					string text = item.testID.Replace("_" + groupName, "");
					if (!hashSet.Contains(text))
					{
						hashSet.Add(text);
						item.testID = text;
						list.Add(item);
					}
				}
			}
		}
		Series.RemoveAll((ScatteredChartSeries res) => !Constants.AllTestsString.Contains(res.testID));
		list.ForEach(delegate(ScatteredChartSeries series)
		{
			Series.Add(series);
		});
	}

	public override IEnumerable<ResultDataSet> GetResultSets()
	{
		List<ResultDataSet> list = new List<ResultDataSet>();
		foreach (ScatteredChartSeries item in Series)
		{
			if (item.DepVar != null && !string.IsNullOrEmpty(item.DepVar.IdVariable))
			{
				ResultDataSet resultDataSet = new ResultDataSet();
				resultDataSet.testID = item.testID;
				resultDataSet.resID = item.DepVar.IdVariable;
				list.Add(resultDataSet);
			}
		}
		return list;
	}

	internal override void ApplyGroupPrefix(Discriminator discriminator)
	{
		if (discriminator != null)
		{
			Series.ForEach(delegate(ScatteredChartSeries ser)
			{
				ser.PrefixDiscriminator(discriminator);
			});
		}
	}
}
