using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

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

using BS.Shared.DataContracts;

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

		private Dictionary<ValueListEntryDC, List<ResourceDC>> sauce;
		public Dictionary<ValueListEntryDC, List<ResourceDC>> Sauce
		{
			get { return sauce; }

			set
			{
				sauce = value;
				listbox_result.ItemsSource = sauce;
			}
		}

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

		public List<ResourceDC> SelectedResources
		{
			get { return (List<ResourceDC>)GetValue(SelectedResourcesProperty); }
			set { SetValue(SelectedResourcesProperty, value); }
		}

		public List<ResourceDC> AusgewaehlteRessourcen
		{
			get { return SelectedResources; }
			set
			{
				SelectedResources = value; 
				OnPropertyChanged("AusgewaehlteRessourcen"); 
				CheckBoxConverter.SelektierteRessourcen = SelectedResources;
			}
		}

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

		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);
		}

		public ResourcesTreeSearchView()
		{
			InitializeComponent();
		}

		private void RessourcenTV_OnClick(object sender, RoutedEventArgs e)
		{
			var cb = (CheckBox)sender;
			var selectedResource = cb.DataContext as ResourceDC;

			if (CheckBoxConverter != null && selectedResource != null)
				CheckBoxConverter.AktuelleRessource = selectedResource;
		}

		private void ResourceCheckedEvent(object sender, RoutedEventArgs e)
		{
			var cb = (CheckBox) sender;
			var x = cb.Tag as ResourceDC;
			if (cb.IsChecked == null || x == null) return;
			
			if (cb.IsChecked.Value && !SelectedResources.Contains(x))
				SelectedResources.Add(x);
			else if (!cb.IsChecked.Value && SelectedResources.Contains(x))
				SelectedResources.Remove(x);

			CheckBoxConverter.SelektierteRessourcen = SelectedResources;

			RaiseSelectionChangedEventEvent();
		}

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