using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace QC.CTDS.DM.DataStructures.FormatDescriptorStructure;

[Serializable]
public class StatisticsResultDataSet : ResultDataSet
{
	private ObservableCollection<StatsType> statisticsTypeCollection = new ObservableCollection<StatsType>();

	private ObservableCollection<StatsType> statisticsTypesList = new ObservableCollection<StatsType>();

	[XmlElement("StatsType", Form = XmlSchemaForm.Unqualified)]
	public ObservableCollection<StatsType> StatsTypes
	{
		get
		{
			return statisticsTypeCollection;
		}
		set
		{
			if (statisticsTypeCollection != value)
			{
				statisticsTypeCollection = value;
			}
		}
	}

	[XmlIgnore]
	public ObservableCollection<StatsType> StatCollectionList
	{
		get
		{
			if (statisticsTypesList == null || statisticsTypesList.Count <= 0)
			{
				CreatePreDefinedStatsTypeList();
			}
			return statisticsTypesList;
		}
		set
		{
			statisticsTypesList = value;
			OnPropertyChanged("StatCollectionList");
		}
	}

	private void CreatePreDefinedStatsTypeList()
	{
		statisticsTypesList.Add(new StatsType
		{
			id = "Min"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Max"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Yield"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Avg"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Sum"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Max Value"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Min Value"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "StdDev"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Median"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Samples"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Worst Case Margin"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Mean Margin"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Cpk"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Status"
		});
		statisticsTypesList.Add(new StatsType
		{
			id = "Actual Values"
		});
	}

	public void CreateNewStatsType(StatsType statsType)
	{
		if (!StatsTypes.Any((StatsType st) => st.id == statsType.id) && statisticsTypesList.Any((StatsType stats) => stats.id == statsType.id))
		{
			statsType.IsSelected = true;
			StatsTypes.Add(statsType);
		}
	}

	public void RemoveStatsType(string statsTypeId)
	{
		StatsType statsType = StatsTypes.FirstOrDefault((StatsType statDel) => statDel.id == statsTypeId);
		if (statsType != null)
		{
			StatsTypes.Remove(statsType);
		}
	}

	public StatisticsResultDataSet()
	{
		StatsTypes.CollectionChanged += EstablishParentChildRelationOnCollectionChanged;
		StatsTypes.CollectionChanged += StatsTypes_CollectionChanged;
	}

	public StatisticsResultDataSet(StatisticsResultDataSet statResultSet)
		: base(statResultSet)
	{
		statisticsTypeCollection = new ObservableCollection<StatsType>();
		foreach (StatsType statsType in statResultSet.StatsTypes)
		{
			statisticsTypeCollection.Add(new StatsType(statsType));
		}
	}

	public StatisticsResultDataSet(ResultDataSet resultSet)
		: base(resultSet)
	{
	}

	private void StatsTypes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		OnPropertyChanged("StatsTypes");
	}
}
