using System;
using System.Collections.Generic;

using BeWo.Data.Entities;
using BeWo.Data.Access;

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

        public DateTime ReportDateTime { get; set; }

        public List<AssessmentSheetRow> Rows { get; set; } 
        
        public static AssessmentSheetRO Create(Customer customer, DateTime dt)
        {
            var result = new AssessmentSheetRO
                {
                    Rows = new List<AssessmentSheetRow>(),
                    Customer = String.Format("{0} {1}", customer.Person.FirstName, customer.Person.LastName),
                    ReportDateTime = dt
                };

            var catOid2RowDict = new Dictionary<long, AssessmentSheetRow>();
            var categories =
                DAOFactory.SearchDAO.GetAssessmentSheetCategoryListForCustomer(customer.Oid.Value);

            foreach (var assessmentSheetCategory in categories)
            {
                var row = new AssessmentSheetRow();
                row.Category = assessmentSheetCategory.Description;
                row.Position = assessmentSheetCategory.Position;
                result.Rows.Add(row);
                catOid2RowDict.Add(assessmentSheetCategory.Oid.Value, row);
                
            }

            var entrylist = DAOFactory.SearchDAO.GetAssessmentSheetEntriesForCustomer(customer.Oid.Value, dt, dt.AddMonths(1));

            foreach (var e in entrylist)
            {
                AssessmentSheetRow row;
                if (catOid2RowDict.ContainsKey(e.AssessmentSheetCategory.Oid.Value))
                {
                    row = catOid2RowDict[e.AssessmentSheetCategory.Oid.Value];
                } 
                else
                {
                    row = new AssessmentSheetRow
                        {
                            Category = e.AssessmentSheetCategory.Description,
                            Position = e.AssessmentSheetCategory.Position
                        };
                    result.Rows.Add(row);
                    catOid2RowDict.Add(e.AssessmentSheetCategory.Oid.Value, row);
                }

                

                if (e.Day >= dt && e.Day < dt.AddMonths(1))
                {
                    var sv = new SheetValue {Value = e.AssessmentSheetValue.Sign, Color = e.AssessmentSheetValue.Color};
                    if (e.Day.Value.Day == 1)
                        row.Day1 = sv;
                    else if (e.Day.Value.Day == 2)
                        row.Day2 = sv;
                    else if (e.Day.Value.Day == 3)
                        row.Day3 = sv;
                    else if (e.Day.Value.Day == 4)
                        row.Day4 = sv;
                    else if (e.Day.Value.Day == 5)
                        row.Day5 = sv;
                    else if (e.Day.Value.Day == 6)
                        row.Day6 = sv;
                    else if (e.Day.Value.Day == 7)
                        row.Day7 = sv;
                    else if (e.Day.Value.Day == 8)
                        row.Day8 = sv;
                    else if (e.Day.Value.Day == 9)
                        row.Day9 = sv;
                    else if (e.Day.Value.Day == 10)
                        row.Day10 = sv;
                    else if (e.Day.Value.Day == 11)
                        row.Day11 = sv;
                    else if (e.Day.Value.Day == 12)
                        row.Day12 = sv;
                    else if (e.Day.Value.Day == 13)
                        row.Day13 = sv;
                    else if (e.Day.Value.Day == 14)
                        row.Day14 = sv;
                    else if (e.Day.Value.Day == 15)
                        row.Day15 = sv;
                    else if (e.Day.Value.Day == 16)
                        row.Day16 = sv;
                    else if (e.Day.Value.Day == 17)
                        row.Day17 = sv;
                    else if (e.Day.Value.Day == 18)
                        row.Day18 = sv;
                    else if (e.Day.Value.Day == 19)
                        row.Day19 = sv;
                    else if (e.Day.Value.Day == 20)
                        row.Day20 = sv;
                    else if (e.Day.Value.Day == 21)
                        row.Day21 = sv;
                    else if (e.Day.Value.Day == 22)
                        row.Day22 = sv;
                    else if (e.Day.Value.Day == 23)
                        row.Day23 = sv;
                    else if (e.Day.Value.Day == 24)
                        row.Day24 = sv;
                    else if (e.Day.Value.Day == 25)
                        row.Day25 = sv;
                    else if (e.Day.Value.Day == 26)
                        row.Day26 = sv;
                    else if (e.Day.Value.Day == 27)
                        row.Day27 = sv;
                    else if (e.Day.Value.Day == 28)
                        row.Day28 = sv;
                    else if (e.Day.Value.Day == 29)
                        row.Day29 = sv;
                    else if (e.Day.Value.Day == 30)
                        row.Day30 = sv;
                    else if (e.Day.Value.Day == 31)
                        row.Day31 = sv;
                }
            }

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

    public class AssessmentSheetRow
    {
        public String Category { get; set; }

        public int Position { get; set; }

        public SheetValue Day1 { get; set; }

        public SheetValue Day2 { get; set; }

        public SheetValue Day3 { get; set; }

        public SheetValue Day4 { get; set; }

        public SheetValue Day5 { get; set; }

        public SheetValue Day6 { get; set; }

        public SheetValue Day7 { get; set; }

        public SheetValue Day8 { get; set; }

        public SheetValue Day9 { get; set; }

        public SheetValue Day10 { get; set; }

        public SheetValue Day11 { get; set; }

        public SheetValue Day12 { get; set; }

        public SheetValue Day13 { get; set; }

        public SheetValue Day14 { get; set; }

        public SheetValue Day15 { get; set; }

        public SheetValue Day16 { get; set; }

        public SheetValue Day17 { get; set; }

        public SheetValue Day18 { get; set; }

        public SheetValue Day19 { get; set; }

        public SheetValue Day20 { get; set; }

        public SheetValue Day21 { get; set; }

        public SheetValue Day22 { get; set; }

        public SheetValue Day23 { get; set; }

        public SheetValue Day24 { get; set; }

        public SheetValue Day25 { get; set; }

        public SheetValue Day26 { get; set; }

        public SheetValue Day27 { get; set; }

        public SheetValue Day28 { get; set; }

        public SheetValue Day29 { get; set; }

        public SheetValue Day30 { get; set; }

        public SheetValue Day31 { get; set; }
    }

    public class SheetValue
    {
        public String Value { get; set; }

        public String Color { get; set; }

        public override string ToString()
        {
            return Value;
        }
    }
}