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.DataStructures.OutputDataStructure;

namespace QC.CTDS.DM.DataStructures.FormatDescriptorStructure;

[Serializable]
public class DiscriminatorElement : BindableObject
{
	private ObservableCollection<Discriminator> discriminator1 = new ObservableCollection<Discriminator>();

	private ObservableCollection<Discriminator> filter = new ObservableCollection<Discriminator>();

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

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

	public DiscriminatorElement()
	{
		Discriminator1.CollectionChanged += EstablishParentChildRelationOnCollectionChanged;
		Discriminator1.CollectionChanged += Discriminator1_CollectionChanged;
		Filter.CollectionChanged += EstablishParentChildRelationOnCollectionChanged;
		Filter.CollectionChanged += Filter_CollectionChanged;
	}

	public DiscriminatorElement(DiscriminatorElement element)
		: base(element)
	{
		discriminator1 = new ObservableCollection<Discriminator>();
		filter = new ObservableCollection<Discriminator>();
		foreach (Discriminator item in element.discriminator1)
		{
			discriminator1.Add(new Discriminator(item));
		}
		foreach (Discriminator item2 in element.filter)
		{
			filter.Add(new Discriminator(item2));
		}
	}

	private void Discriminator1_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		if (e.Action == NotifyCollectionChangedAction.Add)
		{
			foreach (Discriminator newItem in e.NewItems)
			{
				newItem.isFilter = false;
			}
		}
		OnPropertyChanged("Discriminator1");
	}

	private void Filter_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
	{
		if (e.Action == NotifyCollectionChangedAction.Add)
		{
			foreach (Discriminator newItem in e.NewItems)
			{
				newItem.isFilter = true;
			}
		}
		OnPropertyChanged("Filter");
	}

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

	public virtual void AddNewFilter()
	{
		Discriminator discriminator = new Discriminator();
		discriminator.id = "NewFilter" + (Filter.Count() + 1);
		Filter.Add(discriminator);
		discriminator.IsPicked = true;
	}

	public virtual void AddNewDiscriminator()
	{
		Discriminator discriminator = new Discriminator();
		discriminator.id = "NewDiscriminator" + (Discriminator1.Count() + 1);
		Discriminator1.Add(discriminator);
		discriminator.IsPicked = true;
	}

	public virtual void AddNewDiscriminator(string discId)
	{
		Discriminator discriminator = new Discriminator();
		discriminator.id = discId;
		Discriminator1.Add(discriminator);
		discriminator.IsPicked = true;
	}

	public IEnumerable<string> GetDistinctDiscriminatorNames()
	{
		List<string> list = new List<string>();
		for (int i = 0; i < Discriminator1.Count; i++)
		{
			list.Add(Discriminator1[i].id);
			list.AddRange(Discriminator1[i].GetDistinctDiscriminatorNames());
		}
		for (int j = 0; j < Filter.Count; j++)
		{
			list.Add(Filter[j].id);
			list.AddRange(Filter[j].GetDistinctDiscriminatorNames());
		}
		return list.Distinct();
	}

	public IEnumerable<string> GetDistinctDiscriminatorIDOrNames()
	{
		List<string> list = new List<string>();
		for (int i = 0; i < Discriminator1.Count; i++)
		{
			if (Discriminator1[i].visible)
			{
				list.Add(string.IsNullOrEmpty(Discriminator1[i].DiscriminatorName) ? Discriminator1[i].id : Discriminator1[i].DiscriminatorName);
			}
			list.AddRange(Discriminator1[i].GetDistinctDiscriminatorIDOrNames());
		}
		for (int j = 0; j < Filter.Count; j++)
		{
			if (Filter[j].visible)
			{
				list.Add(string.IsNullOrEmpty(Filter[j].DiscriminatorName) ? Filter[j].id : Filter[j].DiscriminatorName);
			}
			list.AddRange(Filter[j].GetDistinctDiscriminatorIDOrNames());
		}
		return list.Distinct();
	}

	public IEnumerable<DescriptorItem> GetDescriptorItems(int depthLevel = 1)
	{
		List<DescriptorItem> list = new List<DescriptorItem>();
		for (int i = 0; i < Discriminator1.Count; i++)
		{
			DescriptorItem descriptorItem = new DescriptorItem();
			descriptorItem.Name = Discriminator1[i].DiscriminatorName;
			descriptorItem.Id = Discriminator1[i].id;
			descriptorItem.IsString = Discriminator1[i].isString;
			descriptorItem.Value = Discriminator1[i].disValue;
			descriptorItem.Depth = depthLevel;
			descriptorItem.IsFilter = false;
			descriptorItem.IsVisible = Discriminator1[i].visible;
			DescriptorItem item = descriptorItem;
			list.Add(item);
			list.AddRange(Discriminator1[i].GetDescriptorItems(depthLevel + 1));
		}
		for (int j = 0; j < Filter.Count; j++)
		{
			DescriptorItem descriptorItem2 = new DescriptorItem();
			descriptorItem2.Name = Filter[j].DiscriminatorName;
			descriptorItem2.Id = Filter[j].id;
			descriptorItem2.IsString = Filter[j].isString;
			descriptorItem2.Value = Filter[j].disValue;
			descriptorItem2.Depth = depthLevel;
			descriptorItem2.IsFilter = true;
			descriptorItem2.IsVisible = Filter[j].visible;
			DescriptorItem item2 = descriptorItem2;
			list.Add(item2);
			list.AddRange(Filter[j].GetDescriptorItems(depthLevel + 1));
		}
		return list;
	}

	public void PrefixDiscriminator(Discriminator dis)
	{
		if (dis != null)
		{
			Discriminator discriminator = new Discriminator(dis);
			Discriminator discriminator2 = discriminator;
			while (discriminator2.Discriminator1.Count > 0)
			{
				discriminator2 = discriminator2.Discriminator1[0];
			}
			discriminator2.Discriminator1 = Discriminator1;
			discriminator2.Filter = Filter;
			ObservableCollection<Discriminator> observableCollection = new ObservableCollection<Discriminator>();
			observableCollection.Add(discriminator);
			Discriminator1 = observableCollection;
			Filter = new ObservableCollection<Discriminator>();
		}
	}
}
