using System;
using BS.Shared;
using BeWo.Report.ReportObjects;
using BeWo.Report;
using System.Collections.Generic;
using BeWo.Data.Entities;
using BeWo.Data.Access;

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

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
            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;
            }
            if (!String.IsNullOrEmpty(pRO.TotalFLSPerCostBearer))
            {
                int pos1 = pRO.TotalFLSPerCostBearer.IndexOf("FLS");

                if (pos1 >= 0)
                {
                    int pos2 = pRO.TotalFLSPerCostBearer.IndexOf("FLS", pos1 + 2);
                    {
                        if (pos2 >= 0)
                        {
                            IList<ServiceCategory> categories = DAOFactory.GenericDAO.GetAll<ServiceCategory>();

                            int smallestIndex = Int32.MaxValue;

                            foreach (var cat in categories)
                            {
                                if (!cat.IsBillable && !String.IsNullOrEmpty(cat.Name) && cat.IsActive == ActivationTypeId.Active && pRO.TotalFLSPerCostBearer.IndexOf(cat.Name) >= 0)
                                {
                                    if (pRO.TotalFLSPerCostBearer.IndexOf(cat.Name) < smallestIndex)
                                    {
                                        smallestIndex = pRO.TotalFLSPerCostBearer.IndexOf(cat.Name);
                                    }
                                }
                            }
                            if (smallestIndex < pRO.TotalFLSPerCostBearer.Length && smallestIndex > 0)
                            {
                                pRO.TotalFLSPerCostBearer = pRO.TotalFLSPerCostBearer.Substring(0, smallestIndex).Trim();
                                if (pRO.TotalFLSPerCostBearer.EndsWith(","))
                                    pRO.TotalFLSPerCostBearer = pRO.TotalFLSPerCostBearer.Substring(0, pRO.TotalFLSPerCostBearer.Length - 1);
                            }
                        }
                    }
                }
            }
            this.bindingSource1.DataSource = pRO;
        }
    }
}
