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 Stundenzettel : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<ServicesOverviewRO>
    {
        public Stundenzettel()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
			double minTotal = 0;
	        bool showALS = false;
			bool showLMS = false;
			String empName = String.Empty;
			Dictionary<String, bool> empCountDict = new Dictionary<String, bool>();

            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("Assistenz"))
		                {
			                showALS = true;
		                }
						else if (s.ServiceCategory.StartsWith("Leistung"))
						{
							showLMS = true;
						}
						else
						{
							minTotal += s.Minutes;
							s.Notice = "d";
						}

						
		                servicesBillable.Add(s);

						//if (!String.IsNullOrEmpty(s.EmployeeFullname))
						//{
						//	empName = s.EmployeeFullname;
						//	if (!empCountDict.ContainsKey(s.EmployeeFullname))
						//		empCountDict.Add(s.EmployeeFullname, true);
						//}
	                }
	                
                }

                pRO.Services = servicesBillable;
            }
			//if (empCountDict.Count == 1)
			//	pRO.MainAttendant = empName;

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

	        if (!showALS)
	        {
		        subReportAls.ReportSource = null;
		        subReportAls.Visible = false;
	        }
	        else
	        {
				StundenzettelALS subReport = subReportAls.ReportSource as StundenzettelALS;

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


			if (!showLMS)
			{
				subReportLms.ReportSource = null;
				subReportLms.Visible = false;
			}
			else
			{
				StundenzettelLMS subReport2 = subReportLms.ReportSource as StundenzettelLMS;

				if (subReport2 != null)
					subReport2.SetReportDataSource(pRO);
			}


			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 == "Direkte Betreuungsleistung")
						{
							return ps;
						}
					}
				}
			}
			return null;
		}

    }
}
