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 CondensedSummary : GroupSetDescriptor
{
	private ObservableCollection<IndexedParameter> indexedParam = new ObservableCollection<IndexedParameter>();

	private ObservableCollection<ResultDataSet> result = new ObservableCollection<ResultDataSet>();

	private OuputGraphDisplayProperties _OGDisplayProperties;

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

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

	[XmlElement("OGDisplayProperties")]
	public OuputGraphDisplayProperties OGDisplayProperties
	{
		get
		{
			return _OGDisplayProperties;
		}
		set
		{
			if (_OGDisplayProperties != value)
			{
				_OGDisplayProperties = value;
				base.OnPropertyChanged("OGDisplayProperties");
			}
		}
	}

	public override string this[string columnName]
	{
		get
		{
			string text = null;
			if (columnName == "CondensedSummaryName" && string.IsNullOrEmpty(base.Name))
			{
				text = "Condensed Summary Name cannot be emtpy";
			}
			return text;
		}
	}

	public override void Changed(string changedFieldName)
	{
		switch (changedFieldName)
		{
		case "IsChecked":
		case "IsExpanded":
		case "IsSelected":
			if (base.Parent != null)
			{
				base.Parent.Changed(changedFieldName);
			}
			break;
		default:
			base.Changed(changedFieldName);
			break;
		}
	}

	public CondensedSummary()
	{
		IndexedParam.CollectionChanged += EstablishParentChildRelationOnCollectionChanged;
		IndexedParam.CollectionChanged += IndexedParam_CollectionChanged;
		Result.CollectionChanged += EstablishParentChildRelationOnCollectionChanged;
		Result.CollectionChanged += Result_CollectionChanged;
	}

	private void Result_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		OnPropertyChanged("Result");
	}

	private void IndexedParam_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		OnPropertyChanged("IndexedParam");
	}

	public override void EstablishParentChildRelationship()
	{
		base.EstablishParentChildRelationship();
	}

	public void CreateNewResult()
	{
		ResultDataSet resultDataSet = new ResultDataSet();
		Result.Add(resultDataSet);
		resultDataSet.resID = "ParameterName";
		resultDataSet.testID = "TestName";
		resultDataSet.IsSelected = true;
	}

	public override List<string> GetTestNames()
	{
		List<string> testNames = new List<string>();
		Result.ForEach(delegate(ResultDataSet res)
		{
			testNames.Add(res.testID);
		});
		return testNames.Distinct().ToList();
	}

	internal override void ApplyFileGrouping(IEnumerable<string> groupNames)
	{
		List<ResultDataSet> list = new List<ResultDataSet>();
		foreach (ResultDataSet item in Result.Where((ResultDataSet res) => !Constants.AllTestsString.Contains(res.testID)))
		{
			foreach (string groupName in groupNames)
			{
				ResultDataSet resultDataSet = new ResultDataSet(item);
				resultDataSet.GroupName = groupName;
				resultDataSet.testID = resultDataSet.testID + "_" + groupName;
				list.Add(resultDataSet);
			}
		}
		Result.RemoveAll((ResultDataSet res) => Constants.AllTestsString.Contains(res.testID));
		list.ForEach(delegate(ResultDataSet resultSet)
		{
			Result.Add(resultSet);
		});
	}

	internal override void RemoveFileGrouping(IEnumerable<string> groupNames)
	{
		HashSet<string> hashSet = new HashSet<string>();
		List<ResultDataSet> list = new List<ResultDataSet>();
		foreach (ResultDataSet item in Result.Where((ResultDataSet 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 + item.resID))
					{
						hashSet.Add(text + item.resID);
						item.testID = text;
						list.Add(item);
					}
				}
			}
		}
		Result.RemoveAll((ResultDataSet res) => !Constants.AllTestsString.Contains(res.testID));
		list.ForEach(delegate(ResultDataSet resultSet)
		{
			Result.Add(resultSet);
		});
	}

	internal override void ApplyGroupPrefix(Discriminator discriminator)
	{
		if (discriminator != null)
		{
			Result.ForEach(delegate(ResultDataSet res)
			{
				res.PrefixDiscriminator(discriminator);
			});
		}
	}

	public override IEnumerable<ResultDataSet> GetResultSets()
	{
		return Result;
	}
}
