using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Data;

namespace TelerikSortingError
{
	public partial class CustomGridFilter : UserControl, IFilteringControl, INotifyPropertyChanged
	{
		private GridViewBoundColumnBase column;
		private CompositeFilterDescriptor compositeFilter;
		private FilterDescriptor rangeFilter;

		#region INotifyPropertyChanged

		public void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

		#region IsActive DependencyProperty 
		public bool IsActive
		{
			get { return (bool)GetValue(IsActiveProperty); }
			set { SetValue(IsActiveProperty, value); }
		}

		public static readonly DependencyProperty IsActiveProperty =
			DependencyProperty.Register(
				"IsActive",
				typeof(bool),
				typeof(CustomGridFilter),
				new PropertyMetadata(false));
		#endregion

		#region Aargang DependencyProperty 

		public int? Aargang
		{
			get { return (int?)GetValue(AargangProperty); }
			set { SetValue(AargangProperty, value); }
		}

		public static readonly DependencyProperty AargangProperty =
			DependencyProperty.Register(
				"Aargang",
				typeof(int?),
				typeof(CustomGridFilter),
				new PropertyMetadata(
					null,
					(sender, evt) => { ((CustomGridFilter)sender).OnPropertyChanged("Aargang"); }
				)
			);

		#endregion

		public CustomGridFilter()
		{
			InitializeComponent();

			DataContext = this;

			PropertyChanged += CustomGridFilter_PropertyChanged;
		}

		private void CustomGridFilter_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "Aargang" && Aargang > 1900 && Aargang < 2100)
			{
				OnFilter(null, null);
			}
		}

		public void Prepare(Telerik.Windows.Controls.GridViewColumn column)
		{
			this.column = column as GridViewBoundColumnBase;

			if (this.column == null)
			{
				return;
			}

			if (compositeFilter == null)
			{
				CreateFilters();
			}
		}

		private void CreateFilters()
		{
			string dataMember = column.DataMemberBinding.Path.Path;

			compositeFilter = new CompositeFilterDescriptor();

			rangeFilter = new FilterDescriptor(dataMember, FilterOperator.IsEqualTo, null);

			compositeFilter.FilterDescriptors.Add(rangeFilter);
		}

		private void OnFilter(object sender, RoutedEventArgs e)
		{
			rangeFilter.Value = new YearRange { Year = Aargang ?? 0 };

			if (!column.DataControl.FilterDescriptors.Contains(compositeFilter))
			{
				column.DataControl.FilterDescriptors.Add(compositeFilter);
			}

			IsActive = true;

			var popup = this.ParentOfType<System.Windows.Controls.Primitives.Popup>();
			if (popup != null)
			{
				popup.IsOpen = false;
			}
		}

		private void OnClear(object sender, RoutedEventArgs e)
		{
			if (column.DataControl.FilterDescriptors.Contains(compositeFilter))
			{
				column.DataControl.FilterDescriptors.Remove(compositeFilter);
			}

			Aargang = 0;

			IsActive = false;

			var popup = this.ParentOfType<System.Windows.Controls.Primitives.Popup>();
			if (popup != null)
			{
				popup.IsOpen = false;
			}
		}

	}
}
