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.Core;
using BS.Shared.Extensions;

using DevExpress.Xpf.Editors.Settings;

namespace BeWo.View.Detail
{
    public partial class ServiceCategoryView : BeWoView
    {
        private ServiceCategoryListVM _ViewModel;

        public ServiceCategoryView(ServiceCategoryListVM pViewModel)
        {
            InitializeComponent();
            ViewModel = pViewModel;
        
            column_visibility.EditSettings = new ComboBoxEditSettings { ItemsSource = EnumTranslations.Accountingvisibility2Translations, DisplayMember = "Value", ValueMember = "Key" };

            //if (combo_approvedvisibility.SelectedIndex == -1)
            //    combo_approvedvisibility.SelectedIndex = 1;

            column_interval.EditSettings = new ComboBoxEditSettings { ItemsSource = EnumTranslations.AccountingInterval2Translations, DisplayMember = "Value", ValueMember = "Key" };
        }

        public override bool IsDirty
        {
            get
            {
                return ViewModel != null && ViewModel.IsDirty;
            }
        }

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

        // TODO
        internal override DependencyObject ValidationOnSaveRootElement
        {
            get
            {
                return button_addCategory;
            }
        }

        private ServiceCategoryListVM ViewModel
        {
            get
            {
                return _ViewModel;
            }

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

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

            if (ViewModel.RemovedCount > 0)
            {
                lToDos.Add(s => s.DeactivateServiceCategories(ViewModel.GetRemoved().ToDictionary(dc => dc.ServiceCategoryOid.Value, dc => dc.ServiceCategoryVersion.Value)));
            }

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

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

            ServiceFacade.DoMultipleOperationsServicesAsync(lToDos, ReloadViewModel);
        }

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

        private void ReloadViewModel()
        {
            Cache.GetInstance().ClearServiceCategoryDescriptions();
            VMFactory.CreateServiceCategoryListVMAsync(r => this.Dispatch(delegate { ViewModel = r; }));
        }

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

        private void PART_Editor_Click(object sender, RoutedEventArgs e)
        {
            var data = datagrid_categories.View.FocusedRowData;

            if (data != null && data.RowHandle != null)
            {
                var vm = datagrid_categories.GetRow(data.RowHandle.Value) as ServiceCategoryVM;

                if (vm != null)
                {
                    ViewModel.EditVM             = vm;
                    popup_costrateperiods.IsOpen = true;
                }
            }
        }

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