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.LebenshilfeDuisburgReports
{
    public partial class StundenzettelLMS : DevExpress.XtraReports.UI.XtraReport
    {
		public StundenzettelLMS()
        {
            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.IsBillable)
					{
						if (s.ServiceCategory.StartsWith("Leistung"))
						{
							minTotal += s.Minutes;
							s.Notice = "l";
						}
						servicesBillable.Add(s);

					}

				}

				pRO.Services = servicesBillable;
			}

			lblFLSTotal.Text = String.Format("{0:0.00}", minTotal / 60);

			lblBewilligteStunden.Text = String.Format("{0:0.##} Stunden", pRO.ApprovedFLSPerWeek);

			var stats = GetStatistics(pRO);
			if (stats != null)
			{
				lblBewilligteStunden.Text = String.Format("{0:0.##} Stunden", stats.MinutesApprovedPerWeek / 60);
			}

			bindingSource1.DataSource = pRO;

		}

		private SupportConceptPeriodStatisticsDC GetStatistics(ServicesOverviewRO ro)
		{
			if (ro.CostBearer2SupportConceptList != null && ro.CostBearer2SupportConceptList.Count > 0)
			{
				var service = new OperationsServiceImp();
				long cb2scOid = ro.CostBearer2SupportConceptList[0].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 == "Leistungsmodul Serviceorientiert")
						{
							return ps;
						}
					}
				}
			}
			return null;
		}
    }
}
