using System.Collections.Generic;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BeWo.Service.ServiceImplementations;
using BS.Shared.DataContracts;

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

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
			var stats = GetStatistics(pRO);
	        if (stats != null)
	        {
		        var free = stats.MinutesFreePerWeek/60;
		        lblFreiProWoche.Text = string.Format("{0:0.##}", free);
	        }
	        else
	        {
		        lblFreiProWoche.Text = "";
	        }
	        
            if (pRO.Services != null)
            {
                List<ServicesOverviewRO.ServiceDetail> servicesBillable = new List<ServicesOverviewRO.ServiceDetail>();

                foreach (ServicesOverviewRO.ServiceDetail s in pRO.Services)
                {
                    if (s.IsBillable)
                        servicesBillable.Add(s);
                }

                pRO.Services = servicesBillable;
            }
            this.bindingSource1.DataSource = pRO;
        

			//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)
					{
						return stats.PeriodStatistics[0];
					}
				}

			}
			return null;
		}

    }
}
