using System.Collections.Generic;
using System.Linq;

using BS.Shared;

namespace BeWo.Data.Entities
{
    public class ServiceInvoice : BeWoEntityBase
    {
        public static string PropertyName_AmountAdvancePayments = "AmountAdvancePayments";

        public static string PropertyName_AmountEquityContribution = "AmountEquityContribution";

        public static string PropertyName_InvoiceBase = "InvoiceBase";

        public static string PropertyName_ServiceInvoicePeriodList = "ServiceInvoicePeriodList";

        public static string PropertyName_ServiceUnitOid = "ServiceUnitOid";

        private decimal? _AmountAdvancePayments;

        private decimal? _AmountEquityContribution;

        private InvoiceBase _InvoiceBase;

        private IList<ServiceInvoicePeriod> _ServiceInvoicePeriodList;

        private long? _ServiceUnitOid;

        public ServiceInvoice()
        {
            this._Tid = TableID.ServiceInvoice;
        }

        public virtual decimal? AmountAdvancePayments
        {
            get
            {
                return this._AmountAdvancePayments;
            }

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

        public virtual decimal? AmountEquityContribution
        {
            get
            {
                return this._AmountEquityContribution;
            }

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

        public virtual InvoiceBase InvoiceBase
        {
            get
            {
                return this._InvoiceBase ?? (this._InvoiceBase = new InvoiceBase());
            }

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

        public virtual IList<ServiceInvoicePeriod> ServiceInvoicePeriodList
        {
            get
            {
                return this._ServiceInvoicePeriodList ?? (this._ServiceInvoicePeriodList = new List<ServiceInvoicePeriod>());
            }

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

        public virtual long? ServiceUnitOid
        {
            get
            {
                return this._ServiceUnitOid;
            }

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

        public virtual decimal? GetTotalClaim()
        {
            var res = this._ServiceInvoicePeriodList.Sum(i => i.Claim);
            if (this.AmountAdvancePayments.HasValue)
            {
                res -= this.AmountAdvancePayments;
            }

            if (this.AmountEquityContribution.HasValue)
            {
                res -= this.AmountEquityContribution;
            }

            return res;
        }
    }
}