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 MultiEmployeeSearch : INotifyPropertyChanged
	{
		public static readonly DependencyProperty SelectedResourcesProperty = DependencyProperty.Register("SelectedEmployees", typeof(ObservableCollection<CompactEmployeeDC>), typeof(MultiEmployeeSearch));
		public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MultiEmployeeSearch));
		public static readonly DependencyProperty ResultListBackgroundProperty = DependencyProperty.Register("ResultListBackground", typeof(Brush), typeof(MultiEmployeeSearch), new UIPropertyMetadata(Brushes.White, (s, e) => ((MultiEmployeeSearch)s).listbox_result.Background = e.NewValue as Brush));

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

			set
			{
				sauce = value;
				AngezeigteMitarbeiter = sauce;
			}
		}

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

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

		public ObservableCollection<CompactEmployeeDC> SelectedEmployees
		{
			get { return (ObservableCollection<CompactEmployeeDC>)GetValue(SelectedResourcesProperty); }
			set { SetValue(SelectedResourcesProperty, value); }
		}

		private ObservableCollection<CompactEmployeeDC> _AngezeigteMitarbeiter = new ObservableCollection<CompactEmployeeDC>();
		public ObservableCollection<CompactEmployeeDC> AngezeigteMitarbeiter
		{
			get
			{
				return _AngezeigteMitarbeiter;
			}
			set
			{
				_AngezeigteMitarbeiter = value;
				OnPropertyChanged("AngezeigteMitarbeiter");
			} 
		}

		public List<CompactEmployeeDC> AusgewaehlteMitarbeiter
		{
			get { return SelectedEmployees.ToList(); }
			set
			{
				SelectedEmployees = value.ToObservableCollection();
				OnPropertyChanged("AusgewaehlteMitarbeiter");
				CheckBoxConverter.SelektierteMitarbeiter = SelectedEmployees.ToList();
			}
		}

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

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

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

			CheckBoxConverter.SelektierteMitarbeiter = SelectedEmployees.ToList();

			RaiseSelectionChangedEventEvent();
		}

		private void EmployeeLB_OnClick(object sender, MouseButtonEventArgs e)
		{
			var cb = (CheckBox)sender;
			var selectedEmployee = cb.DataContext as CompactEmployeeDC;

			if (CheckBoxConverter != null && selectedEmployee != null)
				CheckBoxConverter.AktuellerMitarbeiter = selectedEmployee;
		}


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

		public event PropertyChangedEventHandler PropertyChanged;

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

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

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

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

			SelectedEmployees.Clear();

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

			CheckBoxConverter.SelektierteMitarbeiter = SelectedEmployees.ToList();

			OnPropertyChanged("AusgewaehlteMitarbeiter");

			RaiseSelectionChangedEventEvent();
		}
	}
}
