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

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

using BS.Shared.Extensions;

using DevExpress.Xpf.Grid;


namespace BeWo.View.Detail
{
    public partial class AssessmentSheetCategoryView : BeWoView
    {
        private AssessmentSheetCategoryListVM _Viewmodel;

        public AssessmentSheetCategoryView(AssessmentSheetCategoryListVM viewModel)
        {
            this.InitializeComponent();
            this.ViewModel = viewModel;
        }

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

        public override string Title
        {
            get
            {
                return "Punktebogen Kategorien";
            }
        }

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

        private AssessmentSheetCategoryListVM ViewModel
        {
            get
            {
                return this._Viewmodel;
            }

            set
            {
                this.DataContext = value;
                this._Viewmodel = value;
                this._Viewmodel.VMList.Sort(AssessmentSheetCategoryVM.Compare);
            }
        }

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

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

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

            if (this.ViewModel.RemovedCount > 0)
            {
                lToDos.Add(s => s.DeactivateAssessmentSheetCategories(this.ViewModel.GetRemoved()));
            }

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

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            var lVM = this.datagrid_asc.GetCurrentValue<AssessmentSheetCategoryVM>();
            if (lVM != null)
            {
                this.ViewModel.VMList.Remove(lVM);
                BeWoWpfUtils.RefreshDXGrid(this.datagrid_asc);
            }
        }

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

            if (data != null && data.RowHandle != null)
            {
                AssessmentSheetCategoryVM vm = this.datagrid_asc.GetRow(data.RowHandle.Value) as AssessmentSheetCategoryVM;

                if (vm != null)
                {
                    this.ViewModel.PossibleValues.ForEach(i => i.IsEditSelected = vm.Values.Exists(j => j.AssessmentSheetValueOid.Equals(i.AssessmentSheetValueOid)));
                    this.ViewModel.EditVM = vm;
                    this.popup_values.IsOpen = true;
                }
            }
        }

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

        private void button_addAsc_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.NewVM.Values = this.ViewModel.PossibleValues.Where(i => i.IsNewSelected).ToList();
            this.ViewModel.PossibleValues.ForEach(i => i.IsNewSelected = false);
            this.ViewModel.AddNewVMToList();
            this._Viewmodel.VMList.Sort(AssessmentSheetCategoryVM.Compare);
        }

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

            this.popup_values.IsOpen = false;
        }

    }
}