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

using BS.Shared;
using BeWo.Data.Access;
using BeWo.Data.Entities;


namespace BeWo.Report.ReportObjects
{
    public class AdditionalServiceBookingRO : AbstractBeWoReportObject<AdditionalServiceBookingRO>
    {
        public String Month { get; set; }

        public int Year { get; set; }

        public String AdditionalServiceRegion { get; set; }

        public String AdditionalService { get; set; }

        public List<AdditionalServiceBookingRow> Rows { get; set; }

        public static AdditionalServiceBookingRO Create(AdditionalServiceRegion region, Monat month, int year)
        {
            var result = new AdditionalServiceBookingRO
                {
                    Rows = new List<AdditionalServiceBookingRow>(),
                    AdditionalServiceRegion = region.Name,
                    AdditionalService = region.AdditionalService.Name,
                    Month = month.ToString(),
                    Year = year
                };

            var additionalServiceBookings =
                DAOFactory.GenericDAO.GetAll<AdditionalServiceBooking>()
                          .Where(
                              booking =>
                              booking.AdditionalServiceRegion == region &&
                              booking.Datum.Value.Month == (int)month &&
                              booking.Datum.Value.Year == year);

            var c2aodList = new Dictionary<string, string[]>();

            foreach (var item in additionalServiceBookings)
            {
                if (item.Datum == null) continue;
                foreach (var e in item.Customer2AddServiceBookings)
                {
	                var customer = DAOFactory.GenericDAO.GetByID<Customer>(e.CustomerOid);

					var key = customer.Person.LastName + ", " + customer.Person.FirstName;
                    if (c2aodList.ContainsKey(key))
                    {
                        c2aodList[key][item.Datum.Value.Day - 1] = (e.Participated ? "P" : "");
                    }
                    else
                    {
                        var attendancies = new string[31];
                        attendancies[item.Datum.Value.Day - 1] = (e.Participated ? "P" : "");

                        c2aodList.Add(key, attendancies);
                    }
                }
            }

            foreach (var item in c2aodList)
            {
                result.Rows.Add(new AdditionalServiceBookingRow
                    {
                        Customer = item.Key,
                        AttendanceOnDay1 = item.Value[0] ?? "",
                        AttendanceOnDay2 = item.Value[1] ?? "",
                        AttendanceOnDay3 = item.Value[2] ?? "",
                        AttendanceOnDay4 = item.Value[3] ?? "",
                        AttendanceOnDay5 = item.Value[4] ?? "",
                        AttendanceOnDay6 = item.Value[5] ?? "",
                        AttendanceOnDay7 = item.Value[6] ?? "",
                        AttendanceOnDay8 = item.Value[7] ?? "",
                        AttendanceOnDay9 = item.Value[8] ?? "",
                        AttendanceOnDay10 = item.Value[9] ?? "",
                        AttendanceOnDay11 = item.Value[10] ?? "",
                        AttendanceOnDay12 = item.Value[11] ?? "",
                        AttendanceOnDay13 = item.Value[12] ?? "",
                        AttendanceOnDay14 = item.Value[13] ?? "",
                        AttendanceOnDay15 = item.Value[14] ?? "",
                        AttendanceOnDay16 = item.Value[15] ?? "",
                        AttendanceOnDay17 = item.Value[16] ?? "",
                        AttendanceOnDay18 = item.Value[17] ?? "",
                        AttendanceOnDay19 = item.Value[18] ?? "",
                        AttendanceOnDay20 = item.Value[19] ?? "",
                        AttendanceOnDay21 = item.Value[20] ?? "",
                        AttendanceOnDay22 = item.Value[21] ?? "",
                        AttendanceOnDay23 = item.Value[22] ?? "",
                        AttendanceOnDay24 = item.Value[23] ?? "",
                        AttendanceOnDay25 = item.Value[24] ?? "",
                        AttendanceOnDay26 = item.Value[25] ?? "",
                        AttendanceOnDay27 = item.Value[26] ?? "",
                        AttendanceOnDay28 = item.Value[27] ?? "",
                        AttendanceOnDay29 = item.Value[28] ?? "",
                        AttendanceOnDay30 = item.Value[29] ?? "",
                        AttendanceOnDay31 = item.Value[30] ?? ""
                    });
            }

            result.Rows.Sort((a, b) => a.Customer.CompareTo(b.Customer));
            return result;
        }
    }

    public class AdditionalServiceBookingRow
    {
        public string Customer { get; set; } // Nachname, Vorname

        public string AttendanceOnDay1 { get; set; } // O -> nicht anwesend, P -> anwesend

        public string AttendanceOnDay2 { get; set; }

        public string AttendanceOnDay3 { get; set; }

        public string AttendanceOnDay4 { get; set; }

        public string AttendanceOnDay5 { get; set; }

        public string AttendanceOnDay6 { get; set; }

        public string AttendanceOnDay7 { get; set; }

        public string AttendanceOnDay8 { get; set; }

        public string AttendanceOnDay9 { get; set; }

        public string AttendanceOnDay10 { get; set; }

        public string AttendanceOnDay11 { get; set; }

        public string AttendanceOnDay12 { get; set; }

        public string AttendanceOnDay13 { get; set; }

        public string AttendanceOnDay14 { get; set; }

        public string AttendanceOnDay15 { get; set; }

        public string AttendanceOnDay16 { get; set; }

        public string AttendanceOnDay17 { get; set; }

        public string AttendanceOnDay18 { get; set; }

        public string AttendanceOnDay19 { get; set; }

        public string AttendanceOnDay20 { get; set; }

        public string AttendanceOnDay21 { get; set; }

        public string AttendanceOnDay22 { get; set; }

        public string AttendanceOnDay23 { get; set; }

        public string AttendanceOnDay24 { get; set; }

        public string AttendanceOnDay25 { get; set; }

        public string AttendanceOnDay26 { get; set; }

        public string AttendanceOnDay27 { get; set; }

        public string AttendanceOnDay28 { get; set; }

        public string AttendanceOnDay29 { get; set; }

        public string AttendanceOnDay30 { get; set; }

        public string AttendanceOnDay31 { get; set; }
    }
}
