using System;
using BeWo.Report.ReportObjects;
using BeWo.Report;
using System.Collections.Generic;

namespace BeWo.AssistenzaReports
{
    public partial class FLSAuswertung : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<FLSReportRO>
    {
        public FLSAuswertung()
        {
            InitializeComponent();
        }

        public void SetReportDataSource(FLSReportRO pRO)
        {
            if (pRO.FLSReportGroups != null)
            {
                foreach (var group in pRO.FLSReportGroups)
                {
                    FLSReportRO.FLSReportDetail sonstigesAlt = null;
                    FLSReportRO.FLSReportDetail fahrzeiten = null;
                    FLSReportRO.FLSReportDetail buerozeiten = null;
                    FLSReportRO.FLSReportDetail sonstigesNeu = null;

                    if (group.FLSReportDetails != null)
                    {
                        foreach (var detail in group.FLSReportDetails)
                        {
                            if (detail.Name == "Sonstiges")
                            {
                                sonstigesAlt = detail;
                            }
                            else 
                            {
                                if (detail.ServiceRecords != null)
                                {
                                    foreach (var sr in detail.ServiceRecords)
                                    {
                                        sr.Notice = "";
                                    }
                                }

                            }
                        }
                    }
                    if (sonstigesAlt != null)
                    {
                        group.FLSReportDetails.Remove(sonstigesAlt);

                        if (sonstigesAlt.ServiceRecords != null)
                        {
                            foreach (var sr in sonstigesAlt.ServiceRecords)
                            {
                                if (sr.ServiceCategory.IndexOf("Fahrtzeiten") == 0)
                                {
                                    if (fahrzeiten == null)
                                    {
                                        fahrzeiten = new FLSReportRO.FLSReportDetail();
                                        fahrzeiten.ServiceRecords = new List<FLSReportRO.FLSReportServiceRecord>();
                                        fahrzeiten.Name = "Fahrtzeiten";
                                    }
                                    fahrzeiten.TotalFLM += sr.RoundedDuration;
                                    fahrzeiten.ServiceRecords.Add(sr);

                                }
                                else if (sr.ServiceCategory.IndexOf("Büro") == 0)
                                {
                                    if (buerozeiten == null)
                                    {
                                        buerozeiten = new FLSReportRO.FLSReportDetail();
                                        buerozeiten.ServiceRecords = new List<FLSReportRO.FLSReportServiceRecord>();
                                        buerozeiten.Name = "Bürozeiten";
                                    }
                                    buerozeiten.TotalFLM += sr.RoundedDuration;
                                    buerozeiten.ServiceRecords.Add(sr);
                                }
                                else
                                {
                                    if (sonstigesNeu == null)
                                    {
                                        sonstigesNeu = new FLSReportRO.FLSReportDetail();
                                        sonstigesNeu.ServiceRecords = new List<FLSReportRO.FLSReportServiceRecord>();
                                        sonstigesNeu.Name = sonstigesAlt.Name;
                                    }
                                    sonstigesNeu.TotalFLM += sr.RoundedDuration;
                                    sonstigesNeu.ServiceRecords.Add(sr);
                                }

                            }
                            if (buerozeiten != null)
                                group.FLSReportDetails.Insert(0, buerozeiten);
                            if (fahrzeiten != null)
                                group.FLSReportDetails.Insert(0, fahrzeiten);
                            if (sonstigesNeu != null)
                                group.FLSReportDetails.Add(sonstigesNeu);
                        }
                    }

                }
            }
           
            

            
            this.bindingSource1.DataSource = pRO;
        }

    }
}
