using System;
using System.Drawing.Text;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BS.Shared;
using BS.Shared.Extensions;

namespace CaritasWuppertalSolingen
{
    public partial class Stundenzettel : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<ServicesOverviewRO>
    {
        public Stundenzettel()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
	        var org = GetOrganisation(pRO);

	        if (org != null)
	        {
		        if (org.Name.Contains("Jugendamt"))
		        {
			        cellAz.Text = "Az. Jugendamt:";
			        lblWithFactor.Visible = false;
			        lblSumWithFactor.Visible = false;
		        }
		        var rateFactor = GetRateFactor(pRO);
		        lblWithFactor.Text = String.Format("2. Summe der abzurechnenden Leistungen (= 1 x {0:0.0})", rateFactor);
		        lblSumWithFactor.Text = String.Format("{0:0.##} x {1:0.0} = {2:0.##} Std.", pRO.TotalFLMBillable/60,
			        rateFactor, (pRO.TotalFLMBillable/60)*(double)rateFactor);

	        }
	        bindingSource1.DataSource = pRO;
        }

		private decimal GetRateFactor(ServicesOverviewRO pRO)
		{
			if (pRO.CostBearer2SupportConceptList.Count > 0)
			{
				var c2s = pRO.CostBearer2SupportConceptList[0];

				var dcList = MapperFactory.CostRatePeriodDC_CostRatePeriod.MapToNewDCs(c2s.CostBearer.CostRatePeriods);

				var rf = dcList.GetCostRatePeriodForDate(CostRatePeriodType.RateFactor, pRO.StartDate);
				if (rf != null && rf.CostRateValue.HasValue)
				{
					return (100 + rf.CostRateValue.Value) / 100;
				}
			}

			return 1;
		}

		private Organisation GetOrganisation(ServicesOverviewRO pRO)
		{
			if (pRO.CostBearer2SupportConceptList.Count > 0)
			{
				var c2s = pRO.CostBearer2SupportConceptList[0];

				return c2s.CostBearer.Organisation;
			}
			return null;
		}
    }
}
