using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

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

using BS.Shared.Extensions;

namespace BeWo.View.Detail.Accounting
{
    public partial class EquityInvoiceItemEditView : BeWoView
    {
        private InvoiceBaseVM _Viewmodel;

        public EquityInvoiceItemEditView()
        {
            this.InitializeComponent();

            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) => this.FireEditingFinished()));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, (s, e) => this.SaveData()));
        }

        public event EventHandler<EventArgs> EditingFinished;

        public InvoiceBaseVM ViewModel
        {
            get
            {
                return this._Viewmodel;
            }

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

        protected override void Save()
        {
            if (this.ViewModel.InvoiceItems == null || this.ViewModel.InvoiceItems.VMList.Count == 0)
            {
                MessageBox.Show("Bitte erstellen Sie mindestens einen Rechnungsposten", "Speichern nicht möglich", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            var lToDos = new List<Action<IAccountingService>>();
            var dc = new[] { this.ViewModel.CommitToDataContract() }.ToList();

            if (this.ViewModel.IsNew)
            {
                lToDos.Add(s => s.InsertNewInvoiceBases(dc));
            }
            else
            {
                lToDos.Add(s => s.UpdateInvoiceBases(dc));
            }

            ServiceFacade.DoMultipleAccountingServicesAsync(lToDos, FireEditingFinished);
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Soll der Rechnungsposten entfernt werden?", "Rechnungsposten entfernen", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                this.ViewModel.InvoiceItems.VMList.Remove(this.datagrid_invoiceItems.GetCurrentValue<InvoiceItemVM>());
                BeWoWpfUtils.RefreshDXGrid(this.datagrid_invoiceItems);
            }
        }

        private void FireEditingFinished()
        {
            if (this.EditingFinished != null)
            {
                this.EditingFinished(this, new EventArgs());
            }
        }

        private void gridView_Loaded(object sender, RoutedEventArgs e)
        {
            var test = this.gridView.GetAllChildren();

            foreach (var t in test)
            {
                if (t is TextBlock && ((TextBlock)t).Text.StartsWith("Click here to add"))
                {
                    ((TextBlock)t).Text = "Klicken Sie hier um einen Rechnugsposten hinzuzufügen";
                    ((TextBlock)t).Foreground = Brushes.Black;
                    ((TextBlock)t).FontWeight = FontWeights.Bold;
                }
            }
        }
    }
}