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.Extensions;

namespace BeWo.View.Detail
{
    /// <summary>
    /// Interaction logic for EmploymentTypeView.xaml
    /// </summary>
    public partial class EmploymentTypeView : BeWoView
    {
        private EmploymentTypeListVM _ViewModel;

        public EmploymentTypeView(EmploymentTypeListVM pViewModel)
        {
            this.InitializeComponent();
            this.ViewModel = pViewModel;
        }

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

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

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

            set
            {
                this._ViewModel = value;
                this.groupbox_root.DataContext = value;
                this.grid.ItemsSource = value.VMList;
            }
        }

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

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

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

            if (this.ViewModel.RemovedCount > 0)
            {
                lToDos.Add(s => s.DeactivateEmploymentTypes(this.ViewModel.GetRemoved().ToDictionary(v => v.EmploymentTypeOid.Value, v => v.EmploymentTypeVersion.Value)));
            }

            ServiceFacade.DoMultipleEmployeeServicesAsync(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<EmploymentTypeVM>();
            if (lVM != null)
            {
                this.ViewModel.VMList.Remove(lVM);
                BeWoWpfUtils.RefreshDXGrid(this.grid);
            }
        }

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

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

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