using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Report;
using BeWo.Report.ReportObjects;
using BS.Shared;
using BS.Shared.Core;

namespace KoelnRingReports
{
    public partial class ErgaenzendeDiensteTeilnahmeMonat : DevExpress.XtraReports.UI.XtraReport, IBeWoReport<QueryRO>
    {
		public ErgaenzendeDiensteTeilnahmeMonat()
        {
            InitializeComponent();
        }

	    public void SetReportDataSource(QueryRO pRO)
	    {
		    var dateString = pRO.Rows[0].Field1;
		    var bereich = pRO.Rows[0].Field2;

		    var regions = DAOFactory.GenericDAO.GetAll<AdditionalServiceRegion>();
			regions = regions.Where(f => String.Equals(f.AdditionalService.Name, bereich.ToString(), StringComparison.CurrentCultureIgnoreCase)).ToList();
			
		    var monat = DateTime.Parse(dateString.ToString());
			var daysInMonth = DateTime.DaysInMonth(monat.Year, monat.Month);

		    var m = new DateTime(monat.Year, monat.Month, DateTime.DaysInMonth(monat.Year, monat.Month));
		    var bookings = new List<AdditionalServiceBooking>();

		    foreach (var region in regions)
		    {
			    if (bookings.Any(a => a.AdditionalServiceRegion.Equals(region)))
				    continue;

				bookings.AddRange(DAOFactory.SearchDAO.GetAdditionalServiceBookings(region.Oid, monat, m));
		    }

			var reportObj = ErgaenzendeDiensteTeilnahmeMonatRO.Create(bookings, monat, bereich.ToString());

			xrTableCell30.BackColor = daysInMonth >= 29 ? Color.Transparent : Color.LightGray;
			xrTableCell31.BackColor = daysInMonth >= 30 ? Color.Transparent : Color.LightGray;
			xrTableCell32.BackColor = daysInMonth == 31 ? Color.Transparent : Color.LightGray;

			xrTableCell62.BackColor = xrTableCell30.BackColor;
			xrTableCell63.BackColor = xrTableCell31.BackColor;
			xrTableCell64.BackColor = xrTableCell32.BackColor;

			xrTableCell30.ForeColor = daysInMonth >= 29 ? Color.Black : Color.DimGray;
			xrTableCell31.ForeColor = daysInMonth >= 30 ? Color.Black : Color.DimGray;
			xrTableCell32.ForeColor = daysInMonth == 31 ? Color.Black : Color.DimGray;

			bindingSource1.DataSource = reportObj;

			xrLabel1.Text = string.Format("Ergänzende Dienste Teilnahme für den Monat {0}", monat.ToString("MMMM yyyy"));
	    }

		
    }

	public class ErgaenzendeDiensteTeilnahmeMonatRO : AbstractBeWoReportObject<ErgaenzendeDiensteTeilnahmeMonatRO>
	{
		public string Bereich { get; set; }
		public DateTime Date { get; set; }
		public List<ErgDiensteTeilnahmeMonatZeile> Zeilen { get; set; }
		public int Gesamtsumme { get; set; }

		public static ErgaenzendeDiensteTeilnahmeMonatRO Create(IEnumerable<AdditionalServiceBooking> bookings, DateTime date, string bereich)
		{
			var pRO = new ErgaenzendeDiensteTeilnahmeMonatRO { Date = date, Bereich = bereich };
			var zeilen = new List<ErgDiensteTeilnahmeMonatZeile>();
			
			foreach (var booking in bookings)
			{
				if (!booking.Datum.HasValue)
					continue;

				foreach (var customer in booking.Customer2AddServiceBookings)
				{
					if (customer.Participated)
					{
						var x = new ErgDiensteTeilnahmeMonatZeile();

						if (zeilen.Any(a => a.CustomerOid.Equals(customer.CustomerOid)))
						{
							x = zeilen.First(f => f.CustomerOid.Equals(customer.CustomerOid));
						}
						else
						{
							var c = DAOFactory.GenericDAO.GetByID<Customer>(customer.CustomerOid);
							x.CustomerOid = customer.CustomerOid;
							x.CustomerFirstname = c.Person.FirstName;
							x.CustomerLastname = c.Person.LastName;

							SetCustomerVarFields(c, x);

							zeilen.Add(x);
						}
						var index = booking.Datum.Value.Day - 1;
						x.AttendanceCounts[index]++;
					}
				}
			}
			zeilen.Sort((a, b) => String.Format("{0}_{1}", a.CustomerLastname, a.CustomerFirstname).CompareTo(String.Format("{0}_{1}", b.CustomerLastname, b.CustomerFirstname)));
			pRO.Zeilen = zeilen;
			pRO.Gesamtsumme = zeilen.Select(s => s.AttendanceCounts.Sum()).Sum();

			return pRO;
		}

		private static void SetCustomerVarFields(Customer c, ErgDiensteTeilnahmeMonatZeile x)
		{
			foreach (var vv in c.VarFieldValueList)
			{
				var varFieldValue = Utils.XMLDeserializeFromString(vv.SerializedValue, Type.GetType(vv.TypeName));
				if (varFieldValue != null)
				{
					if (vv.VarFieldDefOid.Value == 1)
					{
						x.Klient = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 2)
					{
						x.Abteilung = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 3)
					{
						x.Wohnort = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 4)
					{
						x.Status = varFieldValue.ToString();
					}
					else if (vv.VarFieldDefOid.Value == 5)
					{
						x.KT = varFieldValue.ToString();
					}
				}
			}
			List<String> betreuungsarten = new List<String>();
			foreach (var v2o in c.ValueList)
			{
				if (v2o.Entry.Type == ValueListEntryType.CustomerCareType)
				{
					betreuungsarten.Add(v2o.Entry.Value);
				}
			}

			betreuungsarten.Sort((a, b) => a.CompareTo(b));

			foreach (var ba in betreuungsarten)
			{
				if (String.IsNullOrEmpty(x.Betreuungsart))
				{
					x.Betreuungsart = ba;
				}
				else
				{
					x.Betreuungsart += ", " + ba;
				}
			}
		}
		
	}

