using BeWo.ServiceProxy;
using BeWo.ViewModel;

using BS.Shared.Extensions;

namespace BeWo.View.Detail
{
    public partial class InvoiceNumberView : BeWoView
    {
        private InvoiceNumberVM _Viewmodel;

        public InvoiceNumberView(InvoiceNumberVM viewModel)
        {
            this.InitializeComponent();
            this.ViewModel = viewModel;
        }

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

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

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

        protected override void Save()
        {
            ServiceFacade.DoAccountingServiceAsync(s => s.UpdateInvoiceNumber(this.ViewModel.CommitToDataContract()), this.ReloadViewModel);
        }

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