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

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

using BS.Shared;
using BS.Shared.Core;
using BS.Shared.Extensions;

namespace BeWo.View.Detail
{
    /// <summary>
    /// Interaction logic for EmploymentTypeView.xaml
    /// </summary>
    public partial class DokumentationstypView : BeWoView
    {
        private ValueListEntryListVM _ViewModel;
       
        public DokumentationstypView(ValueListEntryListVM pViewModel)
        {
            this.InitializeComponent();
			
			this.ViewModel = pViewModel;
        }

        public override bool IsDirty
        {
            get
            {
                return this.ViewModel != null ? this.ViewModel.IsDirty : false;
            }
        }

        public override string Title
        {
            get
            {
                return EnumTranslations.ValueListEntryTypeTranslations[this.ViewModel.EntryType];
            }
        }

        public ValueListEntryType Type
        {
            get
            {
                return this.ViewModel.EntryType;
            }
        }

        internal override DependencyObject ValidationOnSaveRootElement
        {
            get
            {
                return this.button_add;
            }
        }

        private ValueListEntryListVM ViewModel
        {
            get
            {
                return this._ViewModel;
            }

            set
            {
                this._ViewModel = value;
				if (this._ViewModel != null)
				{
					this._ViewModel.VMList.Sort((a, b) => a.DataContract.ValueListEntryOid.Value.CompareTo(b.DataContract.ValueListEntryOid.Value));
				}
                this.groupbox_root.DataContext = value;
                this.grid.DataSource = value.VMList;
            }
        }

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

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

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

            if (this.ViewModel.RemovedCount > 0)
            {
                lToDos.Add(s => s.DeactivateValueListEntries(this.ViewModel.GetRemoved().ToDictionary(v => v.ValueListEntryOid.Value, v => v.ValueListEntryVersion.Value)));
            }

            ServiceFacade.DoMultipleValueListServicesAsync(lToDos, this.ReloadViewModel);
        }

        // public override void ExportToExcel(string pFileID)
        // {
        // var lContent = listbox_ValueList.ItemsSource.Cast<ValueListEntryDC>()
        // .Select(lEntry => new List<string> { lEntry.TypeDescription })
        // .ToList();

        // ServiceFacade.DoDownloadService(s => s.PrepareExcelFile(pFileID, new List<string> { ViewModel.HeaderText }, lContent));
        // }

        // public override bool CanExcelExport
        // {
        // get { return listbox_ValueList.Items.Count > 0; }
        // }
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            var lVM = this.grid.GetCurrentValue<ValueListEntryVM>();
            if (lVM != null)
            {
                this.ViewModel.VMList.Remove(lVM);
                BeWoWpfUtils.RefreshDXGrid(this.grid);
            }
        }

        private void ReloadViewModel()
        {
            Cache.GetInstance().ClearValueListEntries(this.ViewModel.EntryType);
            VMFactory.CreateValueListVMAsync(this.ViewModel.EntryType, r => this.Dispatch(delegate { this.ViewModel = r; }));
        }

        private void button_add_Click(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel.VMList.Count < 5)
            {
                // if (ValidationTrigger.Validate(this))
                this.ViewModel.AddNewVMToList();
            }
            else {
                MessageBox.Show("Es können maximal fünf Dokumentationstypen angelegt werden.", "Hinzufügen nicht möglich", MessageBoxButton.OK, MessageBoxImage.Information);
            }

            // grid.DataSource = null;
            // grid.DataSource = ViewModel.VMList;
        }
    }
}