using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;

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

using BS.Shared.Extensions;

namespace BeWo.View.Detail
{
    public partial class InvoiceView : BeWoView
    {
        private InvoiceListVM _Viewmodel;

        public InvoiceView(InvoiceListVM invoiceVM)
        {
            this.InitializeComponent();
            this.ViewModel = invoiceVM;

            this.pu_edit.CommandBindings.Add(
                new CommandBinding(
                    ApplicationCommands.Close, 
                    (s, e) =>
                        {
                            this.pu_edit.BindingGroup.CancelEdit();
                            this.CloseEditPopUp();
                        }));

            this.pu_edit.CommandBindings.Add(
                new CommandBinding(
                    ApplicationCommands.Save, 
                    (s, e) =>
                        {
                            this.pu_edit.BindingGroup.CommitEdit();
                            this.CloseEditPopUp();
                            this.SaveData();
                        }));
        }

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

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

            set
            {
                this.DataContext = value;
                this._Viewmodel = value;
            }
        }

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

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

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

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

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

        private void CloseEditPopUp()
        {
            this.pu_edit.IsOpen = false;
            this.MainControl.ChangePopUpOpacityMode(false);
        }

        private void ReloadViewModel()
        {
            VMFactory.CreateInvoiceListVMAsync(
                r => this.Dispatch(
                    delegate
                        {
                            this.ViewModel = r;
                            BindingOperations.GetMultiBindingExpression(this.combo_customer, Selector.SelectedValueProperty).UpdateSource();
                        }));
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Sind Sie sicher, dass Sie den gewählten Eintrag löschen möchten?", "Rechnung löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                this.ViewModel.VMList.Remove(BeWoWpfUtils.GetTag<InvoiceVM>(sender));
                this.SaveData();
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.EditVM = BeWoWpfUtils.GetTag<InvoiceVM>(sender);
            this.pu_edit.IsOpen = true;
            this.MainControl.ChangePopUpOpacityMode(true);
        }

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