using System;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;
using BS.Shared;

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

        public void SetReportDataSource(ServicesOverviewRO pRO)
        {
            double gesamt = 0;
            double abrechenbar = 0;

            if (pRO.Services != null)
            {
                foreach (var sd in pRO.Services)
                {
                    if (sd.IsBillable)
                    {
                        abrechenbar += sd.Minutes;
                    }
                    gesamt += sd.Minutes;
                }
            }

            lblSummeFLS.Text = String.Format("{0:0.00}", gesamt / 60);
            lblFLS2.Text = String.Format("{0:0.00}", gesamt / 60);
            //lblFlmAbrechenbar.Text = String.Format("{0:0.##}", abrechenbar);
            //lblFlsAbrechenbar.Text = String.Format("{0:0.00}", abrechenbar / 60);
            //lblFlsFactor.Text = String.Format("{0:0.00}", (abrechenbar / 60) * 1.2);

            pRO.CalculateFLSSollIst();

            lblFLSProMonat.Text = String.Format("{0:0.00}", pRO.ApprovedFLSPerMonth);
            lblGesamtbudget.Text = String.Format("{0:0.00}", pRO.ApprovedFLSTotal);


            lblRestbudget.Text = String.Format("{0:0.00}", pRO.FreeMinutesThisMonth / 60);

            SetzeKopfbereich(pRO);
            this.bindingSource1.DataSource = pRO;
        }

        private void SetzeKopfbereich(ServicesOverviewRO pRO)
        {
            String employee1 = "";
            String employee2 = "";

            if (pRO.CostBearer2SupportConceptList != null)
            {
                var cb2sc = pRO.CostBearer2SupportConceptList[0];
                lblJugendamt.Text = pRO.Costbearer;

                if (cb2sc.CostBearerContactPerson != null)
                {
                    //if (cb2sc.CostBearerContactPerson.Sex.HasValue &&
                    //    cb2sc.CostBearerContactPerson.Sex.Value == Sex.Female)
                    //{
                    //    cellAnsprechpartner.Text = "Frau ";
                    //}
                    //else if (cb2sc.CostBearerContactPerson.Sex.HasValue &&
                    //    cb2sc.CostBearerContactPerson.Sex.Value == Sex.Male)
                    //{
                    //    cellAnsprechpartner.Text = "Herr ";
                    //}
                    //cellAnsprechpartner.Text += cb2sc.CostBearerContactPerson.LastName;
                }
                foreach (var scap in cb2sc.ApprovalPeriodList)
                {
                    if (scap.StartDate <= pRO.EndDate && scap.EndDate >= pRO.StartDate)
                    {
                        if (scap.ServiceCategory != null)
                        {
                            //cellLeistung.Text = scap.ServiceCategory.Name;
                        }
                    }
                }

                var customer = cb2sc.SupportConcept.Customer;

                foreach (var c2o in customer.Employee2CustomerList)
                {
                    foreach (var valueEntry in c2o.ValueList)
                    {
                        if (valueEntry.Entry.Type == ValueListEntryType.StaffRoleType)
                        {
                            if (valueEntry.Entry.SystemEntryID != null &&
                                valueEntry.Entry.SystemEntryID.Value == SystemEntryID.EmployeeRoleMainAttendant)
                            {
                                if (String.IsNullOrEmpty(employee1))
                                {
                                    employee1 = c2o.Employee.Person.FirstName + " " + c2o.Employee.Person.LastName;
                                }
                                else
                                {
                                    employee2 = c2o.Employee.Person.FirstName + " " + c2o.Employee.Person.LastName;
                                }
                            }
                            else
                            {
                                employee2 = c2o.Employee.Person.FirstName + " " + c2o.Employee.Person.LastName;
                            }
                        }
                    }
                }
                if (customer.AssistanceBegin.HasValue)
                {
                    lblAufnahmedatum.Text = String.Format("{0:dd.MM.yyyy}", customer.AssistanceBegin);
                }
                else
                {
                    lblAufnahmedatum.Text = String.Format("{0:dd.MM.yyyy}", cb2sc.StartDate);
                }
            }
            lblFachkraft.Text = employee1;
            lblFachkraft2.Text = employee2;
            
        }
    }
}
