using System;
using System.Collections;
using System.ComponentModel;
using BeWo.Service.ServiceImplementations;
using BS.Shared.DataContracts;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;

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

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
	        
	        


			double minTotal = 0;

            if (pRO.Services != null)
            {
                foreach (var item in pRO.Services)
                {
	                if (item.IsBillable)
	                {
		                if (item.ServiceCategory == "Hausmeister 0,3" ||
							(item.ServiceCategory == "LVR Fachleistung" && !item.ServiceDescription.Contains("Bereitschaft")))
						{
							if (item.CustomerCount > 1)
							{
								item.ServiceDescription += " (" + item.CustomerCount + " Teilnehmer)";
							}

							minTotal += item.Minutes;
							item.Notice = "fls";
						}
	                }
                }
            }

			lblApprovedMinutes.Text = String.Format("{0:0}/{1:0.00}", pRO.ApprovedFLSPerWeek * 60, pRO.ApprovedFLSPerWeek);

			var stats = GetStatistics(pRO);

			if (stats != null)
			{
				pRO.FreeMinutesThisMonth = stats.IstTotalUntilThisMonth / 60;

				lblApprovedMinutes.Text = String.Format("{0:0}/{1:0.00}", stats.MinutesApprovedPerWeek, stats.MinutesApprovedPerWeek / 60);
			}

			DateTime dt = DateTime.Now.Date.AddMinutes(minTotal);
			TimeSpan tspan = dt.Subtract(DateTime.Now.Date);

			double d = tspan.TotalHours;

			d = Math.Floor(d);

			lblHoursMinutesString.Text = String.Format("{0:0.00} Stunden ({1}:{2:00})", minTotal / 60, d, tspan.Minutes);

	        lblTotalMinutes.Text = String.Format("{0:0.##}", minTotal);

            this.bindingSource1.DataSource = pRO;
        }

		private SupportConceptStatisticsDC GetStatistics(ServicesOverviewRO ro)
		{
			if (ro.CostBearer2SupportConceptList != null)
			{
				var service = new OperationsServiceImp();
				long cb2scOid = ro.CostBearer2SupportConceptList[0].Oid.Value;


				if (ro.CostBearer2SupportConceptList.Count > 1)
				{
					foreach (var c2s in ro.CostBearer2SupportConceptList)
					{
						if (c2s.CostBearer.Organisation.Name == "LVR FLS")
							cb2scOid = c2s.Oid.Value;
					}
				}

				return service.CreateSupportConceptStatistics(cb2scOid, ro.EndDate);
			}
			return null;
		}

    }
}
