using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Text;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;

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

        public void SetReportDataSource(SettlementRO pRO)
        {
            if (pRO.DifferentHourlyRateCount == 1)
            {
	            cellZeitraum1.Text = String.Format("{0}-{1}", pRO.AccountingStartDateString, pRO.AccountingEndDateString);
            }
			else if (pRO.DifferentHourlyRateCount == 2)
			{
				cellZeitraum2.Text = String.Format("{0}-{1}", pRO.AccountingStartDateString, pRO.HourlyRateOldEndDateString);
				cellZeitraum1.Text = String.Format("{0}-{1}", pRO.HourlyRateNewStartDateString, pRO.AccountingEndDateString);
			}
			else
			{
				cellZeitraum3.Text = String.Format("{0}-{1}", pRO.AccountingStartDateString, pRO.HourlyRate3EndDateString);
				cellZeitraum2.Text = String.Format("{0}-{1}", pRO.HourlyRateOldStartDateString, pRO.HourlyRateOldEndDateString);
				cellZeitraum1.Text = String.Format("{0}-{1}", pRO.HourlyRateNewStartDateString, pRO.AccountingEndDateString);
			}

	        if (pRO.ClaimWithEigenbeteiligung < 0)
	        {
		        pRO.FinalSentence =
			        String.Format("Sie erhalten den überzahlten Betrag in Höhe von {0} auf Ihre Aufforderung hin zurück.",
				        pRO.Claim.Replace("-", "").Trim());
	        }
			else if (pRO.ClaimWithEigenbeteiligung > 0)
	        {
				pRO.FinalSentence = "Bitte überweisen Sie unsere Restforderung nach Prüfung der Schlussabrechnung auf das Ihnen angegebene Konto.";
	        }

	        pRO.ClaimWithEigenbeteiligung = pRO.AmountTotalWithHourlyRate3 + (pRO.AmountTotalWithOldHourlyRate ?? 0) + (pRO.AmountTotalWithNewHourlyRate ?? 0);

	        if (!String.IsNullOrEmpty(pRO.AbsenceTimes))
	        {
		        var cb2Sc = DAOFactory.GenericDAO.LoadByID<CostBearer2SupportConcept>(pRO.CostBearer2SupportConceptOid);
		        var customer = cb2Sc.SupportConcept.Customer;
		        var atList = customer.AbsenceTimes;

		        foreach (var absenceTime in atList)
		        {
			        pRO.AbsenceTimes = CreateAbsenceTimes(atList, pRO);
		        }
	        }
            this.bindingSource1.DataSource = pRO;
        }

		private string CreateAbsenceTimes(System.Collections.Generic.IList<AbsenceTime> atList,SettlementRO pRO)
		{
			if (atList != null)
			{
				StringBuilder sb = new StringBuilder();
				foreach (AbsenceTime at in atList)
				{
					if (at.AbsenceSpan != null && at.AbsenceReason != null && !String.IsNullOrEmpty(at.AbsenceReason.Description) && at.AbsenceReason.Description.ToLower().Contains("krank"))
					{
						bool cont = true;

						if (at.Start != null && at.Start > pRO.AccountingEndDate)
						{
							cont = false;
						}

						if (cont && at.End != null && at.End < pRO.AccountingStartDate)
						{
							cont = false;
						}

						if (cont)
						{
							if (sb.Length > 0)
							{
								sb.Append(", ");
							}

							if (at.Start.HasValue)
							{
								sb.Append(string.Format("{0:00}", at.Start.Value.Day));
								sb.Append(".");
								sb.Append(string.Format("{0:00}", at.Start.Value.Month));
								sb.Append(".");
								sb.Append(at.Start.Value.Year.ToString().Substring(2));

								if (at.End.HasValue)
								{
									sb.Append(" - ");
									sb.Append(string.Format("{0:00}", at.End.Value.Day));
									sb.Append(".");
									sb.Append(string.Format("{0:00}", at.End.Value.Month));
									sb.Append(".");
									sb.Append(at.End.Value.Year.ToString().Substring(2));
								}
								}

							sb.Append(" ");
							sb.Append(at.AbsenceReason.Description);

						}
					}
				}

				if (sb.Length > 0)
				{
					return "Abwesenheitszeiten: " + sb;
				}
			}
			return String.Empty;
		}
    }
}
