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

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

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

using DevExpress.Xpf.Editors.Settings;

namespace BeWo.View.Detail
{
    public partial class SupportConceptGoalView : BeWoView
    {
        private SupportConceptGoalListVM _ViewModel;

        public SupportConceptGoalView(SupportConceptGoalListVM pViewModel)
        {
            this.InitializeComponent();

            // combobox_categories.ItemTemplateSelector = new ComboBoxTemplateSelector(combobox_categories);
            this.column_category.EditSettings = new ComboBoxEditSettings { ItemsSource = pViewModel.PossibleCategories };

            this.ViewModel = pViewModel;
        }

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

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

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

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

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

        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);
        }

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

        private void ReloadViewModel()
        {
            Cache.GetInstance().ClearValueListEntries(ValueListEntryType.SupportConceptGoalType);
            Cache.GetInstance().ClearValueListEntries(ValueListEntryType.SupportConceptGoalCategoryType);
            VMFactory.CreateSupportConceptGoalListVMAsync(ValueListEntryType.SupportConceptGoalType, ValueListEntryType.SupportConceptGoalCategoryType, r => this.Dispatch(delegate { this.ViewModel = r; }));
        }

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