﻿using Cwc.Connect.Web.Models;
using Cwc.Connect.Web.ViewModels;
using Raven.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Cwc.Connect.Web.Controllers
{
    public class AttendanceReportController : BaseController
    {
        //
        // GET: /AttendanceReport/

        public ActionResult Index()
        {
            AttendanceReportViewModel vm;
            using (var session = DocumentStore.OpenSession())
            {

                var lifeGroups = (from p in session.Query<LifeGroup>() where p.IsHidden == false orderby p.Name select p).ToList();

                vm = LoadLifeGroupsAndDatesToViewModel(session, lifeGroups);

            }
            return View(vm);
        }

        private AttendanceReportViewModel LoadLifeGroupsAndDatesToViewModel(IDocumentSession session, List<LifeGroup> lifeGroups)
        {
            var allPersons = session.Query<Person>().Take(1000).ToList();
            var numOfDays = 10;
            var vm = new AttendanceReportViewModel();
            vm.LifeGroups = new List<AttendanceReportViewModel.LifeGroup>();
            foreach (var lg in lifeGroups)
            {
                var vmLg = new AttendanceReportViewModel.LifeGroup();
                vmLg.Name = lg.Name;
                vmLg.Dates = new List<DateTimeOffset>();
                vmLg.PersonAttendance = new List<AttendanceReportViewModel.PersonAttendance>();
                var index = 0;

                //Load the last ten attendances
                var availableAttendanceRecords = (from p in lg.Attendance orderby p.Day descending select p).Take(numOfDays);
                foreach (var attendance in availableAttendanceRecords)
                {                    
                    vmLg.Dates.Add(attendance.Day);
                    var persons = from p in attendance.AttendanceRecords select p;
                    foreach (var person in persons)
                    {
                        AttendanceReportViewModel.PersonAttendance personAttendance;
                        personAttendance = (from p in vmLg.PersonAttendance where p.PersonId.Equals(person.PersonId) select p).FirstOrDefault();
                        if (personAttendance == null) //not in the view model, add this person
                        {
                            var name = (from p in allPersons where p.Id == person.PersonId select p).FirstOrDefault();
                            var disp = name == null ? "No name" : PersonController.GetPersonName(name);
                            personAttendance = new AttendanceReportViewModel.PersonAttendance()
                            {
                                PersonId = person.PersonId,
                                PersonName = disp,
                                PresenseIndicator = new bool?[availableAttendanceRecords.Count()]
                            };
                            vmLg.PersonAttendance.Add(personAttendance);
                        }
                        personAttendance.PresenseIndicator[index] = person.WasPresent;
                    }
                    index++;
                }
                
                vm.LifeGroups.Add(vmLg);
            }
            return vm;
        }

        //
        // GET: /AttendanceReport/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AttendanceReport/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AttendanceReport/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AttendanceReport/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /AttendanceReport/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AttendanceReport/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /AttendanceReport/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
