using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DataClasses;
using DataRepositories;
using WebApplication.Models;

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

        //
        // GET: /TimeTable/
        [AllowAnonymous]
        public ActionResult Index(int? RoomId)
        {
            var roomRepository = new Repository<Room>(context);
            var model = roomRepository.GetAll().ToList();

            if (model.Count > 0)
            {
                ViewBag.RoomId = RoomId ?? model.First().Id;
            }
            else
            {
                model.Add(new Room() { Id = -1, Location = "Žiadne miestnosti" });
            }

            return View(model);
        }


        //
        // GET: /TimeTable/_RoomTimeTablePartial
        [ChildActionOnly]
        [AllowAnonymous]
        public PartialViewResult RoomTimeTablePartial(int? RoomId)
        {
            var schooldayRepository = new Repository<SchoolDayRoom>(context);
            var model = schooldayRepository.GetTimeTableForRoom(RoomId);

            return PartialView("_TimeTablePartial", model);
        }


        //
        // GET: /TimeTable/StudentTimeTable
        [RoleAuthorize(Role = RoleEnum.Students)]
        public ActionResult StudentTimeTable()
        {
            return View();
        }


        //
        // GET: /TimeTable/_RoomTimeTablePartial
        [ChildActionOnly]
        [RoleAuthorize(Role = RoleEnum.Students)]
        public PartialViewResult StudentTimeTablePartial()
        {
            var schooldayRepository = new Repository<SchoolDayRoom>(context);
            var model = schooldayRepository.GetAll().ToList();

            ViewBag.LoginName = SimpleMemberShipUser.CurrentUserName();
            return PartialView("_TimeTablePartial", model);
        }


        //
        // GET: /TimeTable/RoomRegistration
        [RoleAuthorize(Role = RoleEnum.Teachers)]
        public ActionResult RoomRegistration()
        {
            var model = new RoomRegistration();
            model.LoadData(context);

            var rooms = model.Rooms.Cast<Room>().Select(x => x.Id).ToList();

            ViewBag.DefaultRoomId = (rooms.Count > 0) ? rooms.FirstOrDefault() : -1;

            return View(model);
        }


        //
        // GET: /TimeTable/RoomRegistration
        [RoleAuthorize(Role = RoleEnum.Teachers)]
        [HttpPost]
        public ActionResult RoomRegistration(RoomRegistration roomRegistration)
        {
            if (ModelState.IsValid && !roomRegistration.HiddenPostBack)
            {
                var schooldayRepository = new Repository<SchoolDayRoom>(context);
                var errors = schooldayRepository.Insert(new SchoolDayRoomParameters(roomRegistration.ParseSelectedItems(), context));

                if (errors.Any())
                {
                    errors.ForEach(e => ModelState.AddModelError("", e));
                }
                else
                {
                    context.SaveChanges();
                }
            }

            roomRegistration.LoadData(context);
            return View(roomRegistration);
        }


        //
        // GET: /TimeTable/TimeTableRegister
        [RoleAuthorize(Role = RoleEnum.Students)]
        public ActionResult TimeTableRegister()
        {
            var lessonRepository = new Repository<Lesson>(context);
            var lessons = lessonRepository.GetAll();

            ViewBag.Lessons = lessons;

            return View();
        }


        //
        // GET: /TimeTable/_TimeTableRegisterPartial
        [ChildActionOnly]
        [RoleAuthorize(Role = RoleEnum.Students)]
        public PartialViewResult TimeTableRegisterPartial()
        {
            var schooldayRepository = new Repository<SchoolDayRoom>(context);
            var model = schooldayRepository.GetAll().ToList();

            ViewBag.LoginName = SimpleMemberShipUser.CurrentUserName();
            return PartialView("_TimeTablePartial", model);
        }


        //
        // GET: /TimeTable/DoTimeTableRegister
        [RoleAuthorize(Role = RoleEnum.Students)]
        [HttpPost]
        public ActionResult DoTimeTableRegister(int? lessonId, string submit, string cancel)
        {
            if (lessonId > 0)
            {
                string currrentUserName = SimpleMemberShipUser.CurrentUserName();
                var lessonRepository = new Repository<Lesson>(context);
                Lesson lesson = lessonRepository.GetById(lessonId);
                var userRepository = new Repository<UserProfile>(context);
                var currentuser = (from u in userRepository.GetAll()
                                   where u.Login == currrentUserName
                                   select u).FirstOrDefault();

                if (!string.IsNullOrEmpty(submit))
                {
                    lesson.UserProfiles.Add(currentuser);
                }

                if (!string.IsNullOrEmpty(cancel))
                {
                    lesson.UserProfiles.Remove(currentuser);
                }

                context.SaveChanges();
            }

            return RedirectToAction("TimeTableRegister");
        }
    }
}
