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

namespace BeWo.SozialeDiensteNiederrhein
{
    public partial class StundenzettelAssistenz : XtraReport//, IBeWoReport<ServicesOverviewRO>
    {
		public StundenzettelAssistenz()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
			double minTotal = 0;

			if (pRO.Services != null)
			{
				List<ServicesOverviewRO.ServiceDetail> servicesBillable = new List<ServicesOverviewRO.ServiceDetail>();

				foreach (ServicesOverviewRO.ServiceDetail s in pRO.Services)
				{
					if (s.ServiceCategory.Contains("Assistenz"))
					{
						minTotal += s.Minutes;

					}

				}

			}
			bindingSource1.DataSource = pRO;
			// rechnungsDatum.Text = DateTime.Now.ToShortDateString();

			lblSummeMinuten.Text = string.Format("{0:0.##}", minTotal);

			var approved = pRO.ApprovedFLSPerWeek;

			var stats = GetStatistics(pRO);
			if (stats != null)
			{
				approved = stats.MinutesApprovedPerWeek / 60;
			}

			lblBewilligteStunden.Text = string.Format("{0:0.##}", approved);
        }

		private SupportConceptPeriodStatisticsDC GetStatistics(ServicesOverviewRO ro)
		{
			if (ro.CostBearer2SupportConceptList != null && ro.CostBearer2SupportConceptList.Count > 0)
			{
				var service = new OperationsServiceImp();

				foreach (var cb2sc in ro.CostBearer2SupportConceptList)
				{
					long cb2scOid = cb2sc.Oid.Value;


					var stats = service.CreateSupportConceptStatistics(cb2scOid, ro.EndDate);

					if (stats.PeriodStatistics != null && stats.PeriodStatistics.Count > 0)
					{
						foreach (var ps in stats.PeriodStatistics)
						{
							if (ps.SupportConceptApprovalPeriod != null && ps.SupportConceptApprovalPeriod.ServiceCategory != null &&
								ps.SupportConceptApprovalPeriod.ServiceCategory.Name.Contains("Assistenz"))
							{
								return ps;
							}
						}
					}
				}

			}
			return null;
		}
    }
}
