using System;
using System.Windows.Media;

using BS.Shared.Extensions;

namespace BS.Shared.DataContracts
{
    partial class SupportConceptOverviewDC
    {
        public string BERecordedAndBilled
        {
            get
            {
                decimal diff = BERecordedTillNow - BEBilledTillNow;
                if (diff > 0)
                {
                    return string.Format("{0:0.00} (von {1:0.00})", BEBilledTillNow, BERecordedTillNow);
                }
                return string.Format("{0:0.00}", BEBilledTillNow);
            }
        }

        public string PassageStatus
        {
            get
            {
                decimal minpercent = -1;
                decimal maxpercent = -1;

                decimal sumPerYear = GetAppovedBEPerYear();
                sumPerYear = Math.Round(sumPerYear);
                if (sumPerYear == 99)
                {
                    minpercent = 8900m/99m;
                    maxpercent = 10900m/99m;
                }
                else if (sumPerYear == 120)
                {
                    minpercent = 11000m/120m;
                    maxpercent = 13300m/120m;
                }
                else if (sumPerYear == 147)
                {
                    minpercent = 13400m/147m;
                    maxpercent = 17200m/147m;
                }
                else if (sumPerYear == 198)
                {
                    minpercent = 17300m/198m;
                    maxpercent = 24300m/198m;
                }
                else if (sumPerYear == 288)
                {
                    minpercent = 24400m/288m;
                    maxpercent = 31500m/288m;
                }
                else if (sumPerYear == 343)
                {
                    minpercent = 31600m/343m;
                    maxpercent = 39600m/343m;
                }
                if (minpercent > 0)
                {
                    if (minpercent <= BEDifferenceApprovedRecordedTillNowInPercent &&
                        BEDifferenceApprovedRecordedTillNowInPercent <= maxpercent)
                    {
                        return "Innerhalb des Korridors";
                    }
                    return "Außerhalb des Korridors";
                }
                return null;
            }
        }

        private decimal GetAppovedBEPerYear()
        {
            decimal total = BEApprovedSum;
            DateTime? start = ApprovedStartDate;
            if (!start.HasValue)
                start = RequestedStartDate;
            DateTime? end = ApprovedEndDate;
            if (!end.HasValue)
                end = RequestedEndDate;

            if (start.HasValue && end.HasValue)
            {
                decimal days = (decimal)end.Value.Subtract(start.Value).Days + 1;
                decimal years = days / 365m;
	            if (years != 0)
	            {
		            return total/years;
	            }
            }

            return 0;
        }

        public Brush PassageStatusColor
        {
            get
            {
                var status = PassageStatus;
                if (status != null)
                {
                    if (status.Contains("Innerhalb"))
                        return Brushes.ForestGreen;
                    else
                        return Brushes.Red;
                }
                return null;
            }
        }

        public string HeaderString
        {
            get
            {
                var open = this.BEOpen;
                var openPerWeek = this.BEOpenPerWeek;
                var unit = "Stunden";

                if (this.ServiceUnit != null && this.ServiceUnit.CostRateValue.HasValue && !string.IsNullOrEmpty(this.ServiceUnit.UnitName))
                {
                    unit = this.ServiceUnit.UnitName;
                    if (this.ServiceUnit.CostRateValue != 60)
                    {
                        open = this.ServiceUnit.GetMinutesInBE(open);
                        openPerWeek = this.ServiceUnit.GetMinutesInBE(openPerWeek);
                    }
                }

                if (this.IsApproved && this.ApprovedStartDate.HasValue && this.ApprovedEndDate.HasValue)
                {
                    return string.Format("{0} - {1}   |   {2:0.00} Wochen verbleibend   |   {3:0.00} {4} bzw. {5:0.00} {4}/Woche verbleibend", this.ApprovedStartDate.Value.ToMonthSlashShortYear(), this.ApprovedEndDate.Value.ToMonthSlashShortYear(), this.OpenWeeks, open, unit, openPerWeek);
                }
                if (this.RequestedStartDate.HasValue && this.RequestedEndDate.HasValue)
                {
                    return string.Format("{0} - {1}   |   {2:0.00} Wochen verbleibend   |   {3:0.00} {4} bzw. {5:0.00} {4}/Woche verbleibend   |   Nicht bewilligt!",
                                         this.RequestedStartDate.Value.ToShortDateString(),
                                         this.RequestedEndDate.Value.ToShortDateString(),
                                         this.OpenWeeks,
                                         open,
                                         unit,
                                         openPerWeek);
                }
                return "Keine Zeitangaben   |   Nicht bewilligt!";
            }
        }

        public string Unit
        {
            get
            {
                if (this.ServiceUnit != null && this.ServiceUnit.CostRateValue.HasValue && !string.IsNullOrEmpty(this.ServiceUnit.UnitName))
                {
                    return this.ServiceUnit.UnitName;
                }

                return "Stunden";
            }
        }

	}
}