using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BS.Shared.Extensions;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;
using BS.Shared;

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

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

	        var cb2sc = DAOFactory.GenericDAO.GetByID<CostBearer2SupportConcept>(pRO.CostBearer2SupportConceptOid);

	        if (cb2sc.CostBearer != null)
	        {
		        var crs = cb2sc.CostBearer.CostRatePeriods;
		        var crDcs = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(crs);

				var serviceUnit = crDcs.GetCostRatePeriodForDate(CostRatePeriodType.MinutesPerServiceUnit, DateTime.Now);
		        if (serviceUnit != null && !String.IsNullOrEmpty(serviceUnit.UnitName) && serviceUnit.UnitName.ToLower() != "fls")
		        {
			        replaceFls = true;
		        }
	        }
	        String stdText = "FLS";

	        if (replaceFls)
	        {
		        xrLabel3.Text = xrLabel3.Text.Replace("Betreuungsleistungen", "Serviceleistung");
		        xrTableCell19.Text = xrTableCell19.Text.Replace("Fachleistungsstunden (FLS)", "Stunden");

		        stdText = "Std.";
				if (pRO.ApprovedFLS != null)
					pRO.ApprovedFLS = pRO.ApprovedFLS.Replace("FLS", stdText);
				if (pRO.ApprovedFLSWithFactor != null)
					pRO.ApprovedFLSWithFactor = pRO.ApprovedFLSWithFactor.Replace("FLS", stdText);
	        }

	        if (pRO.DifferentHourlyRateCount == 3)
            {
                //Erbrachte Leistungen (FLS) - bis [HourlyRateOldEndDateString] à [HourlyRateOldString] €

                pRO.Abrechnungstext1 = String.Format("{0} bis {1}", 
                    pRO.AccountingStartDateString, pRO.HourlyRate3EndDateString);

                pRO.Abrechnungstext1 += String.Format(": {0:0.##} {3} {1}x {2:c}",
                    pRO.GeleisteteFLSMitStundensatz3, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRate3, stdText);

               //[AccountingStartDateString] - [HourlyRate3EndDateString] sind [RecordedFLSWithHourlyRate3String] Std. x [HourlyRate3String] € =";

                pRO.Betrag1 = String.Format("{0:c}", pRO.GeleisteteFLSMitStundensatz3 * (decimal)pRO.RateFactor * pRO.HourlyRate3);


                pRO.Abrechnungstext2 = String.Format("{0} bis {1}",
                    pRO.HourlyRateOldStartDateString, pRO.HourlyRateOldEndDateString);
                pRO.Abrechnungstext2 += String.Format(": {0:0.##} {3} {1}x {2:c}",
                    pRO.GeleisteteFLSMitStundensatzAlt, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateOld, stdText);

                pRO.Betrag2 = String.Format("{0:c}", pRO.GeleisteteFLSMitStundensatzAlt * (decimal)pRO.RateFactor * pRO.HourlyRateOld);


                pRO.Abrechnungstext3 = String.Format("{0} bis {1}",
                    pRO.HourlyRateNewStartDateString, pRO.AccountingEndDateString);
                pRO.Abrechnungstext3 += String.Format(": {0:0.##} {3} {1}x {2:c}",
                    pRO.GeleisteteFLSMitStundensatzAktuell, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateNew, stdText);

                pRO.Betrag3 = String.Format("{0:c}", pRO.GeleisteteFLSMitStundensatzAktuell * (decimal)pRO.RateFactor * pRO.HourlyRateNew);
            }
            else if (pRO.DifferentHourlyRateCount == 2)
            {
                
                pRO.Abrechnungstext2 = String.Format("{0} bis {1}",
                    pRO.AccountingStartDateString, pRO.HourlyRateOldEndDateString);
                pRO.Abrechnungstext2 += String.Format(": {0:0.##} {3} {1}x {2:c}",
                    pRO.GeleisteteFLSMitStundensatzAlt, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateOld, stdText);

                pRO.Betrag2 = String.Format("{0:c}", pRO.GeleisteteFLSMitStundensatzAlt * (decimal)pRO.RateFactor * pRO.HourlyRateOld);


                pRO.Abrechnungstext3 = String.Format("{0} bis {1}",
                    pRO.HourlyRateNewStartDateString, pRO.AccountingEndDateString);
                pRO.Abrechnungstext3 += String.Format(": {0:0.##} {3} {1}x {2:c}",
                    pRO.GeleisteteFLSMitStundensatzAktuell, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateNew, stdText);

                pRO.Betrag3 = String.Format("{0:c}", pRO.GeleisteteFLSMitStundensatzAktuell * (decimal)pRO.RateFactor * pRO.HourlyRateNew);
            }
            else
            {
                pRO.Abrechnungstext3 = String.Format("{0} bis {1}",
                    pRO.AccountingStartDateString, pRO.AccountingEndDateString);
                pRO.Abrechnungstext3 += String.Format(": {0:0.##} {3} {1}x {2:c}",
                    pRO.GeleisteteFLSMitStundensatzAktuell, pRO.RateFactor == 1 ? "" : "x " + pRO.RateFactorText2 + " ", pRO.HourlyRateNew, stdText);

                pRO.Betrag3 = String.Format("{0:c}", pRO.GeleisteteFLSMitStundensatzAktuell * (decimal)pRO.RateFactor * pRO.HourlyRateNew);
            }
            

            this.bindingSource1.DataSource = pRO;
        }
    }
}
