using System;
using System.Collections.Generic;
using System.Linq;
using BeWo.Data.Entities;
using BeWo.Report.ReportObjects;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;

namespace BeWo.Report.DefaultReports
{
	public partial class WeeklyAppointmentReport : XtraReport
	{
		public List<SchedulerAppointment> TermineMontag { get; set; }
		public List<SchedulerAppointment> TermineDienstag { get; set; }
		public List<SchedulerAppointment> TermineMittwoch { get; set; }
		public List<SchedulerAppointment> TermineDonnerstag { get; set; }
		public List<SchedulerAppointment> TermineFreitag { get; set; }
		public List<SchedulerAppointment> TermineSamstag { get; set; }
		public List<SchedulerAppointment> TermineSonntag { get; set; }

		public WeeklyAppointmentReport()
		{
			InitializeComponent();
		}

		public void SetReportDataSource(KalenderRO pRO)
		{
			// Die Woche des ersten Termins wird dargestellt:
			// Wenn die Auswahl an einem Sonntag beginnt, wird die Woche des Tages dargestellt und nicht die im Kalender dargestellte Zeitliche auswahl.
			bindingSource1.DataSource = pRO;

			TermineMontag = new List<SchedulerAppointment>();
			TermineDienstag = new List<SchedulerAppointment>();
			TermineMittwoch = new List<SchedulerAppointment>();
			TermineDonnerstag = new List<SchedulerAppointment>();
			TermineFreitag = new List<SchedulerAppointment>();
			TermineSamstag = new List<SchedulerAppointment>();
			TermineSonntag = new List<SchedulerAppointment>();

			pRO.Appointments = pRO.Appointments.OrderBy(a => a.StartDate).ToList();
			foreach (var app in pRO.Appointments)
			{
				switch (app.StartDate.Value.DayOfWeek)
				{
					case DayOfWeek.Monday:
						TermineMontag.Add(app);
						break;
					case DayOfWeek.Tuesday:
						TermineDienstag.Add(app);
						break;
					case DayOfWeek.Wednesday:
						TermineMittwoch.Add(app);
						break;
					case DayOfWeek.Thursday:
						TermineDonnerstag.Add(app);
						break;
					case DayOfWeek.Friday:
						TermineFreitag.Add(app);
						break;
					case DayOfWeek.Saturday:
						TermineSamstag.Add(app);
						break;
					case DayOfWeek.Sunday:
						TermineSonntag.Add(app);
						break;
				}
			}

			// Montag finden
			if(pRO.Appointments.Count > 0)
			{
				var f = pRO.Appointments.First();
				var x = (int) f.StartDate.Value.DayOfWeek;
				x = x == 0 ? x + 6 : x - 1;
				var monday = f.StartDate.Value.AddDays(-1*x);
			}

			

			TabelleFuellen(TermineMontag, montagTabelle);
			TabelleFuellen(TermineDienstag, dienstagTabelle);
			TabelleFuellen(TermineMittwoch, mittwochTabelle);
			TabelleFuellen(TermineDonnerstag, donnerstagTabelle);
			TabelleFuellen(TermineFreitag, freitagTabelle);
			TabelleFuellen(TermineSamstag, samstagTabelle);
			TabelleFuellen(TermineSonntag, sonntagTabelle);
		}

		public void TabelleFuellen(List<SchedulerAppointment> termine, XRTable tabelle)
		{
			if (termine.Count == 0)
				tabelle.Visible = false;

			for (var i = 0; i < termine.Count; i++)
			{
				var r = i == 0 ? tabelle.Rows[i] : tabelle.InsertRowBelow(tabelle.Rows[i - 1]);

				r.Cells[0].Text = string.Format("{0} - {1}", termine[i].StartDate.Value.ToShortTimeString(), termine[i].EndDate.Value.ToShortTimeString());
				r.Cells[1].Text = termine[i].Subject;

				if (i != 0)
				{
					r.Borders = BorderSide.Bottom | BorderSide.Right | BorderSide.Left;
				}
			}
		}
	}
}
