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

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

			pRO.ApprovedFLS = pRO.ApprovedFLS.Replace("FLS", "").Trim();
			pRO.RecordedFLS = pRO.RecordedFLS.Replace("FLS", "").Trim();

	        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)
	        {
		        pRO.InvoiceItems = CreateDefaultSpitzabrechnungInvoiceItems(pRO);
	        }
	        else
	        {
				var id = GetCustomerId(pRO);
				if (id == "LWLNEU")
				{
					pRO.RateFactorText = null;
					pRO.RateFactorText2 = null;

					if (pRO.InvoiceItems != null)
					{
						foreach (var ii in pRO.InvoiceItems)
						{
							ii.ItemDescription = String.Format("{0:dd.MM.yyyy} bis {1:dd.MM.yyyy}: {2:0.##} FLS x {3:c}",
								ii.ItemPeriodStart, ii.ItemPeriodEnd, ii.UnitCount, ii.AmountPerUnit);
						}
					}
				}
				else
				{
					if (pRO.InvoiceItems != null)
					{
						foreach (var ii in pRO.InvoiceItems)
						{
							ii.ItemDescription = String.Format("{0:dd.MM.yyyy} bis {1:dd.MM.yyyy}: {2:0.##} FLS {3}x {4:c}",
								ii.ItemPeriodStart, ii.ItemPeriodEnd,
								ii.UnitCount, ii.RateFactor == 0 ? "" : "x " + pRO.RateFactorText2 + " ",
								ii.AmountPerUnit);
						}
					}
				}
	        }
	        
	        bindingSource1.DataSource = pRO;
        }

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

		private static IList<InvoiceItemDC> CreateDefaultSpitzabrechnungInvoiceItems(SettlementRO pRO)
		{
			IList<InvoiceItemDC> itemList = new List<InvoiceItemDC>();
			if (pRO.DifferentHourlyRateCount == 3) // 3 Zeiträume -----------------------------------------------------------------------------
			{
				itemList.Add(new InvoiceItemDC
				{
					ItemDescription = String.Format("({0}-{1})", pRO.AccountingStartDateString.Replace(".20", "."), pRO.HourlyRate3EndDateString.Replace(".20", ".")),
                    GrossAmountTotal = pRO.GeleisteteFLSMitStundensatz3 * pRO.RateFactor * pRO.HourlyRate3,
                    UnitCount        = pRO.GeleisteteFLSMitStundensatz3,
                    RateFactor       = pRO.RateFactor,
                    AmountPerUnit    = pRO.HourlyRate3,
                    Notice = "abzurechnende FLS"
				});

				itemList.Add(new InvoiceItemDC
				{
					ItemDescription = String.Format("({0}-{1})", pRO.HourlyRateOldStartDateString.Replace(".20", "."), pRO.HourlyRateOldEndDateString.Replace(".20", ".")),
                    GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAlt * pRO.RateFactor * pRO.HourlyRateOld,
                    UnitCount        = pRO.GeleisteteFLSMitStundensatzAlt,
                    RateFactor       = pRO.RateFactor,
                    AmountPerUnit    = pRO.HourlyRateOld,
					Notice = ""
				});

				itemList.Add(new InvoiceItemDC
				{
					ItemDescription = String.Format("ab {0}", pRO.HourlyRateNewStartDateString.Replace(".20", ".")),
                    GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew,
                    UnitCount        = pRO.GeleisteteFLSMitStundensatzAktuell,
                    RateFactor       = pRO.RateFactor,
                    AmountPerUnit    = pRO.HourlyRateNew,
					Notice = ""
				});

			}
            else if (pRO.DifferentHourlyRateCount == 2) // 2 Zeiträume -----------------------------------------------------------------------------
			{
				itemList.Add(new InvoiceItemDC
				{
					ItemDescription = String.Format("({0}-{1})", pRO.AccountingStartDateString.Replace(".20", "."), pRO.HourlyRateOldEndDateString.Replace(".20", ".")),
                    GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAlt * pRO.RateFactor * pRO.HourlyRateOld,
                    UnitCount        = pRO.GeleisteteFLSMitStundensatzAlt,
                    RateFactor       = pRO.RateFactor,
                    AmountPerUnit    = pRO.HourlyRateOld,
                    Notice           = "abzurechnende FLS"
				});

				itemList.Add(new InvoiceItemDC
				{
					ItemDescription = String.Format("ab {0}", pRO.HourlyRateNewStartDateString.Replace(".20", ".")),
                    GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew,
                    UnitCount        = pRO.GeleisteteFLSMitStundensatzAktuell,
                    RateFactor       = pRO.RateFactor,
                    AmountPerUnit    = pRO.HourlyRateNew,
					Notice = ""
				});

			}
			else // -----------------------------------------------------------------------------------------------------------------------------
			{
				itemList.Add(new InvoiceItemDC
				{
					//ItemDescription = String.Format("({0}-{1})", pRO.HourlyRateNewStartDateString.Replace(".20", "."), pRO.AccountingEndDateString.Replace(".20", ".")),
					ItemDescription  = "",
					GrossAmountTotal = pRO.GeleisteteFLSMitStundensatzAktuell * pRO.RateFactor * pRO.HourlyRateNew,
                    UnitCount        = pRO.GeleisteteFLSMitStundensatzAktuell,
                    RateFactor       = pRO.RateFactor,
                    AmountPerUnit    = pRO.HourlyRateNew,
                    Notice           = "abzurechnende FLS"
				});

			}

			return itemList;
		}
    }
}
