using System;
using System.Text;
using System.Windows.Media;

namespace BS.Shared.DataContracts
{
    public partial class FinanceOverviewItemDC
    {
        public string CostBearerName
        {
            get
            {
                if (this.CostBearer != null && this.CostBearer.Organisation != null)
                {
                    return this.CostBearer.Organisation.Name;
                }

                return null;
            }
        }

        public string CustomerName
        {
            get
            {
                if (this.SupportConcept != null)
                {
                    return this.SupportConcept.CustomerName;
                }

                return null;
            }
        }

        public string DaysOverrun
        {
            get
            {
                if (this.CostBearer != null)
                {
                    if (this.CostBearer.ApprovedEndDate != null)
                    {
                        if (DateTime.Now > this.CostBearer.ApprovedEndDate.Value)
                        {
                            TimeSpan ts = DateTime.Now.Subtract(this.CostBearer.ApprovedEndDate.Value);
                            return String.Format("Fällig seit {0:0.##} Tagen", ts.TotalDays);
                        }
                    }
                }

                return null;
            }
        }

        public decimal OpenAmount
        {
            get
            {
                return this.AmountTotal - this.AmountPaid;
            }
        }

        public Brush OpenAmountForegroundBrush
        {
            get
            {
                if (this.OpenAmount > 0)
                {
                    return new SolidColorBrush(Color.FromRgb(255, 90, 90));
                }
                else if (this.OpenAmount < 0)
                {
                    return new SolidColorBrush(Color.FromRgb(60, 200, 60));
                }

                return Brushes.White;
            }
        }

        public string OpenAmountString
        {
            get
            {
                if (this.SupportConcept != null)
                {
                    return String.Format("{0:c}", this.OpenAmount);
                }

                return null;
            }
        }

        public decimal PaidAmountPercentage
        {
            get
            {
                decimal p = 0;

                if (this.SupportConcept != null && this.AmountTotal != 0)
                {

                    p = (100 * this.AmountPaid) / this.AmountTotal;
                }

                return p;
            }
        }

        public string PaidAmountString
        {
            get
            {
                if (this.SupportConcept != null)
                {
                    return String.Format("{0:c}", this.AmountPaid);
                }

                return null;
            }
        }

        public string ReferenceNumber
        {
            get
            {
                if (this.CostBearer != null)
                {
                    return this.CostBearer.CustomerReferenceNumber;
                }

                return null;
            }
        }

        public string RemainingTimeString
        {
            get
            {
                if (this.SupportConcept != null)
                {
                    if (this.SupportConcept.EndDate != null)
                    {
                        TimeSpan ts = this.SupportConcept.EndDate.Value.Subtract(DateTime.Now);
                        return String.Format("{0:0.##}", ts.TotalDays / 30);
                    }
                }

                return null;
            }
        }

        public string SupportConceptTimeSpanString
        {
            get
            {
                if (this.CostBearer != null)
                {
                    DateTime? start = this.CostBearer.ApprovedStartDate;
                    DateTime? end = this.CostBearer.ApprovedEndDate;

                    StringBuilder sb = new StringBuilder();

                    if (start != null)
                    {
                        sb.Append(start.Value.ToShortDateString());
                    }

                    if (start != null || end != null)
                    {
                        sb.Append(" - ");
                    }

                    if (end != null)
                    {
                        sb.Append(end.Value.ToShortDateString());
                    }

                    return sb.ToString();
                }

                return null;
            }
        }

        public string TotalAmountString
        {
            get
            {
                if (this.SupportConcept != null)
                {
                    return String.Format("{0:c}", this.AmountTotal);
                }

                return null;
            }
        }
    }
}