using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using BS.Shared.Extensions;
using BeWo.Report.ReportObjects;
using BeWo.Report;

namespace BeWo.FibMarburg
{
    public partial class Hilfeplanuebersicht : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<SupportConceptListRO>
    {
        public Hilfeplanuebersicht()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(SupportConceptListRO pRO)
        {
	        foreach (var sc in pRO.SupportConceptDetailList)     
	        {
		        sc.FLSTotalRecordedBillable *= 1.13m;

		        sc.FLSRemaining = sc.FLSTotal - sc.FLSTotalRecordedBillable;

		        sc.FLSPerWeekIst *= 1.13m;

				if (sc.FLSPerWeekSoll != 0)
				{
					sc.DurationPercentage = (sc.FLSPerWeekIst * 100) / sc.FLSPerWeekSoll;
				}

				if (sc.OpenWeeks <= 0)
				{
					sc.FLSOpenPerWeek = 0;
				}
				else
				{
					sc.FLSOpenPerWeek = sc.FLSRemaining / sc.OpenWeeks;
				}

		        sc.PassageStatus = GetPassageStatus(sc);

	        }
            this.bindingSource1.DataSource = pRO;
        }

		private string GetPassageStatus(SupportConceptListRO.SupportConceptDetail sc)
		{
			decimal minpercent = -1;
			decimal maxpercent = -1;

			decimal sumPerYear = GetAppovedBEPerYear(sc);
			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 <= sc.DurationPercentage &&
					sc.DurationPercentage <= maxpercent)
				{
					return "Innerhalb des Korridors";
				}
				return "Außerhalb des Korridors";
			}
			return null;
		}

		private decimal GetAppovedBEPerYear(SupportConceptListRO.SupportConceptDetail sc)
		{
			decimal total = sc.FLSTotal;
		
			decimal days = (decimal)sc.EndDate.Subtract(sc.StartDate).Days + 1;
			decimal years = days / 365m;
			if (years != 0)
			{
				return total / years;
			}
		
			return 0;
		}
    }
}
