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

namespace QC.CTDS.DM.DataStructures.LimitDescriptorDataStructure;

[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class LimitDefinition
{
	private ObservableCollection<LimitGroupType> limitGroupList = new ObservableCollection<LimitGroupType>();

	private ObservableCollection<OutputGroupLimitDescriptor> outputGroupLimitDescriptorSet = new ObservableCollection<OutputGroupLimitDescriptor>();

	private string filePath;

	[XmlElement("LimitGroup", Form = XmlSchemaForm.Unqualified)]
	public ObservableCollection<LimitGroupType> LimitGroupList
	{
		get
		{
			return limitGroupList;
		}
		set
		{
			limitGroupList = value;
		}
	}

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

	[XmlIgnore]
	public string FilePath
	{
		get
		{
			return filePath;
		}
		set
		{
			if (!(filePath == value))
			{
				filePath = value;
			}
		}
	}

	public static bool IsLimitDescriptorFile(string sourceFile)
	{
		return CommonFunctions.HasText(sourceFile, "<LimitDefinition");
	}

	public static LimitDefinition ReadLimitDescriptor(string filePath)
	{
		LimitDefinition limitDefinition = null;
		limitDefinition = CommonFunctions.Deserialize<LimitDefinition>(filePath);
		if (limitDefinition != null)
		{
			limitDefinition.FilePath = filePath;
		}
		return limitDefinition;
	}

	public void ApplyFileGrouping(FileSystemItemsGroupingCollection groupingCollection)
	{
		if (groupingCollection == null || groupingCollection.FileSystemItemsGroupList.Count <= 0)
		{
			return;
		}
		IEnumerable<string> groupNames = from fileGrp in groupingCollection.FileSystemItemsGroupList
			where !string.IsNullOrEmpty(fileGrp.GroupName)
			select fileGrp.GroupName;
		if (groupNames != null && groupNames.Count() > 0)
		{
			OutputGroupLimitDescriptorSet.ForEach(delegate(OutputGroupLimitDescriptor grpSet)
			{
				grpSet.ApplyFileGrouping(groupNames);
			});
		}
	}

	public void ApplyDataGrouping(List<Tuple<string, List<string>>> dataGroupingCollection)
	{
		if (dataGroupingCollection == null || dataGroupingCollection.Count <= 0)
		{
			return;
		}
		IEnumerable<string> groupNames = from dataGrp in dataGroupingCollection
			where !string.IsNullOrEmpty(dataGrp.Item1)
			select dataGrp.Item1;
		if (groupNames != null && groupNames.Count() > 0)
		{
			OutputGroupLimitDescriptorSet.ForEach(delegate(OutputGroupLimitDescriptor grpSet)
			{
				grpSet.ApplyFileGrouping(groupNames);
			});
		}
	}

	public void GetLimitDescParamNames(out List<string> paramNames)
	{
		paramNames = new List<string>();
		XDocument xDocument = null;
		if (!string.IsNullOrEmpty(FilePath))
		{
			xDocument = XDocument.Load(filePath);
		}
		else
		{
			string text = CommonFunctions.SerializeToString(this);
			xDocument = XDocument.Parse(text);
		}
		paramNames.AddRange((from attr in xDocument.Descendants("OutputGroupLimitDescriptor").Descendants("Filter").Attributes("id")
			select attr.Value).Distinct());
		paramNames.AddRange((from idxParam in xDocument.Descendants("OutputGroupLimitDescriptor").Descendants("Result").Descendants("Filter")
				.Attributes("id")
			select idxParam.Value).Distinct());
		paramNames = paramNames.Select((string pName) => pName.Trim()).Distinct().ToList();
	}
}
