using System;

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

namespace BeWo.ViewModel
{
    public class InvoiceVM : AbstractDCMapperVM<InvoiceDC>
    {
        #region Constants and Fields

        public static string PropertyName_Amount = "Amount";

        public static string PropertyName_Customer = "CustomerOid";

        public static string PropertyName_InvoiceDate = "InvoiceDate";

        public static string PropertyName_InvoiceNumber = "InvoiceNumber";

        public static string PropertyName_InvoicingPeriodEnd = "InvoicingPeriodEnd";

        public static string PropertyName_InvoicingPeriodStart = "InvoicingPeriodStart";

        public static string PropertyName_InvoicingPeriodString = "InvoicingPeriodString";

        public static string PropertyName_Notice = "Notice";

        public static string PropertyName_Organisation = "OrganisationOid";

        public static string PropertyName_PaidDate = "PaidDate";

        public static string PropertyName_Recipient = "Recipient";

        public static string PropertyName_RecipientAddress = "RecipientAddress";

        public static string PropertyName_RecipientName = "RecipientName";

        public static string PropertyName_SupportConcept = "SupportConceptOid";

        private decimal _Amount;

        private long? _CustomerOid;

        private DateTime? _InvoiceDate = DateTime.Now.Date;

        private string _InvoiceNumber;

        private DateTime? _InvoicingPeriodEnd;

        private DateTime? _InvoicingPeriodStart;

        private string _Notice;

        private long? _OrganisationOid;

        private DateTime? _PaidDate;

        private IInvoiceRecipient _Recipient;

        private string _RecipientAddress;

        private string _RecipientName;

        private long? _SupportConceptOid;

        #endregion

        #region Constructors and Destructors

        public InvoiceVM(InvoiceDC pDC) : base(pDC, !pDC.InvoiceOid.HasValue) {}

        #endregion

        #region Properties

