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

namespace BeWo.BloemelingUndTeckhaus
{
    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"))
                        {
                            showAssi = true;
                        }
                        else
                        {
                            minTotal += s.Minutes;
                        }
                        servicesBillable.Add(s);
                    }
                }

                pRO.Services = servicesBillable;
            }

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

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

            }
            else
            {
                GroupFooter2.Visible = false;
                //xrSubreport1.Visible = false;
            }
            
            lblSummeMinuten.Text = string.Format("{0:0.##}", minTotal / 60);
            cellMinuten.Text = string.Format("{0:0.##}", minTotal);
            cellStunden.Text = string.Format("{0:0.00}", minTotal / 60);
            lblSummeMinutenFaktor.Text = string.Format("{0:0.##}", Math.Round(minTotal / 60, 2, MidpointRounding.AwayFromZero) * 1.2);

            var approved = pRO.ApprovedFLSPerWeek;

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

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


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

            }
            return null;
        }

    }
}
