using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

using BeWo.Annotations;
using BeWo.Scheduler.Converter;

using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;

namespace BeWo.View.Search
{
	public partial class MultiCustomerSearchView : INotifyPropertyChanged
	{
		public static readonly DependencyProperty SelectedCustomersProperty = DependencyProperty.Register("SelectedCustomers", typeof(ObservableCollection<CompactCustomerDC>), typeof(MultiCustomerSearchView));
		public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MultiCustomerSearchView));
		public static readonly DependencyProperty ResultListBackgroundProperty = DependencyProperty.Register("ResultListBackground", typeof(Brush), typeof(MultiCustomerSearchView), new UIPropertyMetadata(Brushes.White, (s, e) => ((MultiCustomerSearchView)s).listbox_result.Background = e.NewValue as Brush));

		private ObservableCollection<CompactCustomerDC> sauce;
		public ObservableCollection<CompactCustomerDC> Sauce
		{
			get { return sauce; }

			set
			{
				sauce = value;
				AngezeigteKlienten = sauce;
			}
		}

		public ObservableCollection<CompactCustomerDC> SelectedCustomers
		{
			get { return (ObservableCollection<CompactCustomerDC>)GetValue(SelectedCustomersProperty); }
			set { SetValue(SelectedCustomersProperty, value); }
		}

		private ObservableCollection<CompactCustomerDC> _AngezeigteKlienten = new ObservableCollection<CompactCustomerDC>();
		public ObservableCollection<CompactCustomerDC> AngezeigteKlienten
		{
			get
			{
				return _AngezeigteKlienten;
			}
			set
			{
				_AngezeigteKlienten = value;
				OnPropertyChanged("AngezeigteKlienten");
			}
		}

		public List<CompactCustomerDC> AusgewaehlteKlienten
		{
			get { return SelectedCustomers.ToList(); }
			set
			{
				SelectedCustomers = value.ToObservableCollection();
				
				OnPropertyChanged("AusgewaehlteKlienten");
				CheckBoxConverter.SelektierteKlienten = SelectedCustomers.ToList();
			}
		}

		public Brush ResultListBackground
		{
			set { SetValue(ResultListBackgroundProperty, value); }
		}

		public CheckBoxConverter CheckBoxConverter
		{
			get { return Resources["CheckBoxConverter"] as CheckBoxConverter; }
		}

		public MultiCustomerSearchView()
		{
			DataContext = this;
			InitializeComponent();
		}

		private void SuchTextBox_OnPreviewKeyDown(object sender, TextChangedEventArgs e)
		{
			AngezeigteKlienten = Sauce.Where(a => a.DetailDescription.ToLower().Contains(SuchTextBox.Text.ToLower())).ToObservableCollection();
			OnPropertyChanged("AngezeigteKlienten");
		}

		private void CustomerCheckedEvent(object sender, RoutedEventArgs e)
		{
			var cb = (CheckBox)sender;
			var x = cb.Tag as CompactCustomerDC;
			if (cb.IsChecked == null || x == null) return;

			if (cb.IsChecked.Value && !SelectedCustomers.Contains(x))
				SelectedCustomers.Add(x);
			else if (!cb.IsChecked.Value && SelectedCustomers.Contains(x))
				SelectedCustomers.Remove(x);

			CheckBoxConverter.SelektierteKlienten = SelectedCustomers.ToList();

			RaiseSelectionChangedEventEvent();
		}

		public event RoutedEventHandler SelectionChanged
		{
			add { AddHandler(SelectionChangedEvent, value); }
			remove { RemoveHandler(SelectionChangedEvent, value); }
		}

		void RaiseSelectionChangedEventEvent()
		{
			var newEventArgs = new RoutedEventArgs(SelectionChangedEvent);
			RaiseEvent(newEventArgs);
		}

		public event PropertyChangedEventHandler PropertyChanged;

		[NotifyPropertyChangedInvocator]
		private void OnPropertyChanged(string propertyName)
		{
			var handler = PropertyChanged;
			if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
		}

		private void CustomerLB_OnClick(object sender, MouseButtonEventArgs e)
		{
			var cb = (CheckBox)sender;
			var selectedCustomer = cb.DataContext as CompactCustomerDC;

			if (CheckBoxConverter != null && selectedCustomer != null)
				CheckBoxConverter.AktuellerKlient = selectedCustomer;
		}

		private void AlleSelektierenCBUnChecked(object sender, RoutedEventArgs e)
		{
			var cb = (CheckBox)sender;

			SelectedCustomers.Clear();

			if(cb.IsChecked.HasValue && cb.IsChecked.Value)
				foreach(var item in Sauce)
					SelectedCustomers.Add(item);

			CheckBoxConverter.SelektierteKlienten = SelectedCustomers.ToList();

			OnPropertyChanged("AusgewaehlteKlienten");

			RaiseSelectionChangedEventEvent();
		}
	}
}
