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

using BS.Shared.Core;
using DevExpress.XtraReports.UI;

namespace GerdasPflegedienst
{
	[IDSpecificClass(Identifier = "Mitarbeiterstundenkonto")]
	public partial class Mitarbeiterstundenkonto : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<MitarbeiterstundenkontoRO>
	{
        private Dictionary<long, decimal> oid2SumTotal = new Dictionary<long, decimal>();
        private Dictionary<long, decimal> oid2SumSunday = new Dictionary<long, decimal>();
        private Dictionary<long, decimal> oid2SumFeiertag = new Dictionary<long, decimal>();
        private Dictionary<long, decimal> oid2SumKrank = new Dictionary<long, decimal>();
        private Dictionary<long, decimal> oid2SumUrlaub = new Dictionary<long, decimal>();

        public Mitarbeiterstundenkonto()
		{
			InitializeComponent();
		}

		public void SetReportDataSource(MitarbeiterstundenkontoRO pRO)
		{
			var msb = new GerdasMitarbeiterstundenkontoBerechnung();
			msb.CreateReport(pRO);

		    
		    foreach (var employeeDetail in pRO.EmployeeDetailList)
		    {
		        if (!employeeDetail.UeberstundenVormonat.HasValue)
		        {
		            employeeDetail.UeberstundenVormonat = 0;
		        }
		        decimal sumTotal = 0;
                decimal sumSunday = 0;
		        decimal sumFeiertag = 0;
                decimal sumKrank = 0;
                decimal sumUrlaub = 0;

                foreach (var dayDetail in employeeDetail.Days)
                {
		            if (dayDetail.EndDateTime4.HasValue)
		            {
		                dayDetail.EndDateTime1 = dayDetail.EndDateTime4;
		            }
                    else if (dayDetail.EndDateTime3.HasValue)
                    {
                        dayDetail.EndDateTime1 = dayDetail.EndDateTime3;
                    }
                    else if (dayDetail.EndDateTime2.HasValue)
                    {
                        dayDetail.EndDateTime1 = dayDetail.EndDateTime2;
                    }
                    DateTime dt = DateTime.Now.Date.AddMinutes((double)dayDetail.MinutesTotal);
                    TimeSpan tspan = dt.Subtract(DateTime.Now.Date);

                    double d = tspan.TotalHours;

                    d = Math.Floor(d);
                    
                    dayDetail.Custom1 = String.Format("{0}:{1:00}", d, tspan.Minutes);

                    sumTotal += dayDetail.MinutesTotal;
                    sumSunday += dayDetail.HoursSunday;
                    sumFeiertag += dayDetail.HoursHoliday;
                    sumKrank += dayDetail.HoursSick;
                    sumUrlaub += dayDetail.HoursInVacation;
                }

                oid2SumTotal[employeeDetail.Employee.Oid.Value] = sumTotal;
                oid2SumSunday[employeeDetail.Employee.Oid.Value] = sumSunday * 60;
                oid2SumFeiertag[employeeDetail.Employee.Oid.Value] = sumFeiertag * 60;
                oid2SumKrank[employeeDetail.Employee.Oid.Value] = sumKrank * 60;
                oid2SumUrlaub[employeeDetail.Employee.Oid.Value] = sumUrlaub * 60;
		    }
		    
            //ueberschriftsLabel.Text = string.Format("Arbeitszeiterfassung für den Monat {0}", pRO.ReportStartDate.ToString("MMMM yyyy"));
            cellReportZeitraum.Text = string.Format("{0:dd.MM.yyyy} - {1:dd.MM.yyyy}", pRO.ReportStartDate, pRO.ReportEndDate.AddDays(-1));
            cellDruckdatum.Text = string.Format("{0:dd.MM.yyyy HH:mm}", DateTime.Now);
            
			bindingSource1.DataSource = pRO;
		}
        
        private void cellSumMinutesTotal_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            XRTableCell cell = sender as XRTableCell;
            var row = (MitarbeiterstundenkontoRO.EmployeeDetail)DetailReport.GetCurrentRow();

            cell.Text = GetTimeString(oid2SumTotal[row.Employee.Oid.Value]);
        }

        private void cellSumHoursSunday_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            XRTableCell cell = sender as XRTableCell;
            var row = (MitarbeiterstundenkontoRO.EmployeeDetail)DetailReport.GetCurrentRow();

            cell.Text = GetTimeString(oid2SumSunday[row.Employee.Oid.Value]);
        }

        private void cellSumHoursHoliday_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            XRTableCell cell = sender as XRTableCell;
            var row = (MitarbeiterstundenkontoRO.EmployeeDetail)DetailReport.GetCurrentRow();

            cell.Text = GetTimeString(oid2SumFeiertag[row.Employee.Oid.Value]);
        }

        private void cellSumHoursSick_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            XRTableCell cell = sender as XRTableCell;
            var row = (MitarbeiterstundenkontoRO.EmployeeDetail)DetailReport.GetCurrentRow();

            cell.Text = GetTimeString(oid2SumKrank[row.Employee.Oid.Value]);
        }

        private void cellSumHoursInVacation_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            XRTableCell cell = sender as XRTableCell;
            var row = (MitarbeiterstundenkontoRO.EmployeeDetail)DetailReport.GetCurrentRow();

            cell.Text = GetTimeString(oid2SumUrlaub[row.Employee.Oid.Value]);
        }

	    private String GetTimeString(decimal minuten)
	    {
            DateTime dt = DateTime.Now.Date.AddMinutes((double)minuten);
            TimeSpan tspan = dt.Subtract(DateTime.Now.Date);

            double d = tspan.TotalHours;

            d = Math.Floor(d);

            return String.Format("{0}:{1:00}", d, tspan.Minutes);
        }
    }
}
