using BS.Shared.Settings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Media;

namespace BS.Shared.DataContracts
{
	public partial class SupportConceptTreeNodeDC : IFilterableDC, INotifyPropertyChanged
	{
		private bool _IsChecked;

		//private bool _IsCustomerWarningActive;

		//private bool _IsEmployeeWarningActive;

		private bool _IsExpanded;

        public bool ExpiresIn3MonthOrLess
		{
			get
			{
				bool expires = false;

				DateTime? date = null;
				if (SupportConceptCostBearerRelDC != null)
				{
					date = SupportConceptCostBearerRelDC.EndDate;
				}
				else if (SupportConcept != null)
				{
					date = SupportConcept.EndDate;
				}

				if (date != null)
				{
                   expires = date < DateTime.Now.AddMonths(ApplicationSettings.SupportConceptExpirationLimitInMonths);
				}

				if (!expires)
				{
					foreach (SupportConceptTreeNodeDC child in ChildNodes)
					{
						if (child.IsAboutToExpire)
						{
							expires = true;
						}
					}
				}

				return expires;
			}
		}

		// public Brush NodeForegroundBrush
		// {
		// get
		// {
		// return IsAboutToExpire ? Brushes.Red : Brushes.Black;
		// }

		// }

		// public bool IsCustomerWarningActive
		// {
		// get { return _IsCustomerWarningActive; }
		// set
		// {
		// _IsCustomerWarningActive = value;
		// RaisePropertyChanged("IsCustomerWarningActive");
		// }
		// }

		// public bool IsEmployeeWarningActive
		// {
		// get { return _IsEmployeeWarningActive; }
		// set
		// {
		// _IsEmployeeWarningActive = value;
		// RaisePropertyChanged("IsEmployeeWarningActive");
		// }
		// }

		// public string EmployeeWarning
		// {
		// get { return _EmployeeWarning; }
		// set
		// {
		// _EmployeeWarning = value;
		// RaisePropertyChanged("EmployeeWarning");
		// }
		// }

		// public string CustomerWarning
		// {
		// get { return _CustomerWarning; }
		// set
		// {
		// _CustomerWarning = value;
		// RaisePropertyChanged("CustomerWarning");
		// }
		// }

		public bool IsAboutToExpire
		{
			get
			{
				bool expires = false;

				DateTime? date = null;
				if (SupportConceptCostBearerRelDC != null)
				{
					date = SupportConceptCostBearerRelDC.EndDate;
				}
				else if (SupportConcept != null)
				{
					date = SupportConcept.EndDate;
				}

				if (date != null)
				{
					expires = date < DateTime.Now.AddDays(7); // TODO anz Monate einstellbar machen
				}

				if (!expires)
				{
					foreach (SupportConceptTreeNodeDC child in ChildNodes)
					{
						if (child.IsAboutToExpire)
						{
							expires = true;
						}
					}
				}

				return expires;
			}
		}

		public bool IsChecked
		{
			get { return _IsChecked; }

			set
			{
				_IsChecked = value;
				RaisePropertyChanged("IsChecked");
			}
		}

		public bool IsExpanded
		{
			get { return _IsExpanded; }

			set
			{
				_IsExpanded = value;
				RaisePropertyChanged("IsExpanded");
			}
		}

		public string NodeText
		{
			get
			{
				string desc = Description;
				if (SupportConcept != null && NodeType == NodeType.SupportConcept)
				{
					DateTime? start = SupportConcept.StartDate;
					DateTime? end = SupportConcept.EndDate;

					var sb = new StringBuilder();
					if (start != null)
					{
						sb.Append("\t HP vom ");
						sb.Append(start.Value.ToShortDateString());
						if (end != null)
						{
							sb.Append(" - ");
							sb.Append(end.Value.ToShortDateString());
						}
					}

					desc += sb.ToString();
				}
				else
				{
					if (Customer != null && (NodeType == NodeType.Customer_Female || NodeType == NodeType.Customer_Male))
					{
						desc = Customer.ToString();
					}
					else if (SupportConcept != null && NodeType == NodeType.SupportConcept)
					{
						desc = SupportConcept.ToString();
					}
					else if (CostBearer != null && NodeType == NodeType.CostBearer)
					{
						desc = CostBearer.Name;
					}
				}

				return desc;
			}
		}

		public SupportConceptTreeNodeDC Parent { get; set; }

		public SupportConceptTreeNodeDC RootNode { get; set; }

		public ActivationTypeId ActivationType
		{
			get
			{
				var actId = ActivationTypeId.Active;
				if (Customer != null)
				{
					actId = Customer.ActivationType;
				}

				if (actId == ActivationTypeId.Active && SupportConcept != null)
				{
					actId = SupportConcept.ActivationType;
				}

				return actId;
			}

			set { }
		}

		public string DetailDescription
		{
			get { return NodeText; }
		}

		public string FilterRelevants
		{
			get { return NodeText; }
		}

		public string IconPath
		{
			get { return string.Empty; }
		}

		public string SimpleDescription
		{
			get { return NodeText; }
		}

		public bool SupportsActivationType
		{
			get { return true; }
		}

		public long Version
		{
			get { return 0; }

			set { }
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public List<SupportConceptTreeNodeDC> GetFlattenedChilds()
		{
			var lResult = new List<SupportConceptTreeNodeDC>();
			AddFlattenedChilds(lResult);
			return lResult;
		}

		public void RecursivelySetParentOnChilds()
		{
			RootNode = this;
			RecursivelySetParentOnChilds(this);
		}

		public override string ToString()
		{
			return base.ToString();
		}

		internal SupportConceptTreeNodeDC Clone()
		{
			var clone = new SupportConceptTreeNodeDC();

			clone.CostBearer = CostBearer;
			clone.SupportConcept = SupportConcept;
			clone.SupportConceptCostBearerRelDC = SupportConceptCostBearerRelDC;
			clone.Customer = Customer;
			clone.Description = Description;
			clone.Employee = Employee;
			clone.NodeType = NodeType;
            
			var cloneChildNodes = new List<SupportConceptTreeNodeDC>();
			foreach (SupportConceptTreeNodeDC child in ChildNodes)
			{
				cloneChildNodes.Add(child.Clone());
			}

			clone.ChildNodes = cloneChildNodes;

			return clone;
		}

		private void AddFlattenedChilds(List<SupportConceptTreeNodeDC> pList)
		{
			foreach (SupportConceptTreeNodeDC iChild in ChildNodes)
			{
				pList.Add(iChild);
				iChild.AddFlattenedChilds(pList);
			}
		}

		private void RaisePropertyChanged(string p)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(p));
			}
		}

		private void RecursivelySetParentOnChilds(SupportConceptTreeNodeDC pRootNode)
		{
			foreach (SupportConceptTreeNodeDC iChild in ChildNodes)
			{
				iChild.Parent = this;
				iChild.RootNode = pRootNode;
				iChild.RecursivelySetParentOnChilds(pRootNode);
			}
		}

        public SolidColorBrush FilterableBrush { get { return new SolidColorBrush(Colors.Transparent); } }
	}
}