using System;
using System.Collections.Generic;
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 AssessmentSheetValueView : BeWoView
    {
        private AssessmentSheetValueListVM _Viewmodel;

        public AssessmentSheetValueView(AssessmentSheetValueListVM viewModel)
        {
            this.InitializeComponent();
            this.combobox_brush.ItemTemplateSelector = new ComboBoxTemplateSelector(this.combobox_brush);
            this.ViewModel = viewModel;
        }

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

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

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

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

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

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

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

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

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

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

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

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

        private void button_addAsv_Click(object sender, RoutedEventArgs e)
        {
            if (ValidationTrigger.Validate(this))
            {
                this.ViewModel.AddNewVMToList();
                this._Viewmodel.VMList.Sort(AssessmentSheetValueVM.Compare);
            }
        }

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

        private void colorEditor_Loaded(object sender, RoutedEventArgs e)
        {
            ((ListBox)sender).ItemsSource = this.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];
                RowData data = this.datagrid_asv.View.FocusedRowData;

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

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

                this.popup_color.IsOpen = false;
            }
        }

    }
}