using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataClasses;
using DataRepositories;

namespace WebApplication.Models
{
    public class RoomRegistration
    {
        public IEnumerable Day { get; set; }
        public string SelectedDay { get; set; }
        
        public IEnumerable HourFrom { get; set; }
        public string SelectedHourFrom { get; set; }

        public IEnumerable HourTo { get; set; }
        public string SelectedHourTo { get; set; }

        public IEnumerable Rooms { get; set; }
        public string SelectedRoom { get; set; }

        public IEnumerable Lessons { get; set; }
        public string SelectedLesson { get; set; }

        public string Date { get; set; }

        public bool HiddenPostBack { get; set; }

        public void LoadData(DataContext context)
        {
            Day = from DayOfWeekEnum e in Enum.GetValues(typeof(DayOfWeekEnum))
                      select new { Id = e.ToString(), Name = StringEnum.GetStringValue(e) };

            HourFrom = from LessonHoursEnum e in Enum.GetValues(typeof(LessonHoursEnum))
                       select new { Id = e, Name = StringEnum.GetStringValue(e) };

            HourTo = from LessonHoursEnum e in Enum.GetValues(typeof(LessonHoursEnum))
                       select new { Id = e, Name = StringEnum.GetStringValue(e) };

            var roomRepository = new Repository<Room>(context);
            Rooms = roomRepository.GetAll();

            var lessonRepository = new Repository<Lesson>(context);
            Lessons = lessonRepository.GetAll();
        }


        public dynamic ParseSelectedItems()
        {
            dynamic result = new ExpandoObject();
            
            try
            {
                result.Date = Date;
                result.Day = (DayOfWeekEnum) Enum.Parse(typeof (DayOfWeekEnum), SelectedDay);
                result.HourFrom = (LessonHoursEnum) Enum.Parse(typeof (LessonHoursEnum), SelectedHourFrom);
                result.HourTo = (LessonHoursEnum) Enum.Parse(typeof (LessonHoursEnum), SelectedHourTo);
                result.LessonId = int.Parse(SelectedLesson);
                result.RoomId = int.Parse(SelectedRoom);
                result.Error = "";
                return result;
            }

            catch (Exception ex)
            {
                result.Error = "Chybný formát!";
                return result;
            }
        }
    }
}