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

using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.Validation;
using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;

using BS.Shared.Extensions;

using DevExpress.Xpf.Grid;

namespace BeWo.View.Detail
{
	public partial class AbsenceReasonView
	{
		private AbsenceReasonListVM _ViewModel;

		public AbsenceReasonView(AbsenceReasonListVM pViewModel)
		{
			InitializeComponent();
			combobox_brush.ItemTemplateSelector = new ComboBoxTemplateSelector(combobox_brush);
			ViewModel = pViewModel;
		}

		public override bool IsDirty
		{
			get { return ViewModel != null && ViewModel.IsDirty; }
		}

		public override string Title
		{
			get { return "Abwesenheitskategorien"; }
		}

		// TODO
		internal override DependencyObject ValidationOnSaveRootElement
		{
			get { return button_addReason; }
		}

		private AbsenceReasonListVM ViewModel
		{
			get { return _ViewModel; }

			set
			{
				_ViewModel = value;
				root.DataContext = value;
			}
		}

		protected override void Save()
		{
			var lToDos = new List<Action<ICustomerService>>();

			if (ViewModel.ChangedCount > 0)
			{
				lToDos.Add(s => s.UpdateAbsenceReasons(ViewModel.CommitChanged()));
			}

			if (ViewModel.AddedCount > 0)
			{
				lToDos.Add(s => s.InsertAbsenceReasons(ViewModel.CommitAdded()));
			}

			if (ViewModel.RemovedCount > 0)
			{
				lToDos.Add(s => s.DeactivateAbsenceReasons(ViewModel.GetRemoved().ToDictionary(dc => dc.AbsenceReasonOid.Value, dc => dc.AbsenceReasonVersion.Value)));
			}

			ServiceFacade.DoMultipleCustomerServicesAsync(lToDos, ReloadViewModel);
		}

		private void DeleteButton_Click(object sender, RoutedEventArgs e)
		{
			ViewModel.VMList.Remove(datagrid_absencereasons.GetCurrentValue<AbsenceReasonVM>());
			BeWoWpfUtils.RefreshDXGrid(datagrid_absencereasons);
		}

		private void ReloadViewModel()
		{
			VMFactory.CreateAbsenceReasonListVMAsync(r => this.Dispatch(delegate { ViewModel = r; }));
		}

		private void button_addReason_Click(object sender, RoutedEventArgs e)
		{
			if (ValidationTrigger.Validate(this))
			{
				ViewModel.AddNewVMToList();
			}
		}

		private void button_select_color_Click(object sender, RoutedEventArgs e)
		{
			popup_color.IsOpen = true;
		}

		private void colorEditor_Loaded(object sender, RoutedEventArgs e)
		{
			((ListBox) sender).ItemsSource = ViewModel.AllBrushes;
			((ListBox) sender).ItemTemplateSelector = new ComboBoxTemplateSelector((ListBox) sender);
		}

		private void listbox_colorpicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (e.AddedItems.Count == 1)
			{
				object selection = e.AddedItems[0];

				DataViewBase view = datagrid_absencereasons.View;

				RowData data = view.FocusedRowData;

				if (data != null && data.RowHandle != null)
				{
					int rowHandle = data.RowHandle.Value;

					var vm = datagrid_absencereasons.GetRow(rowHandle) as AbsenceReasonVM;

					if (vm != null)
					{
						vm.Brush = selection as SolidColorBrush;
					}
				}

				popup_color.IsOpen = false;
			}
		}
	}
}