using BeWo.ServiceProxy;
using BeWo.ViewModel;

using BS.Shared.Extensions;

namespace BeWo.View.Detail
{
    public partial class MandatorView : BeWoView
    {
        private MandatorVM _Viewmodel;

        public MandatorView(MandatorVM viewModel)
        {
            InitializeComponent();
            ViewModel = viewModel;
        }

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

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

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

        protected override void Save()
        {
            ServiceFacade.DoAccountingServiceAsync(
                s => s.UpdateInvoiceNumber(this.ViewModel.InvoiceNumber.CommitToDataContract()),
                    iv => ServiceFacade.DoOperationsServiceAsync(
                        s => s.UpdateMandator(this.ViewModel.CommitToDataContract()),
                            m => this.Dispatch(
                                delegate
                                {
                                    this.ViewModel = new MandatorVM(new InvoiceNumberVM(iv), m);
                                    BeWoApp.Mandator = m;
                                })));
        }
    }
}