using System;
using System.Collections.Generic;
using System.Linq;
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.Editors.Settings;
using DevExpress.Xpf.Grid;

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

        public RessourceView(ResourceListVM pViewModel)
        {
            this.InitializeComponent();
            this.combobox_brush.ItemTemplateSelector = new ComboBoxTemplateSelector(this.combobox_brush);
            this.ViewModel = pViewModel;

            ComboBoxEditSettings settings = new ComboBoxEditSettings();
            settings.ItemsSource = pViewModel.PossibleCategories;
            this.column_category.EditSettings = settings;

            // settings = new DevExpress.Xpf.Editors.Settings.ComboBoxEditSettings();
            // settings.ItemsSource = ViewModel.AllBrushes;
            // column_Color.EditSettings = settings;
        }

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

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

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

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

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

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

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

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

            if (this.ViewModel.RemovedCount > 0)
            {
                lToDos.Add(s => s.DeactivateResources(this.ViewModel.GetRemoved().ToDictionary(dc => dc.ResourceOid.Value, dc => dc.ResourceVersion.Value)));
            }

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

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

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

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

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

        private void categoryEditor_Loaded(object sender, RoutedEventArgs e)
        {
            ((ComboBox)sender).ItemsSource = this.ViewModel.PossibleCategories;
        }

        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];

                var view = this.datagrid_resources.View;

                RowData data = view.FocusedRowData;

                if (data != null && data.RowHandle != null)
                {
                    int rowHandle = data.RowHandle.Value;

                    ResourceVM vm = this.datagrid_resources.GetRow(rowHandle) as ResourceVM;

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

                this.popup_color.IsOpen = false;
            }
        }
    }
}