using System;
using System.Collections.Generic;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BS.Shared.DataContracts;

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

        public void SetReportDataSource(SettlementRO pRO)
        {
			bool isOldVersion = true;

			if (pRO.HourlyRate3 == 0 && !pRO.HourlyRateOld.HasValue && !pRO.HourlyRateNew.HasValue)
				isOldVersion = false;
			if (pRO.InvoiceItems == null)
				pRO.InvoiceItems = new List<InvoiceItemDC>();

	        if (isOldVersion)
	        {
				pRO.InvoiceItems.Clear();
				if (pRO.DifferentHourlyRateCount == 1)
				{
					pRO.TotalApprovedFLSWithNewHourlyRate = Math.Round(pRO.ApprovedFLSWeekly * pRO.WeeksWithNewHourlyRate, 2);
					pRO.TotalApprovedFLSWithNewHourlyRateString = string.Format("{0:0.00}", pRO.TotalApprovedFLSWithNewHourlyRate);

					var ii = new InvoiceItemDC();
					ii.ItemDescription = String.Format("{0} - {1} {2} FLS x {3} € =", pRO.AccountingStartDateString,
						pRO.AccountingEndDateString, pRO.RecordedFLSWithNewHourlyRateString, pRO.HourlyRateNewString);

					ii.AmountTotal = pRO.AmountTotalWithNewHourlyRate;

					pRO.InvoiceItems.Add(ii);
				}

				else if (pRO.DifferentHourlyRateCount == 2)
				{
					var ii = new InvoiceItemDC();
					ii.ItemDescription = String.Format("{0} - {1} {2} FLS x {3} € =", pRO.AccountingStartDateString,
						pRO.HourlyRateOldEndDateString, pRO.RecordedFLSWithOldHourlyRateString, pRO.HourlyRateOldString);

					ii.AmountTotal = pRO.AmountTotalWithOldHourlyRate;

					pRO.InvoiceItems.Add(ii);

					ii = new InvoiceItemDC();
					ii.ItemDescription = String.Format("{0} - {1} {2} FLS x {3} € =", pRO.HourlyRateNewStartDateString,
						pRO.AccountingEndDateString, pRO.RecordedFLSWithNewHourlyRateString, pRO.HourlyRateNewString);

					ii.AmountTotal = pRO.AmountTotalWithNewHourlyRate;

					pRO.InvoiceItems.Add(ii);
				}
				else if (pRO.DifferentHourlyRateCount == 3)
				{
					var ii = new InvoiceItemDC();
					ii.ItemDescription = String.Format("{0} - {1} {2} FLS x {3} € =", pRO.AccountingStartDateString,
						pRO.HourlyRate3EndDateString, pRO.RecordedFLSWithHourlyRate3String, pRO.HourlyRate3String);

					ii.AmountTotal = pRO.AmountTotalWithHourlyRate3;

					pRO.InvoiceItems.Add(ii);

					ii = new InvoiceItemDC();
					ii.ItemDescription = String.Format("{0} - {1} {2} FLS x {3} € =", pRO.HourlyRateOldStartDateString,
						pRO.HourlyRateOldEndDateString, pRO.RecordedFLSWithOldHourlyRateString, pRO.HourlyRateOldString);

					ii.AmountTotal = pRO.AmountTotalWithOldHourlyRate;

					pRO.InvoiceItems.Add(ii);

					ii = new InvoiceItemDC();
					ii.ItemDescription = String.Format("{0} - {1} {2} FLS x {3} € =", pRO.HourlyRateNewStartDateString,
						pRO.AccountingEndDateString, pRO.RecordedFLSWithNewHourlyRateString, pRO.HourlyRateNewString);

					ii.AmountTotal = pRO.AmountTotalWithNewHourlyRate;

					pRO.InvoiceItems.Add(ii);
				}    
	        }
	        else
	        {
				foreach (var ii in pRO.InvoiceItems)
				{
					ii.ItemDescription = String.Format("{0:dd.MM.yyyy} - {1:dd.MM.yyyy}: {2:0.00} FLS x {3:c} =", ii.ItemPeriodStart,
						ii.ItemPeriodEnd, ii.UnitCount, ii.AmountPerUnit);
				}

				var id = GetCustomerId(pRO);
				if (id == "LWLNEU")
				{
					pRO.RateFactorText = null;
					pRO.RateFactorText2 = null;
				}
	        }

	        this.bindingSource1.DataSource = pRO;
        }

		private String GetCustomerId(SettlementRO pRO)
		{
			var cb2sc = DAOFactory.GenericDAO.LoadByID<CostBearer2SupportConcept>(pRO.CostBearer2SupportConceptOid);
			return cb2sc.CostBearer.ID;
		}
    }
}
