using System;
using System.Collections.Generic;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using BS.Shared;
using BS.Shared.DataContracts;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;

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

        public void SetReportDataSource(SettlementRO pRO)
        {

	        if (pRO.TerminationDate.HasValue)
	        {
		        lblTermination.Text = String.Format("{0:dd.MM.yyyy} {1}", pRO.TerminationDate, pRO.TerminationReason);
	        }
	        bool isOldVersion = true;

	        if (pRO.HourlyRate3 == 0 && !pRO.HourlyRateOld.HasValue && !pRO.HourlyRateNew.HasValue)
		        isOldVersion = false;

            if (pRO.ClaimWithEigenbeteiligung < 0)
				lblClaimText.Text = "Rückzahlungsverpflichtung";

	        if (!isOldVersion && pRO.ApprovedFLSWeekly == 0 && pRO.Approvals != null && pRO.Approvals.Count > 0)
	        {
		        var app = pRO.Approvals[0];
		        if (app.ApprovalUnit.HasValue && app.ApprovalUnit.Value == AccountingIntervalType.Weekly && app.ApprovedPerUnit.HasValue)
			        pRO.ApprovedFLSWeekly = app.ApprovedPerUnit.Value;
	        }
	        else
	        {
				pRO.ApprovedFLSWeekly *= (decimal)pRO.RateFactor;    
	        }
            
            pRO.ApprovedFLSWeekly = Math.Round(pRO.ApprovedFLSWeekly, 2, MidpointRounding.AwayFromZero);

            DateTime dt1;
            DateTime dt2;
            DateTime.TryParse(pRO.AccountingStartDateString, out dt1);
            DateTime.TryParse(pRO.AccountingEndDateString, out dt2);

            TimeSpan sp = dt2.Subtract(dt1);
            lblTage3.Text = String.Format("{0:0.#}", sp.TotalDays + 1);
            lblWeeks3.Text = String.Format("{0:0.##}", (sp.TotalDays + 1) * 0.1427);
            lblFLSTotal3.Text = String.Format("{0:0.##} FLS", Math.Round((sp.TotalDays + 1) * 0.1427, 2) * (double)pRO.ApprovedFLSWeekly);

	        String hourlyRateString = "";
			if (pRO.InvoiceItems == null)
				pRO.InvoiceItems = new List<InvoiceItemDC>();

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

		            if (hourlyRateString.Length > 0)
		            {
			            hourlyRateString += ", ";
		            }
					hourlyRateString += String.Format("{0:c}", ii.AmountPerUnit);
	            }
            }
	        else
	        {
				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} Std. x {3} € =", pRO.AccountingStartDateString,
						pRO.AccountingEndDateString, pRO.RecordedFLSWithNewHourlyRateString, pRO.HourlyRateNewString);

					ii.AmountTotal = pRO.AmountTotalWithNewHourlyRate;

					pRO.InvoiceItems.Add(ii);
					hourlyRateString = String.Format("{0} €", pRO.HourlyRateNewString);
				}

				else if (pRO.DifferentHourlyRateCount == 2)
				{
					var ii = new InvoiceItemDC();
					ii.ItemDescription = String.Format("{0} - {1} {2} Std. 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} Std. x {3} € =", pRO.HourlyRateNewStartDateString,
						pRO.AccountingEndDateString, pRO.RecordedFLSWithNewHourlyRateString, pRO.HourlyRateNewString);

					ii.AmountTotal = pRO.AmountTotalWithNewHourlyRate;

					pRO.InvoiceItems.Add(ii);

					hourlyRateString = String.Format("{0} €, {1} €", pRO.HourlyRateOldString, pRO.HourlyRateNewString);
				}
				else if (pRO.DifferentHourlyRateCount == 3)
				{
					var ii = new InvoiceItemDC();
					ii.ItemDescription = String.Format("{0} - {1} {2} Std. 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} Std. 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} Std. x {3} € =", pRO.HourlyRateNewStartDateString,
						pRO.AccountingEndDateString, pRO.RecordedFLSWithNewHourlyRateString, pRO.HourlyRateNewString);

					ii.AmountTotal = pRO.AmountTotalWithNewHourlyRate;

					pRO.InvoiceItems.Add(ii);

					hourlyRateString = String.Format("{0} €, {1} €, {2} €", pRO.HourlyRate3String, pRO.HourlyRateOldString, pRO.HourlyRateNewString);
				}    
	        }

			labelHourlyRate.Text = hourlyRateString;
            
            this.bindingSource1.DataSource = pRO;
        }

		private void ReportHeader_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
		{

		}
    }
}
