using System;
using System.ComponentModel;
using System.Linq;
using BS.Shared;
using BeWo.Validation;

using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;

namespace BeWo.ViewModel
{
    public class ServiceAccountingVM : AbstractDCMapperVM<ServiceAccountingDC>
    {
        

        public static string PropertyName_BillableAmount = "BillableAmount";

        public static string PropertyName_AccountingInterval = "AccountingInterval";

        public static string PropertyName_IsDefault = "IsDefault";

        public static string PropertyName_ServiceDescription = "ServiceDescription";

        
        private decimal? _BillableAmount;

        private AccountingIntervalType _AccountingInterval;

        private ServiceDescriptionVM _ServiceDescription;

        private bool _IsDefault;

        public ServiceAccountingVM(ServiceAccountingDC pDC)
            : base(pDC, !pDC.ServiceAccountingOid.HasValue)
        {
        }

        public decimal? BillableAmount
        {
            get { return this._BillableAmount; }

            set
            {
                if (this.AreDifferent(this._BillableAmount, value))
                {
                    this._BillableAmount = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.BillableAmount, value), PropertyName_BillableAmount);
                    this.FirePropertyChanged(PropertyName_BillableAmount);
                }
            }
        }

        public AccountingIntervalType AccountingInterval
        {
            get { return this._AccountingInterval; }

            set
            {
                if (this.AreDifferent(this._AccountingInterval, value))
                {
                    this._AccountingInterval = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.AccountingInterval, value), PropertyName_AccountingInterval);
                    this.FirePropertyChanged(PropertyName_AccountingInterval);
                }
            }
        }

        public ServiceDescriptionVM ServiceDescription
        {
            get { return this._ServiceDescription; }

            set
            {
                if (this.AreDifferent(this._ServiceDescription, value))
                {
                    this._ServiceDescription = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ServiceDescription, value), PropertyName_ServiceDescription);
                    this.FirePropertyChanged(PropertyName_ServiceDescription);
                }
            }
        }

        public bool IsDefault
        {
            get { return this._IsDefault; }

            set
            {
                if (this.AreDifferent(this._IsDefault, value))
                {
                    this._IsDefault = value;
                    this.StoreDirtyInformation(true, PropertyName_IsDefault);
                    this.FirePropertyChanged(PropertyName_IsDefault);
                }
            }
        }

        protected override void InitByDataContract(ServiceAccountingDC pDataContract)
        {
            this._BillableAmount = pDataContract.BillableAmount;
            this._AccountingInterval = pDataContract.AccountingInterval;
            this._IsDefault = pDataContract.IsDefault;
            if (pDataContract.ServiceDescription != null)
                this._ServiceDescription = new ServiceDescriptionVM(pDataContract.ServiceDescription);
            else
                this._ServiceDescription = new ServiceDescriptionVM(new ServiceDescriptionDC());

        }

        protected override ServiceAccountingDC MapToDataContract(ServiceAccountingDC pDataContract, bool doCommit)
        {
            pDataContract.BillableAmount = this._BillableAmount;
            pDataContract.AccountingInterval = this._AccountingInterval;
            pDataContract.IsDefault = this._IsDefault;
            pDataContract.ServiceDescription = this._ServiceDescription.CommitToDataContract();

            return pDataContract;
        }

        public override bool IsDirty
        {
            get { return base.IsDirty || ServiceDescription.IsDirty; }
        }
    }
}