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

using BeWo.ServiceProxy;
using BeWo.ViewModel;

using DevExpress.Xpf.Core;

namespace BeWo.View.Detail.Accounting
{
    public partial class ServiceInvoiceItemEditView : BeWoView
    {
        private ServiceInvoiceVM _Viewmodel;

        public ServiceInvoiceItemEditView()
        {
            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 ServiceInvoiceVM ViewModel
        {
            get
            {
                return this._Viewmodel;
            }

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

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

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

            ServiceFacade.DoMultipleAccountingServicesAsync(lToDos, () => this.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.InvoiceBase.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 GridControl_Loaded(object sender, RoutedEventArgs e)
        {
            //((TableView)((GridControl)sender).View).BestFitColumns();
        }

        private void TabControl_Loaded(object sender, RoutedEventArgs e)
        {
            ((DXTabControl)sender).SelectedIndex = 0;
        }

        private void gridView_Loaded(object sender, RoutedEventArgs e)
        {
            // var test = (sender as DevExpress.Xpf.Grid.GridViewBase).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;
            // }
            // }
        }
    }
}