using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using DataClasses;
using DataRepositories;

namespace WebApplication.Controllers
{
    public class AdminController : Controller
    {
        private readonly DataContext context = new DataContext();

        //
        // GET: /Admin/Lessons
        [Authorize]
        public ActionResult Lessons()
        {
            var lessonRepository = new Repository<Lesson>(context);
            var model = lessonRepository.GetAll().ToList();
            return View(model);
        }


        //
        // GET: /Admin/LessonEdit
        [Authorize]
        public ActionResult LessonsEdit(int lessonId)
        {
            if (lessonId > 0)
            {
                var lessonRepository = new Repository<Lesson>(context);
                var model = lessonRepository.GetById(lessonId);
                return View(model);
            }
            return View();
        }



        //
        // GET: /Admin/LessonDelete
        [Authorize]
        public ActionResult LessonDelete(int lessonId)
        {
            var lessonRepository = new Repository<Lesson>(context);
            var lesson = lessonRepository.GetById(lessonId);

            var dayRoomRepository = new Repository<SchoolDayRoom>(context);

            var days = dayRoomRepository.GetAll().Select(d => d).ToList();

            int maxEnumValue = (int)Enum.GetValues(typeof(LessonHoursEnum)).Cast<LessonHoursEnum>().Max();

            days.ForEach(delegate(SchoolDayRoom day)
                {
                    for (int i = 1; i <= maxEnumValue; i++)
                    {
                        PropertyInfo property = typeof(SchoolDayRoom).GetProperty(((LessonHoursEnum)i).ToString());
                        if (property.GetValue(day) == lesson)
                        {
                            property.SetValue(day,null);
                        }
                    }
                }
                );

            lessonRepository.Delete(lesson);
            context.SaveChanges();

            return RedirectToAction("Lessons");
        }


        //
        // POST: /Admin/LessonSave
        [HttpPost]
        public ActionResult LessonsSave(Lesson lesson)
        {
            if (ModelState.IsValid)
            {
                var lessonRepository = new Repository<Lesson>(context);

                if (lesson.Id > 0)
                {
                    context.Entry(lesson).State = EntityState.Modified;
                    lessonRepository.Update(lesson);
                }
                else
                {
                    lessonRepository.Insert(lesson);
                }
                context.SaveChanges();

                return RedirectToAction("Lessons");
            }
            return View("LessonsEdit");
        }


        //
        // GET: /Admin/Rooms
        [RoleAuthorize(Role=RoleEnum.Administrators)]
        public ActionResult Rooms()
        {
            var lessonRepository = new Repository<Room>(context);
            var model = lessonRepository.GetAll().ToList();
            return View(model);
        }


        //
        // GET: /Admin/RoomEdit
        [RoleAuthorize(Role = RoleEnum.Administrators)]
        public ActionResult RoomEdit(int roomId)
        {
            if (roomId > 0)
            {
                var roomRepository = new Repository<Room>(context);
                var model = roomRepository.GetById(roomId);
                return View(model);
            }
            return View();
        }


        //
        // POST: /Admin/RoomSave
        [HttpPost]
        [RoleAuthorize(Role = RoleEnum.Administrators)]
        public ActionResult RoomSave(Room room)
        {
            if (ModelState.IsValid)
            {
                var roomRepository = new Repository<Room>(context);

                if (room.Id > 0)
                {
                    context.Entry(room).State = EntityState.Modified;
                    roomRepository.Update(room);
                }
                else
                {
                    roomRepository.Insert(room);
                }
                context.SaveChanges();

                return RedirectToAction("Rooms");
            }
            return View("RoomEdit");
        }


        //
        // GET: /Admin/RoomDelete
        [RoleAuthorize(Role = RoleEnum.Administrators)]
        public ActionResult RoomDelete(int roomId)
        {
            var roomRepository = new Repository<Room>(context);
            var schoolDayRepository = new Repository<SchoolDayRoom>(context);
            var lessonRepository = new Repository<Lesson>(context);

            var room = roomRepository.GetById(roomId);
            var schooldays = (from s in schoolDayRepository.GetAll()
                              where s.LessonRoom.Id == room.Id
                              select s).ToList();

            List<int> selectedIds = schooldays.Select(k => k.Id).ToList();
            var lessons = (from l in lessonRepository.GetAll()
                           where selectedIds.Contains(l.SchoolDay.Id)
                           select l).ToList();

            lessons.ForEach(s => s.SchoolDay = null);
            schooldays.ForEach(schoolDayRepository.Delete);
            roomRepository.Delete(room);

            context.SaveChanges();

            return RedirectToAction("Rooms");
        }

    }
}
