using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Cryptography;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Report;
using BS.Shared;

namespace BeWo.KontaktUndKrisenhilfeReports
{
    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);

                        var sr = DAOFactory.GenericDAO.LoadByID<ServiceRecord>(s.ServiceRecordOid);

                        var name = s.EmployeeFullname;
                        s.EmployeeFullname = "";
                        s.EmployeeAbbr = "";
                        s.ServiceCategory = "";
                        s.ServiceCategoryAbbr = "";

                        if (name == "Mitarbeiter 1")
                        {
                            s.ServiceCategoryAbbr = String.Format("{0} Teilnehmer", s.CustomerCount);
                        }
                        else
                        {
                            bool hatRolle = false;

                            if (sr.Employee != null && sr.CustomerOid.HasValue)
                            {
                                foreach (var e2c in sr.Employee.Employee2CustomerList)
                                {
                                    if (e2c.CustomerOid == sr.CustomerOid)
                                    {
                                        foreach (var v2o in e2c.ValueList)
                                        {
                                            var entry = v2o.Entry;
                                            if (entry.Type == ValueListEntryType.StaffRoleType)
                                            {
                                                hatRolle = true;
                                                if (entry.Value.Contains("Fallverantwortung"))
                                                {
                                                    s.EmployeeFullname = name;
                                                }
                                                else if (entry.Value == "Regelbetreuer")
                                                {
                                                    s.EmployeeAbbr = name;
                                                }
                                                else if (entry.Value.Contains("Vertreter"))
                                                {
                                                    s.ServiceCategory = name;
                                                }
                                                else
                                                {
                                                    s.EmployeeFullname = name;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (!hatRolle)
                            {
                                s.ServiceCategory = name;
                            }
                        }
                    }
                }

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