        public decimal Amount
        {
            get
            {
                return this._Amount;
            }

            set
            {
                if (this.AreDifferent(this._Amount, value))
                {
                    this._Amount = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Amount, value), PropertyName_Amount);
                    this.FirePropertyChanged(PropertyName_Amount);
                }
            }
        }

        public long? CustomerOid
        {
            get
            {
                return this._CustomerOid;
            }

            set
            {
                if (this.AreDifferent(this._CustomerOid, value))
                {
                    this._CustomerOid = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.CustomerOid, value), PropertyName_Customer);
                    this.FirePropertyChanged(PropertyName_Customer);
                }
            }
        }

        public DateTime? InvoiceDate
        {
            get
            {
                return this._InvoiceDate;
            }

            set
            {
                if (this.AreDifferent(this._InvoiceDate, value))
                {
                    this._InvoiceDate = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.InvoiceDate, value), PropertyName_InvoiceDate);
                    this.FirePropertyChanged(PropertyName_InvoiceDate);
                }
            }
        }

        public string InvoiceNumber
        {
            get
            {
                return this._InvoiceNumber;
            }

            set
            {
                if (this.AreDifferent(this._InvoiceNumber, value))
                {
                    this._InvoiceNumber = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.InvoiceNumber, value), PropertyName_InvoiceNumber);
                    this.FirePropertyChanged(PropertyName_InvoiceNumber);
                }
            }
        }

        public DateTime? InvoicingPeriodEnd
        {
            get
            {
                return this._InvoicingPeriodEnd;
            }

            set
            {
                if (this.AreDifferent(this._InvoicingPeriodEnd, value))
                {
                    this._InvoicingPeriodEnd = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.InvoicingPeriodEnd, value), PropertyName_InvoicingPeriodEnd);
                    this.FirePropertyChanged(PropertyName_InvoicingPeriodEnd);
                    this.FirePropertyChanged(PropertyName_InvoicingPeriodString);
                }
            }
        }

        public DateTime? InvoicingPeriodStart
        {
            get
            {
                return this._InvoicingPeriodStart;
            }

            set
            {
                if (this.AreDifferent(this._InvoicingPeriodStart, value))
                {
                    this._InvoicingPeriodStart = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.InvoicingPeriodStart, value), PropertyName_InvoicingPeriodStart);
                    this.FirePropertyChanged(PropertyName_InvoicingPeriodStart);
                    this.FirePropertyChanged(PropertyName_InvoicingPeriodString);
                }
            }
        }

        public string InvoicingPeriodString
        {
            get
            {
                return this._InvoicingPeriodEnd.HasValue && this._InvoicingPeriodStart.HasValue
                           ? this._InvoicingPeriodStart.Value.ToShortDateString() + " - " + this._InvoicingPeriodEnd.Value.ToShortDateString()
                           : string.Empty;
            }

            set {}
        }

        public string Notice
        {
            get
            {
                return this._Notice;
            }

            set
            {
                if (this.AreDifferent(this._Notice, value))
                {
                    this._Notice = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.Notice, value), PropertyName_Notice);
                    this.FirePropertyChanged(PropertyName_Notice);
                }
            }
        }

        public long? OrganisationOid
        {
            get
            {
                return this._OrganisationOid;
            }

            set
            {
                if (this.AreDifferent(this._OrganisationOid, value))
                {
                    this._OrganisationOid = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.OrganisationOid, value), PropertyName_Organisation);
                    this.FirePropertyChanged(PropertyName_Organisation);
                }
            }
        }

        public DateTime? PaidDate
        {
            get
            {
                return this._PaidDate;
            }

            set
            {
                if (this.AreDifferent(this._PaidDate, value))
                {
                    this._PaidDate = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.PaidDate, value), PropertyName_PaidDate);
                    this.FirePropertyChanged(PropertyName_PaidDate);
                }
            }
        }

        public IInvoiceRecipient Recipient
        {
            get
            {
                return this._Recipient;
            }

            set
            {
                if (this.AreDifferent(this._Recipient, value))
                {
                    this._Recipient = value;

                    this.RecipientAddress = value.InvoiceAddressString;
                    this.RecipientName = value.Name;
                    this.Amount = value.DefaultAmount ?? 0;

                    if (value is CompactCostBearerDC)
                    {
                        this.OrganisationOid = value.Oid;
                    }
                    else if (value is CompactCustomerDC)
                    {
                        this.CustomerOid = value.Oid;
                    }

                    this.FirePropertyChanged(PropertyName_Recipient);
                }
            }
        }

        public string RecipientAddress
        {
            get
            {
                return this._RecipientAddress;
            }

            set
            {
                if (this.AreDifferent(this._RecipientAddress, value))
                {
                    this._RecipientAddress = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.RecipientAddress, value), PropertyName_RecipientAddress);
                    this.FirePropertyChanged(PropertyName_RecipientAddress);
                }
            }
        }

        public string RecipientName
        {
            get
            {
                return this._RecipientName;
            }

            set
            {
                if (this.AreDifferent(this._RecipientName, value))
                {
                    this._RecipientName = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.RecipientName, value), PropertyName_RecipientName);
                    this.FirePropertyChanged(PropertyName_RecipientName);
                }
            }
        }

        public long? SupportConceptOid
        {
            get
            {
                return this._SupportConceptOid;
            }

            set
            {
                if (this.AreDifferent(this._SupportConceptOid, value))
                {
                    this._SupportConceptOid = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.SupportConceptOid, value), PropertyName_SupportConcept);
                    this.FirePropertyChanged(PropertyName_SupportConcept);
                }
            }
        }

        #endregion

        #region Methods

        protected override void InitByDataContract(InvoiceDC pDataContract)
        {
            this._Amount = pDataContract.Amount;
            this._InvoiceDate = pDataContract.InvoiceDate;
            this._InvoiceNumber = pDataContract.InvoiceNumber;
            this._InvoicingPeriodStart = pDataContract.InvoicingPeriodStart;
            this._InvoicingPeriodEnd = pDataContract.InvoicingPeriodEnd;
            this._Notice = pDataContract.Notice;
            this._OrganisationOid = pDataContract.OrganisationOid;
            this._PaidDate = pDataContract.PaidDate;
            this._SupportConceptOid = pDataContract.SupportConceptOid;
            this._CustomerOid = pDataContract.CustomerOid;
            this._RecipientAddress = pDataContract.RecipientAddress;
            this._RecipientName = pDataContract.RecipientName;
        }

        protected override InvoiceDC MapToDataContract(InvoiceDC pDataContract, bool doCommit)
        {
            pDataContract.Amount = this._Amount;
            pDataContract.InvoiceDate = this._InvoiceDate;
            pDataContract.InvoiceNumber = this._InvoiceNumber;
            pDataContract.InvoicingPeriodEnd = this._InvoicingPeriodEnd;
            pDataContract.InvoicingPeriodStart = this._InvoicingPeriodStart;
            pDataContract.Notice = this._Notice;
            pDataContract.OrganisationOid = this._OrganisationOid;
            pDataContract.PaidDate = this._PaidDate;
            pDataContract.SupportConceptOid = this._SupportConceptOid;
            pDataContract.CustomerOid = this._CustomerOid;
            pDataContract.RecipientName = this._RecipientName;
            pDataContract.RecipientAddress = this._RecipientAddress;

            return pDataContract;
        }

        #endregion
    }
}