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 VivatgGmbHReports
{
    public partial class Spitzabrechnung : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<SettlementRO>
    {
        public Spitzabrechnung()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(SettlementRO pRO)
        {
	        bool isDefaultSpitzabrechnung = false;

	        if (pRO.InvoiceItems != null && pRO.InvoiceItems.Count == 1)
	        {
		        var ii = pRO.InvoiceItems[0];
				if ((ii.ItemDescription == "Spitzabrechung" || ii.ItemDescription == "Spitzabrechnung") && ii.GrossAmountTotal == null && ii.RateFactor == null)
		        {
			        isDefaultSpitzabrechnung = true;
		        }
	        }
	        if (!isDefaultSpitzabrechnung)
	        {
		        if (pRO.DifferentHourlyRateCount > 0 && pRO.HourlyRateNew.HasValue)
		        {
			        isDefaultSpitzabrechnung = true;
		        }
	        }

	        if (isDefaultSpitzabrechnung)
	        {
		        //UpdateAbrechnungsFelder(pRO);

		        pRO.InvoiceItems = CreateDefaultSpitzabrechnungInvoiceItems(pRO);
	        }
	        else
	        {
				if (pRO.InvoiceItems != null)
		        {
			        foreach (var ii in pRO.InvoiceItems)
			        {
				        ii.UnitDescription = String.Format("{0:dd.MM.yyyy} bis {1:dd.MM.yyyy}",
					        ii.ItemPeriodStart, ii.ItemPeriodEnd);
						ii.ItemDescription = String.Format("");
			        }
		        }
				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;
	    }

		private IList<InvoiceItemDC> CreateDefaultSpitzabrechnungInvoiceItems(SettlementRO pRO)
		{
			IList<InvoiceItemDC> itemList = new List<InvoiceItemDC>();
			if (pRO.DifferentHourlyRateCount == 3)
			{
				itemList.Add(new InvoiceItemDC
				{
					UnitDescription = String.Format("{0} bis {1}", pRO.AccountingStartDateString, pRO.HourlyRate3EndDateString),
					ItemDescription = String.Format("{0:0.##} FLS {1}x {2:c}",
						pRO.GeleisteteFLSMitStundensatz3, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRate3),

					GrossAmountTotal = pRO.GeleisteteFLSMitStundensatz3 * pRO.RateFactor * pRO.HourlyRate3
				});

				itemList.Add(new InvoiceItemDC
				{
					UnitDescription = String.Format("{0} bis {1}", pRO.HourlyRateOldStartDateString, pRO.HourlyRateOldEndDateString),
					ItemDescription = String.Format("{0:0.##} FLS {1}x {2:c}",
						pRO.GeleisteteFLSMitStundensatzAlt, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateOld),

					GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAlt * pRO.RateFactor * pRO.HourlyRateOld
				});

				itemList.Add(new InvoiceItemDC
				{
					UnitDescription = String.Format("{0} bis {1}", pRO.HourlyRateNewStartDateString, pRO.AccountingEndDateString),
					ItemDescription = String.Format("{0:0.##} FLS {1}x {2:c}",
						pRO.GeleisteteFLSMitStundensatzAktuell, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateNew),

					GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew
				});

			}
			else if (pRO.DifferentHourlyRateCount == 2)
			{
				itemList.Add(new InvoiceItemDC
				{
					UnitDescription = String.Format("{0} bis {1}", pRO.AccountingStartDateString, pRO.HourlyRateOldEndDateString),
					ItemDescription = String.Format("{0:0.##} FLS {1}x {2:c}",
						pRO.GeleisteteFLSMitStundensatzAlt, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateOld),

					GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAlt * (decimal)pRO.RateFactor * pRO.HourlyRateOld
				});

				itemList.Add(new InvoiceItemDC
				{
					UnitDescription = String.Format("{0} bis {1}", pRO.HourlyRateNewStartDateString, pRO.AccountingEndDateString),
					ItemDescription = String.Format("{0:0.##} FLS {1}x {2:c}",
						pRO.GeleisteteFLSMitStundensatzAktuell, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateNew),

					GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew
				});

			}
			else
			{
				itemList.Add(new InvoiceItemDC
				{
					UnitDescription = String.Format("{0} bis {1}", pRO.AccountingStartDateString, pRO.AccountingEndDateString),
					ItemDescription = String.Format("{0:0.##} FLS {1}x {2:c}",
						pRO.GeleisteteFLSMitStundensatzAktuell, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateNew),

					GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew
				});

			}

			return itemList;
		}

		//private IList<InvoiceItemDC> CreateDefaultSpitzabrechnungInvoiceItems(SettlementRO pRO)
		//{
		//	IList<InvoiceItemDC> itemList = new List<InvoiceItemDC>();
		//	if (pRO.DifferentHourlyRateCount == 3)
		//	{
		//		itemList.Add(new InvoiceItemDC
		//		{
		//			UnitDescription = String.Format("{0} bis {1}", pRO.AccountingStartDateString, pRO.HourlyRate3EndDateString),
		//			AmountPerUnit = pRO.HourlyRate3,
		//			UnitCount = pRO.GeleisteteFLSMitStundensatz3 * pRO.RateFactor,
		//			GrossAmountTotal = pRO.GeleisteteFLSMitStundensatz3 * pRO.RateFactor * pRO.HourlyRate3
		//		});

		//		itemList.Add(new InvoiceItemDC
		//		{
		//			UnitDescription = String.Format("{0} bis {1}", pRO.HourlyRateOldStartDateString, pRO.HourlyRateOldEndDateString),
		//			AmountPerUnit = pRO.HourlyRateOld,
		//			UnitCount = pRO.GeleisteteFLSMitStundensatzAlt * pRO.RateFactor,
		//			GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAlt * pRO.RateFactor * pRO.HourlyRateOld
		//		});

		//		itemList.Add(new InvoiceItemDC
		//		{
		//			UnitDescription = String.Format("{0} bis {1}", pRO.HourlyRateNewStartDateString, pRO.AccountingEndDateString),
		//			AmountPerUnit = pRO.HourlyRateNew,
		//			UnitCount = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor,
		//			GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew
		//		});

		//	}
		//	else if (pRO.DifferentHourlyRateCount == 2)
		//	{
		//		itemList.Add(new InvoiceItemDC
		//		{
		//			UnitDescription = String.Format("{0} bis {1}", pRO.AccountingStartDateString, pRO.HourlyRateOldEndDateString),
		//			AmountPerUnit = pRO.HourlyRateOld,
		//			UnitCount = pRO.GeleisteteFLSMitStundensatzAlt * pRO.RateFactor,
		//			GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAlt * (decimal)pRO.RateFactor * pRO.HourlyRateOld
		//		});

		//		itemList.Add(new InvoiceItemDC
		//		{
		//			UnitDescription = String.Format("{0} bis {1}", pRO.HourlyRateNewStartDateString, pRO.AccountingEndDateString),
		//			AmountPerUnit = pRO.HourlyRateNew,
		//			UnitCount = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor,
		//			GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew
		//		});

		//	}
		//	else
		//	{
		//		itemList.Add(new InvoiceItemDC
		//		{
		//			UnitDescription = String.Format("{0} bis {1}", pRO.AccountingStartDateString, pRO.AccountingEndDateString),
		//			AmountPerUnit = pRO.HourlyRateNew,
		//			UnitCount = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor,
		//			GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew
		//		});

		//	}

		//	return itemList;
		//}

		
    }
}