	public class ErgDiensteTeilnahmeMonatZeile
	{
		public long CustomerOid { get; set; }

		public string CustomerFirstname { get; set; }

		public string CustomerLastname { get; set; }

		public string AttendanceCountOnDay1 { get { return AttendanceCounts[0] == 0 ? string.Empty : AttendanceCounts[0].ToString(); } }

		public string AttendanceCountOnDay2 { get { return AttendanceCounts[1] == 0 ? string.Empty : AttendanceCounts[1].ToString(); } }

		public string AttendanceCountOnDay3 { get { return AttendanceCounts[2] == 0 ? string.Empty : AttendanceCounts[2].ToString(); } }

		public string AttendanceCountOnDay4 { get { return AttendanceCounts[3] == 0 ? string.Empty : AttendanceCounts[3].ToString(); } }

		public string AttendanceCountOnDay5 { get { return AttendanceCounts[4] == 0 ? string.Empty : AttendanceCounts[4].ToString(); } }

		public string AttendanceCountOnDay6 { get { return AttendanceCounts[5] == 0 ? string.Empty : AttendanceCounts[5].ToString(); } }

		public string AttendanceCountOnDay7 { get { return AttendanceCounts[6] == 0 ? string.Empty : AttendanceCounts[6].ToString(); } }

		public string AttendanceCountOnDay8 { get { return AttendanceCounts[7] == 0 ? string.Empty : AttendanceCounts[7].ToString(); } }

		public string AttendanceCountOnDay9 { get { return AttendanceCounts[8] == 0 ? string.Empty : AttendanceCounts[8].ToString(); } }

		public string AttendanceCountOnDay10 { get { return AttendanceCounts[9] == 0 ? string.Empty : AttendanceCounts[9].ToString(); } }

		public string AttendanceCountOnDay11 { get { return AttendanceCounts[10] == 0 ? string.Empty : AttendanceCounts[10].ToString(); } }

		public string AttendanceCountOnDay12 { get { return AttendanceCounts[11] == 0 ? string.Empty : AttendanceCounts[11].ToString(); } }

		public string AttendanceCountOnDay13 { get { return AttendanceCounts[12] == 0 ? string.Empty : AttendanceCounts[12].ToString(); } }

		public string AttendanceCountOnDay14 { get { return AttendanceCounts[13] == 0 ? string.Empty : AttendanceCounts[13].ToString(); } }

		public string AttendanceCountOnDay15 { get { return AttendanceCounts[14] == 0 ? string.Empty : AttendanceCounts[14].ToString(); } }

		public string AttendanceCountOnDay16 { get { return AttendanceCounts[15] == 0 ? string.Empty : AttendanceCounts[15].ToString(); } }

		public string AttendanceCountOnDay17 { get { return AttendanceCounts[16] == 0 ? string.Empty : AttendanceCounts[16].ToString(); } }

		public string AttendanceCountOnDay18 { get { return AttendanceCounts[17] == 0 ? string.Empty : AttendanceCounts[17].ToString(); } }

		public string AttendanceCountOnDay19 { get { return AttendanceCounts[18] == 0 ? string.Empty : AttendanceCounts[18].ToString(); } }

		public string AttendanceCountOnDay20 { get { return AttendanceCounts[19] == 0 ? string.Empty : AttendanceCounts[19].ToString(); } }

		public string AttendanceCountOnDay21 { get { return AttendanceCounts[20] == 0 ? string.Empty : AttendanceCounts[20].ToString(); } }

		public string AttendanceCountOnDay22 { get { return AttendanceCounts[21] == 0 ? string.Empty : AttendanceCounts[21].ToString(); } }

		public string AttendanceCountOnDay23 { get { return AttendanceCounts[22] == 0 ? string.Empty : AttendanceCounts[22].ToString(); } }

		public string AttendanceCountOnDay24 { get { return AttendanceCounts[23] == 0 ? string.Empty : AttendanceCounts[23].ToString(); } }

		public string AttendanceCountOnDay25 { get { return AttendanceCounts[24] == 0 ? string.Empty : AttendanceCounts[24].ToString(); } }

		public string AttendanceCountOnDay26 { get { return AttendanceCounts[25] == 0 ? string.Empty : AttendanceCounts[25].ToString(); } }

		public string AttendanceCountOnDay27 { get { return AttendanceCounts[26] == 0 ? string.Empty : AttendanceCounts[26].ToString(); } }

		public string AttendanceCountOnDay28 { get { return AttendanceCounts[27] == 0 ? string.Empty : AttendanceCounts[27].ToString(); } }

		public string AttendanceCountOnDay29 { get { return AttendanceCounts[28] == 0 ? string.Empty : AttendanceCounts[28].ToString(); } }

		public string AttendanceCountOnDay30 { get { return AttendanceCounts[29] == 0 ? string.Empty : AttendanceCounts[29].ToString(); } }

		public string AttendanceCountOnDay31 { get { return AttendanceCounts[30] == 0 ? string.Empty : AttendanceCounts[30].ToString(); } }

		public readonly int[] AttendanceCounts = new int[31];

		public string Betreuungsart { get; set; }

		public int Summe { get { return AttendanceCounts.Sum(); } }

		public string Klient { get; set; }

		public string Abteilung { get; set; }

		public string Wohnort { get; set; }

		public string Status { get; set; }

		public string KT { get; set; }
	}
}
