using System;
using System.Collections.Generic;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using BeWo.Service.Invoicing;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;

namespace BeWo.IBPReports
{
	public partial class Spitzabrechnung : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<SettlementRO>
	{
		public Spitzabrechnung()
		{
			InitializeComponent();
		}

		public void SetReportDataSource(SettlementRO pRO)
		{
			decimal totalFLSBewilligt = 0;
			decimal totalFLSGeleistet = 0;
			decimal totalEURGesamt = 0;
			//decimal totalEURBewilligt = 0;

			//pRO.Approvals = new List<SettlementRO.ApprovalPeriodRO>();
			//var apsByHourlyRate = new Dictionary<decimal, IList<ApprovalPeriod>>();

			foreach (var ap in pRO.CostbearerApprovalPeriods)
			{
				//IList<ApprovalPeriod> list = null;
				//if (apsByHourlyRate.ContainsKey(ap.AmountPerUnit))
				//{
				//	list = apsByHourlyRate[ap.AmountPerUnit];
				//}
				//else
				//{
				//	list = new List<ApprovalPeriod>();
				//	apsByHourlyRate[ap.AmountPerUnit] = list;
				//}
				//list.Add(ap);
				totalFLSBewilligt += ap.ApprovedTotal;
			}

			//foreach (var hr in apsByHourlyRate.Keys)
			//{
			//	IList<ApprovalPeriod> list = apsByHourlyRate[hr];

			//	decimal flsTotal = 0;

			//	SettlementRO.ApprovalPeriodRO apRo = new SettlementRO.ApprovalPeriodRO();
			//	apRo.PeriodStartDate = DateTime.MaxValue;
			//	apRo.PeriodEndDate = DateTime.MinValue;

			//	foreach (var approvalPeriod in list)
			//	{
			//		if (approvalPeriod.PeriodStartDate < apRo.PeriodStartDate)
			//			apRo.PeriodStartDate = approvalPeriod.PeriodStartDate;
			//		if (approvalPeriod.PeriodEndDate > apRo.PeriodEndDate)
			//			apRo.PeriodEndDate = approvalPeriod.PeriodEndDate;

			//		flsTotal += approvalPeriod.ApprovedTotal;
			//	}
			//	apRo.AmountPerUnit = hr;
			//	apRo.MaxApprovedUnitCount = flsTotal;

			//	decimal a = Math.Round(flsTotal, 2) * hr;
			//	a = Math.Round(a, 2);
			//	apRo.MaxApprovedAmount = a;
				
			//	totalEURBewilligt += a;

			//	pRO.Approvals.Add(apRo);
			//}

			foreach (var ii in pRO.InvoiceItems)
			{
				totalFLSGeleistet += ii.UnitCount ?? 0;
				totalEURGesamt += ii.GrossAmountTotal ?? 0;
			}

			lblFLSGesamtBewilligt.Text = String.Format("{0:0.00}", totalFLSBewilligt);
			lblFLSErbracht.Text = String.Format("{0:0.00}", totalFLSGeleistet);
			lblEURGesamt.Text = String.Format("{0:c}", totalEURGesamt);
			//lblEURGesamtBewilligt.Text = String.Format("{0:c}", totalEURBewilligt);
			//lblFLSGesamtBewilligt2.Text = String.Format("{0:0.00}", totalFLSBewilligt);

			this.bindingSource1.DataSource = pRO;
		}
	}
}
