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

namespace QC.CTDS.DM.DataStructures.FormatDescriptorStructure;

[Serializable]
public class Discriminator : DiscriminatorElement, IDataErrorInfo
{
	private string discriminatorName;

	private string _id;

	private string _disValue;

	private bool _visible = true;

	private bool _isString;

	private bool _optional;

	private bool _exclude;

	private bool _isFilter;

	private bool isExpanded = true;

	private bool isPicked;

	[XmlAttribute(AttributeName = "name")]
	[DefaultValue("")]
	public string DiscriminatorName
	{
		get
		{
			return discriminatorName;
		}
		set
		{
			if (!(discriminatorName == value))
			{
				discriminatorName = value;
				OnPropertyChanged("DiscriminatorName");
			}
		}
	}

	[XmlAttribute]
	public string id
	{
		get
		{
			return _id;
		}
		set
		{
			if (!(_id == value))
			{
				_id = value.Trim();
				OnPropertyChanged("id");
			}
		}
	}

	[XmlAttribute(AttributeName = "value")]
	public string disValue
	{
		get
		{
			return _disValue;
		}
		set
		{
			if (!(_disValue == value))
			{
				_disValue = value;
				OnPropertyChanged("disValue");
			}
		}
	}

	[XmlIgnore]
	public bool visible
	{
		get
		{
			return _visible;
		}
		set
		{
			if (_visible != value)
			{
				_visible = value;
				OnPropertyChanged("visible");
			}
		}
	}

	[XmlAttribute(AttributeName = "visible")]
	[DefaultValue("true")]
	public string visibleVal
	{
		get
		{
			return visible.ToString().ToLower();
		}
		set
		{
			bool result = false;
			bool.TryParse(value, out result);
			visible = result;
		}
	}

	[XmlIgnore]
	public bool isString
	{
		get
		{
			return _isString;
		}
		set
		{
			if (_isString != value)
			{
				_isString = value;
				base.OnPropertyChanged("isString");
			}
		}
	}

	[XmlAttribute(AttributeName = "isString")]
	[DefaultValue("false")]
	public string isStringVal
	{
		get
		{
			return isString.ToString().ToLower();
		}
		set
		{
			bool result = false;
			bool.TryParse(value, out result);
			isString = result;
		}
	}

	[DefaultValue(false)]
	[XmlAttribute(AttributeName = "optional")]
	public bool Optional
	{
		get
		{
			return _optional;
		}
		set
		{
			if (_optional != value)
			{
				_optional = value;
				base.OnPropertyChanged("Optional");
			}
		}
	}

	[DefaultValue(false)]
	[XmlAttribute]
	public bool exclude
	{
		get
		{
			return _exclude;
		}
		set
		{
			if (_exclude != value)
			{
				_exclude = value;
				base.OnPropertyChanged("exclude");
			}
		}
	}

	[XmlIgnore]
	public bool isFilter
	{
		get
		{
			return _isFilter;
		}
		set
		{
			if (_isFilter != value)
			{
				_isFilter = value;
				base.OnPropertyChanged("isFilter");
			}
		}
	}

	[XmlIgnore]
	public bool IsExpanded
	{
		get
		{
			return isExpanded;
		}
		set
		{
			if (isExpanded != value)
			{
				isExpanded = value;
				SetChildrenDiscriminatorsExpandedState();
				OnPropertyChanged("IsExpanded");
			}
		}
	}

	[XmlIgnore]
	public bool IsPicked
	{
		get
		{
			return isPicked;
		}
		set
		{
			if (isPicked != value)
			{
				isPicked = value;
				OnPropertyChanged("IsPicked");
			}
		}
	}

	public string Error
	{
		get
		{
			throw new NotImplementedException();
		}
	}

	public string this[string columnName]
	{
		get
		{
			string result = null;
			if (columnName == "id" && string.IsNullOrEmpty(id))
			{
				result = "Loop condition cannot be null or empty.";
			}
			return result;
		}
	}

	public Discriminator()
	{
	}

	public Discriminator(Discriminator dis)
		: base(dis)
	{
		discriminatorName = dis.discriminatorName;
		_id = dis._id;
		_disValue = dis._disValue;
		_visible = dis._visible;
		_isString = dis._isString;
		_exclude = dis._exclude;
		_optional = dis._optional;
		isFilter = dis.isFilter;
		isExpanded = dis.isExpanded;
		isPicked = dis.isPicked;
	}

	public override void Changed(string changedFieldName)
	{
		if (changedFieldName == "IsPicked" || changedFieldName == "IsExpanded")
		{
			if (base.Parent != null)
			{
				base.Parent.Changed(changedFieldName);
			}
		}
		else
		{
			base.Changed(changedFieldName);
		}
	}

	private void SetChildrenDiscriminatorsExpandedState()
	{
		base.Discriminator1.ForEach(delegate(Discriminator discriminator)
		{
			discriminator.IsExpanded = IsExpanded;
		});
		base.Filter.ForEach(delegate(Discriminator discriminator)
		{
			discriminator.IsExpanded = IsExpanded;
		});
	}

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