using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
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 OutputGroupSetDescriptor : GroupSetDescriptor
{
	private uint _SheetWidth;

	private short _ChartsPerSlide = 1;

	private ObservableCollection<GroupSetDescriptor> groupSetDescCollection = new ObservableCollection<GroupSetDescriptor>();

	[DefaultValue(0)]
	[XmlAttribute(AttributeName = "width")]
	public uint SheetWidth
	{
		get
		{
			return _SheetWidth;
		}
		set
		{
			if (_SheetWidth != value)
			{
				_SheetWidth = value;
				base.OnPropertyChanged("SheetWidth");
			}
		}
	}

	[DefaultValue(1)]
	[XmlAttribute(AttributeName = "chartsPerSlide")]
	public short ChartsPerSlide
	{
		get
		{
			return _ChartsPerSlide;
		}
		set
		{
			if (_ChartsPerSlide != value)
			{
				_ChartsPerSlide = value;
				base.OnPropertyChanged("ChartsPerSlide");
			}
		}
	}

	[XmlIgnore]
	public override bool PPTMode
	{
		get
		{
			return pptMode;
		}
		set
		{
			if (pptMode == value)
			{
				return;
			}
			pptMode = value;
			OnPropertyChanged("PPTMode");
			if (GroupSetDescCollection != null)
			{
				GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupSetDesc)
				{
					groupSetDesc.PPTMode = value;
				});
			}
		}
	}

	[XmlIgnore]
	public override bool? IsChecked
	{
		get
		{
			return isChecked;
		}
		set
		{
			SetState(value, updateChildren: true);
		}
	}

	[XmlIgnore]
	public override bool? IsForPPTChecked
	{
		get
		{
			return isForPPTChecked;
		}
		set
		{
			SetPPTState(value, updateChildren: true);
		}
	}

	[XmlElement(ElementName = "CondensedSummaryDescriptor", Type = typeof(CondensedSummary), Form = XmlSchemaForm.Unqualified)]
	[XmlElement(ElementName = "OutputGroupDescriptor_Sc", Type = typeof(ScatterOutputGroupDescriptor), Form = XmlSchemaForm.Unqualified)]
	[XmlElement(ElementName = "WideCondensedSummary", Type = typeof(WideCondensedSummary), Form = XmlSchemaForm.Unqualified)]
	[XmlElement(ElementName = "OutputGroupDescriptor_MultiD", Type = typeof(MultiDimensionOutputGroupDescriptor), Form = XmlSchemaForm.Unqualified)]
	[XmlElement(ElementName = "OutputGroupDescriptor_Lin", Type = typeof(LineOutputGroupDescriptor), Form = XmlSchemaForm.Unqualified)]
	[XmlElement(ElementName = "OutputGroupDescriptor_Hist", Type = typeof(HistogramOutputGroupDescriptor), Form = XmlSchemaForm.Unqualified)]
	public ObservableCollection<GroupSetDescriptor> GroupSetDescCollection
	{
		get
		{
			return groupSetDescCollection;
		}
		set
		{
			if (groupSetDescCollection != value)
			{
				groupSetDescCollection = value;
				base.OnPropertyChanged("GroupSetDescCollection");
			}
		}
	}

	public override string this[string columnName]
	{
		get
		{
			string result = null;
			if (columnName == "OutputGroupName" && string.IsNullOrEmpty(base.Name))
			{
				result = "Sheet name cannot be empty";
			}
			if (columnName == "SheetWidth" && SheetWidth < 0)
			{
				result = "Sheet width cannot be less than zero";
			}
			if (columnName == "ChartsPerSlide" && (ChartsPerSlide < 1 || ChartsPerSlide > 4))
			{
				result = "Number of charts per slide have to between 1 and 4";
			}
			return result;
		}
	}

	public OutputGroupSetDescriptor()
	{
		GroupSetDescCollection.CollectionChanged += EstablishParentChildRelationOnCollectionChanged;
		GroupSetDescCollection.CollectionChanged += GroupSetDescCollection_CollectionChanged;
	}

	private void GroupSetDescCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		base.OnPropertyChanged("GroupSetDescCollection");
	}

	protected void SetState(bool? state, bool updateChildren)
	{
		if (isChecked == state)
		{
			return;
		}
		isChecked = state;
		if (isChecked.HasValue && updateChildren && GroupSetDescCollection != null)
		{
			GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupSetDesc)
			{
				groupSetDesc.IsChecked = state;
			});
		}
		OnPropertyChanged("IsChecked");
	}

	protected void SetPPTState(bool? state, bool updateChildren)
	{
		if (isForPPTChecked == state)
		{
			return;
		}
		isForPPTChecked = state;
		if (isForPPTChecked.HasValue && updateChildren && GroupSetDescCollection != null)
		{
			GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupSetDesc)
			{
				groupSetDesc.IsForPPTChecked = state;
			});
		}
		OnPropertyChanged("IsForPPTChecked");
	}

	public override void Changed(string changedFieldName)
	{
		switch (changedFieldName)
		{
		case "IsChecked":
			ChangeSelectedStateFromChildren();
			base.Changed(changedFieldName);
			break;
		case "IsForPPTChecked":
			ChangePPTSelectedStateFromChildren();
			base.Changed(changedFieldName);
			break;
		case "PPTMode":
			break;
		default:
			base.Changed(changedFieldName);
			break;
		case "IsExpanded":
			break;
		case "IsSelected":
			break;
		case "IsPicked":
			break;
		}
	}

	public void ChangeSelectedStateFromChildren()
	{
		bool? state2 = null;
		List<bool> childStates = new List<bool>();
		GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupSetDesc)
		{
			childStates.Add(groupSetDesc.IsChecked.Value);
		});
		if (childStates.Count > 0)
		{
			if (childStates.TrueForAll((bool state) => state))
			{
				state2 = true;
			}
			else if (childStates.TrueForAll((bool state) => !state))
			{
				state2 = false;
			}
		}
		else
		{
			state2 = false;
		}
		SetState(state2, updateChildren: false);
	}

	public void ChangePPTSelectedStateFromChildren()
	{
		bool? state2 = null;
		List<bool> childStates = new List<bool>();
		GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor grpSetDesc)
		{
			childStates.Add(grpSetDesc.IsForPPTChecked.Value);
		});
		if (childStates.Count > 0)
		{
			if (childStates.TrueForAll((bool state) => state))
			{
				state2 = true;
			}
			else if (childStates.TrueForAll((bool state) => !state))
			{
				state2 = false;
			}
		}
		else
		{
			state2 = false;
		}
		SetPPTState(state2, updateChildren: false);
	}

	public void RemoveUnselectedDescriptors()
	{
		RemoveCharts(checkStateToRemove: false);
	}

	public void CreateNewWideCondensedSummary()
	{
		WideCondensedSummary wideCondensedSummary = new WideCondensedSummary();
		wideCondensedSummary.Name = "WideCondensedSummary " + GroupSetDescCollection.Count;
		wideCondensedSummary.CreateNewStatsResult();
		GroupSetDescCollection.Add(wideCondensedSummary);
		base.IsExpanded = true;
		wideCondensedSummary.IsSelected = true;
		ChangeSelectedStateFromChildren();
	}

	public void CreateNewCondensedSummary()
	{
		CondensedSummary condensedSummary = new CondensedSummary();
		condensedSummary.Name = "CondensedSummary " + GroupSetDescCollection.Count;
		condensedSummary.CreateNewResult();
		GroupSetDescCollection.Add(condensedSummary);
		base.IsExpanded = true;
		condensedSummary.IsSelected = true;
		ChangeSelectedStateFromChildren();
	}

	public void CreateNewLineChart()
	{
		LineOutputGroupDescriptor lineOutputGroupDescriptor = new LineOutputGroupDescriptor();
		lineOutputGroupDescriptor.Name = "LineChart  " + GroupSetDescCollection.Count;
		lineOutputGroupDescriptor.CreateNewBin();
		GroupSetDescCollection.Add(lineOutputGroupDescriptor);
		base.IsExpanded = true;
		lineOutputGroupDescriptor.IsForPPTChecked = true;
		lineOutputGroupDescriptor.PPTMode = (PPTMode ? true : false);
		ChangePPTSelectedStateFromChildren();
		lineOutputGroupDescriptor.IsSelected = true;
		ChangeSelectedStateFromChildren();
	}

	public void CreateNewHistogramChart()
	{
		HistogramOutputGroupDescriptor histogramOutputGroupDescriptor = new HistogramOutputGroupDescriptor();
		histogramOutputGroupDescriptor.Name = "Histogram  " + GroupSetDescCollection.Count;
		histogramOutputGroupDescriptor.CreateNewResult();
		GroupSetDescCollection.Add(histogramOutputGroupDescriptor);
		base.IsExpanded = true;
		histogramOutputGroupDescriptor.IsForPPTChecked = true;
		histogramOutputGroupDescriptor.PPTMode = (PPTMode ? true : false);
		ChangePPTSelectedStateFromChildren();
		histogramOutputGroupDescriptor.IsSelected = true;
		ChangeSelectedStateFromChildren();
	}

	public void CreateNewScatteredChart()
	{
		ScatterOutputGroupDescriptor scatterOutputGroupDescriptor = new ScatterOutputGroupDescriptor();
		scatterOutputGroupDescriptor.Name = "Scattered  " + GroupSetDescCollection.Count;
		scatterOutputGroupDescriptor.xName = "X axis";
		scatterOutputGroupDescriptor.yName = "Y axis";
		scatterOutputGroupDescriptor.CreateNewSeries();
		GroupSetDescCollection.Add(scatterOutputGroupDescriptor);
		base.IsExpanded = true;
		scatterOutputGroupDescriptor.IsForPPTChecked = true;
		scatterOutputGroupDescriptor.PPTMode = (PPTMode ? true : false);
		ChangePPTSelectedStateFromChildren();
		scatterOutputGroupDescriptor.IsSelected = true;
		ChangeSelectedStateFromChildren();
	}

	public void CreateNewMultiDimensionChart()
	{
		CreateNewMultiDimensionChart("X axis", "Y axis", "Z axis", null, null);
	}

	public void CreateNewMultiDimensionChart(string xAxisName, string yAxisName, string zAxisName, string testID, string chartTitle)
	{
		MultiDimensionOutputGroupDescriptor multiDimensionOutputGroupDescriptor = new MultiDimensionOutputGroupDescriptor();
		multiDimensionOutputGroupDescriptor.Name = (string.IsNullOrEmpty(chartTitle) ? ("MultiDimensionChart  " + GroupSetDescCollection.Count) : chartTitle);
		multiDimensionOutputGroupDescriptor.xName = xAxisName;
		multiDimensionOutputGroupDescriptor.yName = yAxisName;
		multiDimensionOutputGroupDescriptor.zName = zAxisName;
		if (string.IsNullOrEmpty(testID))
		{
			multiDimensionOutputGroupDescriptor.CreateNewMultiDimensionResult();
		}
		else
		{
			multiDimensionOutputGroupDescriptor.CreateNewMultiDimensionResult(xAxisName, yAxisName, zAxisName, testID);
		}
		GroupSetDescCollection.Add(multiDimensionOutputGroupDescriptor);
		base.IsExpanded = true;
		multiDimensionOutputGroupDescriptor.IsForPPTChecked = true;
		multiDimensionOutputGroupDescriptor.PPTMode = (PPTMode ? true : false);
		ChangePPTSelectedStateFromChildren();
		multiDimensionOutputGroupDescriptor.IsSelected = true;
		ChangeSelectedStateFromChildren();
	}

	public void RemoveSelectedCharts()
	{
		RemoveCharts(checkStateToRemove: true);
	}

	private void RemoveCharts(bool checkStateToRemove)
	{
		if (GroupSetDescCollection != null)
		{
			GroupSetDescCollection.RemoveAll((GroupSetDescriptor grpDesc) => grpDesc.IsChecked == checkStateToRemove);
		}
	}

	internal void RemoveAllCharts()
	{
		if (GroupSetDescCollection != null)
		{
			GroupSetDescCollection.Clear();
		}
	}

	public List<GroupSetDescriptor> GetSelectedGroupSetDescriptors()
	{
		List<GroupSetDescriptor> list = new List<GroupSetDescriptor>();
		list.AddRange(GroupSetDescCollection.Where((GroupSetDescriptor histChrtDesc) => histChrtDesc.IsChecked == true));
		return list;
	}

	public override List<string> GetTestNames()
	{
		List<string> testNames = new List<string>();
		GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupDesc)
		{
			testNames.AddRange(groupDesc.GetTestNames());
		});
		return testNames;
	}

	internal override void ApplyFileGrouping(IEnumerable<string> groupNames)
	{
		GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupDesc)
		{
			groupDesc.ApplyFileGrouping(groupNames);
		});
	}

	internal override void RemoveFileGrouping(IEnumerable<string> groupNames)
	{
		GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupDesc)
		{
			groupDesc.RemoveFileGrouping(groupNames);
		});
	}

	public override IEnumerable<ResultDataSet> GetResultSets()
	{
		List<ResultDataSet> resultSets = new List<ResultDataSet>();
		GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupDesc)
		{
			resultSets.AddRange(groupDesc.GetResultSets());
		});
		return resultSets;
	}

	internal override void ApplyGroupPrefix(Discriminator discriminator)
	{
		throw new NotImplementedException();
	}

	internal void ApplyChartPrefixGrouping(ReportSettings reportSettingsInfo)
	{
		if (reportSettingsInfo.ChartDiscriminator != null)
		{
			GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupDesc)
			{
				groupDesc.PrefixDiscriminator(reportSettingsInfo.ChartDiscriminator);
			});
		}
		if (reportSettingsInfo.GroupDiscriminator != null)
		{
			GroupSetDescCollection.ForEach(delegate(GroupSetDescriptor groupDesc)
			{
				groupDesc.ApplyGroupPrefix(reportSettingsInfo.GroupDiscriminator);
			});
		}
	}
}
