using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace BS.Shared.DataContracts
{
    [DataContract]
    public class ServiceInvoiceDC : IDataContract
    {
        private InvoiceBaseDC _InvoiceBase;

        [DataMember]
        public decimal? AmountAdvancePayments { get; set; }

        [DataMember]
        public decimal? AmountEquityContribution { get; set; }

        [DataMember]
        public InvoiceBaseDC InvoiceBase
        {
            get { return _InvoiceBase ?? (_InvoiceBase = new InvoiceBaseDC()); }

            set { _InvoiceBase = value; }
        }

        public String RecipientPostBox
        {
            get
            {
                String result = null;

                if (InvoiceBase != null)
                {
                    var contactInfo = InvoiceBase.RecipientContactInformations.FirstOrDefault(f => f.ContactType.Equals(ContactType.business_POBox));

                    if (contactInfo != null)
                    {
                        result = contactInfo.ContactValue;
                    }
                }

                return result;
            }

            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    if (!InvoiceBase.RecipientContactInformations.Any(ci => ci.ContactType.Equals(ContactType.business_POBox)))
                    {
                        var contactInfo = new ContactDC {ContactType = ContactType.business_POBox, ContactValue = value};

                        InvoiceBase.RecipientContactInformations.Add(contactInfo);
                    }
                    else
                    {
                        var contactInfo = InvoiceBase.RecipientContactInformations.FirstOrDefault(f => f.ContactType.Equals(ContactType.business_POBox));
                        if (contactInfo != null)
                        {
                            contactInfo.ContactValue = value;
                        }
                    }
                }
            }
        }

        [DataMember]
        public long? ServiceInvoiceOid { get; set; }

        [DataMember]
        public List<ServiceInvoicePeriodDC> ServiceInvoicePeriods { get; set; }

        [DataMember]
        public long? ServiceInvoiceVersion { get; set; }

        [DataMember]
        public CostRatePeriodDC ServiceUnit { get; set; }

        public decimal? ClaimSum
        {
            get { return ServiceInvoicePeriods.Sum(i => i.Claim); }
        }

        public decimal? EndClaim
        {
            get
            {
                decimal? amount = ClaimSum;
                if (!amount.HasValue)
                    amount = 0;
                if (AmountAdvancePayments.HasValue)
                    amount -= AmountAdvancePayments.Value;
                if (AmountEquityContribution.HasValue)
                    amount -= AmountEquityContribution.Value;


                return amount;
            }
        }
    }
}