using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using BS.Shared.DataContracts.Compact;
using BeWo.Report;
using DevExpress.XtraReports.UI;
using BeWo.Report.ReportObjects;
using BeWo.Data.Entities;
using BeWo.Data.Access;
using System.Text;

namespace BeWo.SBBMainzReports
{
    public partial class Klientenstammblatt : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<CustomerDataRO>
    {
        public Klientenstammblatt()
        {
            InitializeComponent();
        }


        public void SetReportDataSource(CustomerDataRO pRO)
        {
            CompactEmployeeDC hauptBetreuuer = null;
            if (pRO.RelatedEmployees != null)
            {
                foreach (var empRel in pRO.RelatedEmployees)
                {

                    if (hauptBetreuuer == null && empRel.RelationRole != null && empRel.RelationRole.IsSystemEntry)
                    {
                        hauptBetreuuer = empRel.Employee;
                    }
                }
            }

            StringBuilder abwesenheiten = new StringBuilder();
            if (hauptBetreuuer != null)
            {
                Employee emp = DAOFactory.GenericDAO.LoadByID<Employee>(hauptBetreuuer.EmployeeOid);
                if (emp.AbsenceTimes != null)
                {
                    var sortedAt = emp.AbsenceTimes.OrderBy(at => at.Start);
                    foreach (var at in sortedAt)
                    {
                        if (!at.End.HasValue || at.End.Value >= DateTime.Now.Date)
                        {
                            if (abwesenheiten.Length > 0)
                                abwesenheiten.Append("\r\n");
                            if (at.End.HasValue)
                                abwesenheiten.Append(String.Format("{0:dd.MM.yyyy} bis {1:dd.MM.yyyy}", at.Start, at.End));
                            else
                                abwesenheiten.Append(String.Format("ab {0:dd.MM.yyyy}", at.Start));
                            if (at.AbsenceReason != null)
                            {
                                abwesenheiten.Append(": ");
                                abwesenheiten.Append(at.AbsenceReason.Description);
                            }
                        }
                        
                    }
                }
            }

            lblAbwesenheitenHauptbetreuuer.Text = abwesenheiten.ToString();
            this.bindingSource1.DataSource = pRO;

            
        }
    }
}
