using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;

namespace ClearValidation_INotifyDataErrorInfo
{
	/// <summary>
	/// A football club.
	/// </summary>
	public class Club : INotifyPropertyChanged, INotifyDataErrorInfo, IEditableObject
	{
		public event PropertyChangedEventHandler PropertyChanged;

		private string name;
		private DateTime established;
		private int stadiumCapacity;

		public string Name
		{
			get { return this.name; }
			set
			{
				if (value != this.name)
				{
					this.name = value;
					this.OnPropertyChanged("Name");
				}
			}
		}

		public DateTime Established
		{
			get { return this.established; }
			set
			{
				if (value != this.established)
				{
					this.established = value;
					this.OnPropertyChanged("Established");
					this.ValidateEstablished();
				}
			}
		}

		private void ValidateEstablished()
		{
			var error = "Established date cannot be before 1800!";

			if (this.Established.Year < 1800)
			{
				this.AddError("Established", error);
			}
			else
			{
				this.RemoveError("Established", error);
			}
		}

		public int StadiumCapacity
		{
			get { return this.stadiumCapacity; }
			set
			{
				if (value != this.stadiumCapacity)
				{
					this.stadiumCapacity = value;
					this.OnPropertyChanged("StadiumCapacity");
					//this.ValidateStadiumCapacity();
				}
			}
		}

		private void ValidateStadiumCapacity()
		{
			var error = "Capacity cannot be smaller than 30,000!";

			if (this.StadiumCapacity < 30000)
			{
				this.AddError("StadiumCapacity", error);
			}
			else
			{
				this.RemoveError("StadiumCapacity", error);
			}
		}

		public Club()
		{
		}

		public Club(string name, DateTime established, int stadiumCapacity)
		{
			this.name = name;
			this.established = established;
			this.stadiumCapacity = stadiumCapacity;
		}

		
		protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
		{
			PropertyChangedEventHandler handler = this.PropertyChanged;
			if (handler != null)
			{
				handler(this, args);
			}
		}

		private void OnPropertyChanged(string propertyName)
		{
			this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
		}

		public override string ToString()
		{
			return this.Name;
		}

		public static ObservableCollection<Club> GetClubs()
		{
			ObservableCollection<Club> clubs = new ObservableCollection<Club>();
			Club club;

			// Liverpool
			club = new Club("Liverpool", new DateTime(1892, 1, 1), 45362);
			clubs.Add(club);

			// Manchester Utd.
			club = new Club("Manchester Utd.", new DateTime(1878, 1, 1), 76212);
			clubs.Add(club);

			// Chelsea
			club = new Club("Chelsea", new DateTime(1905, 1, 1), 42055);
			clubs.Add(club);

			// Arsenal
			club = new Club("Arsenal", new DateTime(1886, 1, 1), 60355);
			clubs.Add(club);

			return clubs;
		}


		private Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();

		public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

		private void OnDataErrorsChanged(string propertyName)
		{
			if (this.ErrorsChanged != null)
			{
				this.ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
			}
		}

		protected virtual void AddError(string propertyName, string errorMessage)
		{
			if (!this.errors.ContainsKey(propertyName))
			{
				this.errors.Add(propertyName, new List<String>());
			}

			if (!this.errors[propertyName].Contains(errorMessage))
			{
				this.errors[propertyName].Add(errorMessage);
				this.OnDataErrorsChanged(propertyName);
			}
		}

		protected virtual void RemoveError(string propertyName, string errorMessage)
		{
			if (this.errors.ContainsKey(propertyName))
			{
				if (this.errors[propertyName].Contains(errorMessage))
				{
					this.errors[propertyName].Remove(errorMessage);
					if (this.errors[propertyName].Count == 0)
					{
						this.errors.Remove(propertyName);
					}

					this.OnDataErrorsChanged(propertyName);
				}
			}
		}

		public System.Collections.IEnumerable GetErrors(string propertyName)
		{
			if (string.IsNullOrEmpty(propertyName) || !this.errors.ContainsKey(propertyName))
			{
				return null;
			}

			return this.errors[propertyName];
		}

		public bool HasErrors
		{
			get { return this.errors.Count > 0; }
		}
	
		public void BeginEdit()
		{
			errors.Clear();
			this.OnDataErrorsChanged(string.Empty);
		}

		public void CancelEdit()
		{
			
		}

		public void EndEdit()
		{
			this.ValidateStadiumCapacity();
		}

	
	}
}
