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;
using System.Collections.Generic;

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

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

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

				foreach (ServicesOverviewRO.ServiceDetail s in pRO.Services)
				{
					if (s.IsBillable)
					{
						if (s.ServiceCategory.Contains("Assistenz") || s.ServiceDescription.Contains("Assistenz"))
						{
							showAssi = true;
						}
						else
						{
							minTotal += s.Minutes;
						}
						servicesBillable.Add(s);
					}
				}

				pRO.Services = servicesBillable;
			}

			if (showAssi)
			{
				var subReport = xrSubreport1.ReportSource as AssistenzstundenReport;

				if (subReport != null)
					subReport.SetReportDataSource(pRO);

			}
			else
			{
				GroupFooter2.Visible = false;
				//xrSubreport1.Visible = false;
			}
			bindingSource1.DataSource = pRO;
			lblSummeMinuten.Text = string.Format("{0:0.##}", minTotal);
			lblSummeMinuten.Text = string.Format("{0:0.##}", minTotal);

			//lblSummeMinutenFaktor.Text = string.Format("{0:0.##}", minTotal * 1.2);
			var approved = pRO.ApprovedFLSPerWeek;

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


            this.bindingSource1.DataSource = pRO;
        }

		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.StartsWith("FLS"))
							{
								return ps;
							}
						}
					}
				}

			}
			return null;
		}
    }
}
