using CustomFilterRow_WPF;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using GridViewColumn = Telerik.Windows.Controls.GridViewColumn;

namespace CustomFilterRow_WPF
{
	public class ColumnFilterCell : UserControl, INotifyPropertyChanged
	{
		private GridViewColumn column;

		public ColumnFilterCell()
		{
			this.DataContext = this;
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public GridViewColumn Column
		{
			get
			{
				return this.column;
			}
			set
			{
				if (value != this.column)
				{
					this.column = value;
					this.OnPropertyChanged("Column");
				}
			}
		}

		public Telerik.Windows.Data.FilterDescriptor FilterDescriptor
		{
			get
			{
				return (Telerik.Windows.Data.FilterDescriptor)this.GetValue(FilterDescriptorProperty);
			}
			set
			{
				this.SetValue(FilterDescriptorProperty, value);
			}
		}

		public static readonly DependencyProperty FilterDescriptorProperty =
			DependencyProperty.Register("FilterDescriptor",
				typeof(Telerik.Windows.Data.FilterDescriptor), typeof(ColumnFilterCell),
				new System.Windows.PropertyMetadata(null, OnFilterDescriptorChanged)
			);

		private static void OnFilterDescriptorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((ColumnFilterCell)d).OnFilterDescriptorChanged((Telerik.Windows.Data.FilterDescriptor)e.OldValue, (Telerik.Windows.Data.FilterDescriptor)e.NewValue);
		}

		protected virtual void OnFilterDescriptorChanged(Telerik.Windows.Data.FilterDescriptor oldValue, Telerik.Windows.Data.FilterDescriptor newValue)
		{
		}

		public IEnumerable<RadMenuItem> FilterOperators
		{
			get
			{
				List<RadMenuItem> filterOperatorMenuItems = new List<RadMenuItem>();

				filterOperatorMenuItems.Add(this.CreateMenu("No Filter"));

				foreach (FilterOperator filterOperator in ApplicableFilterOperators)
				{
					filterOperatorMenuItems.Add(CreateMenu(filterOperator));
				}

				return filterOperatorMenuItems;
			}
		}

		protected virtual IEnumerable<FilterOperator> ApplicableFilterOperators
		{
			get
			{
				return new FilterOperator[]
                {
                    FilterOperator.IsLessThan,
		            FilterOperator.IsLessThanOrEqualTo,
		            FilterOperator.IsEqualTo,
		            FilterOperator.IsNotEqualTo,
		            FilterOperator.IsGreaterThanOrEqualTo,
		            FilterOperator.IsGreaterThan
                };
			}
		}

		private bool isFilterActive;

		public bool IsFilterActive
		{
			get
			{
				return this.isFilterActive;
			}
			protected set
			{
				if (this.isFilterActive != value)
				{
					this.isFilterActive = value;
					OnPropertyChanged("IsFilterActive");
					OnPropertyChanged("FilterVisibility");
				}
			}
		}

		public Visibility FilterVisibility
		{
			get
			{
				if (this.IsFilterActive)
				{
					return Visibility.Visible;
				}
				else
				{
					return Visibility.Collapsed;
				}
			}
		}

		private RadMenuItem CreateMenu(FilterOperator filterOperator)
		{
			RadMenuItem menu = CreateMenu(filterOperator.ToString());
			menu.Tag = filterOperator;

			return menu;
		}

		private RadMenuItem CreateMenu(string name)
		{
			RadMenuItem menu = new RadMenuItem();
			menu.Header = name;
			menu.Click += OnMenuItemClick;

			return menu;
		}

		private void OnMenuItemClick(object sender, RadRoutedEventArgs e)
		{
			RadMenuItem clickedMenuItem = (RadMenuItem)sender;
			if (clickedMenuItem.Tag != null)
			{
				this.FilterDescriptor.Operator = (FilterOperator)clickedMenuItem.Tag;
				this.IsFilterActive = true;
			}
			else
			{
				this.IsFilterActive = false;
			}
		}

		protected void OnPropertyChanged(string propertyName)
		{
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
