using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using BS.Shared.Core;
using BS.Shared.Extensions;

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

using DevExpress.Xpf.Editors.Settings;
using DevExpress.Xpf.Grid;

namespace BeWo.View.Detail
{
	public partial class ServiceDescriptionView : BeWoView
	{
		private ServiceDescriptionListVM _ViewModel;

		public ServiceDescriptionView(ServiceDescriptionListVM pViewModel)
		{
			InitializeComponent();

			combobox_categories.ItemTemplateSelector = new ComboBoxTemplateSelector(combobox_categories);

			column_category.EditSettings = new ComboBoxEditSettings {ItemsSource = pViewModel.PossibleCategories};

			column_interval.EditSettings = new ComboBoxEditSettings {ItemsSource = EnumTranslations.AccountingInterval2Translations, DisplayMember = "Value", ValueMember = "Key"};

			ViewModel = pViewModel;
		}

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

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

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

		private ServiceDescriptionListVM ViewModel
		{
			get { return _ViewModel; }

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

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

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

			if (ViewModel.RemovedCount > 0)
			{
				lToDos.Add(s => s.DeactivateServiceDescriptions(ViewModel.GetRemoved().ToDictionary(dc => dc.ServiceDescriptionOid.Value, dc => dc.ServiceDescriptionVersion.Value)));
			}

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

			ServiceFacade.DoMultipleOperationsServicesAsync(lToDos, ReloadViewModel);
		}

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

		private void ReloadViewModel()
		{
			Cache.GetInstance().ClearServiceCategoryDescriptions();
			VMFactory.CreateServiceDescriptionListVMAsync(r => this.Dispatch(delegate { ViewModel = r; }));
		}

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

		private void PART_Editor_Click(object sender, RoutedEventArgs e)
		{
			RowData data = datagrid_descriptions.View.FocusedRowData;

			if (data != null && data.RowHandle != null)
			{
				var vm = datagrid_descriptions.GetRow(data.RowHandle.Value) as ServiceDescriptionVM;

				if (vm != null)
				{
					ViewModel.EditVM = vm;
					popup_costrateperiods.IsOpen = true;
				}
			}
		}

		private void button_edit_Click(object sender, RoutedEventArgs e)
		{
			//this.ViewModel.EditVM.Values = this.ViewModel.PossibleValues.Where(i => i.IsEditSelected).ToList();

			popup_costrateperiods.IsOpen = false;
		}
	}
}