/// <summary>
        /// You should write tests for this function. You will also need to
        /// modify it, such that it will correctly return the name of the main
        /// teacher of each course.
        /// </summary>
        /// <param name="semester"></param>
        /// <param name="page">1-based index of the requested page.</param>
        /// <returns></returns>
        public Envelope<CourseInstanceDTO> GetCourseInstancesBySemester(string semester = null, int page = 1, string language = "en-GB")
        {
            const int PAGE_SIZE = 10;

            if (page < 1) throw new AppValidationException("PAGE_OUT_OF_BOUNDS");

            if (string.IsNullOrEmpty(semester))
            {
                semester = "20153";
            }

            if(language != "is-IS")
            {
                language = "en-GB";
            }

            List<CourseInstanceDTO> courses;

            if(language == "is-IS")
            {
                courses = (from c in _courseInstances.All()
                    join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
                    where c.SemesterID == semester
                    orderby ct.CourseID
                    select new CourseInstanceDTO
                    {
                        Name = ct.Name,
                        TemplateID = ct.CourseID,
                        CourseInstanceID = c.ID,
                        MainTeacher = ""
                    }).Skip((page - 1) * PAGE_SIZE).Take(PAGE_SIZE).ToList();
            }
            else
            {
                courses = (from c in _courseInstances.All()
                           join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
                           where c.SemesterID == semester
                           orderby ct.CourseID
                           select new CourseInstanceDTO
                           {
                               Name = ct.NameEN,
                               TemplateID = ct.CourseID,
                               CourseInstanceID = c.ID,
                               MainTeacher = ""
                           }).Skip((page - 1) * PAGE_SIZE).Take(PAGE_SIZE).ToList();
            }

            var coursesCount = _courseInstances.All().Where(x => x.SemesterID == semester).Count();
            int pageCount = (coursesCount + PAGE_SIZE - 1) / PAGE_SIZE;

            Envelope<CourseInstanceDTO> envelope = new Envelope<CourseInstanceDTO>
            {
                Items = courses,
                PageCount = pageCount,
                PageNumber = page,
                PageSize = PAGE_SIZE,
                TotalNumberOfItems = coursesCount
            };

            return envelope;
        }
 /// <summary>
 /// You should write tests for this function. You will also need to
 /// modify it, such that it will correctly return the name of the main
 /// teacher of each course.
 /// </summary>
 /// <param name="semester"></param>
 /// <param name="page">1-based index of the requested page.</param>
 /// <returns></returns>
 public Envelope GetCourseInstancesBySemester(string semester = null, int page = 1, bool english = false)
 {
     if (string.IsNullOrEmpty(semester))
     {
         semester = "20153";
     }
     int skip = page - 1;
     skip *= numberOnPage;
     int pagenum = page*numberOnPage;
     var totalNumber = _courseInstances.All().Count();
     var courses = (from c in _courseInstances.All()
         join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
         where c.SemesterID == semester
         select new CourseInstanceDTO
         {
             Name = english ? ct.NameEN : ct.Name,
             TemplateID = ct.CourseID,
             CourseInstanceID = c.ID,
             MainTeacher = "" // Hint: it should not always return an empty string!
         }).ToList().Skip(skip).Take(pagenum).ToList();
     var pageCount = Math.Ceiling(((decimal)totalNumber/(decimal)numberOnPage));
     Envelope myEnvelope = new Envelope();
     myEnvelope.page = new Page();
     myEnvelope.courses = courses;
     myEnvelope.page.PageCount = (int) pageCount;
     myEnvelope.page.PageNumber = page;
     myEnvelope.page.PageSize = numberOnPage;
     myEnvelope.page.TotalNumberOfItems = totalNumber;
     return myEnvelope;
 }
        /// <summary>
        /// You should write tests for this function. You will also need to
        /// modify it, such that it will correctly return the name of the main
        /// teacher of each course.
        /// </summary>
        /// <param name="language"></param>
        /// <param name="semester"></param>
        /// <param name="page">1-based index of the requested page.</param>
        /// <returns></returns>
        public Envelope<List<CourseInstanceDTO>> GetCourseInstancesBySemester(string language, string semester = null, int page = 1)
		{
            const int PAGE_SIZE = 10;
            List<CourseInstanceDTO> courses;

            if (string.IsNullOrEmpty(semester))
			{
				semester = "20153";
			} 

            // Get the list of courses for the page with name in accepted language
            if (language == "is")
            {
                courses = (from c in _courseInstances.All()
                               join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
                               orderby c.ID
                               where c.SemesterID == semester
                               select new CourseInstanceDTO
                               {
                                   Name             = ct.Name, // Icelandic
                                   TemplateID       = ct.CourseID,
                                   CourseInstanceID = c.ID,
                                   MainTeacher      = ""
                               }).Skip((page - 1) * PAGE_SIZE).Take(PAGE_SIZE).ToList();
            }
            else
            {
                courses = (from c in _courseInstances.All()
                               join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
                               orderby c.ID
                               where c.SemesterID == semester
                               select new CourseInstanceDTO
                               {
                                   Name             = ct.NameEN, // English
                                   TemplateID       = ct.CourseID,
                                   CourseInstanceID = c.ID,
                                   MainTeacher      = ""
                               }).Skip((page - 1) * PAGE_SIZE).Take(PAGE_SIZE).ToList();
            }

            // Get the count of all courses on the given semester
            var ciCount = (from c in _courseInstances.All()
                                        join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
                                        where c.SemesterID == semester
                                        select c).Count();
            // Calculate the page count
            var pageCount = (int)Math.Ceiling((double)ciCount / PAGE_SIZE);

            // Create the envelope with the page info and the list of courses
            var paging = new PagingInfo
            {
                PageCount = pageCount,
                PageSize = PAGE_SIZE,
                PageNumber = page,
                TotalNumberOfItems = courses.Count
            };

            var envelope = new Envelope<List<CourseInstanceDTO>>
            {
                Items = courses,
                Paging = paging
            };
            
            return envelope;
		}
        /// <summary>
        /// Finds CourseInstances taught on the given semester.
        /// If no language is specified, the language "is" is used instead.
        /// If no semester is given, the current semester "20153" is used instead.
        /// If no page number is given, the first page is used.
        /// If the page number exceeds the total number of pages, an empty list is returned.
        /// </summary>
        /// <param name="language">The language the user has specified for the name of each course.</param>
        /// <param name="semester">The semester to get courses from</param>
        /// <param name="page">The number of the page to get from the list of course instances</param>
        /// <returns>A List of CourseInstanceDTOs taught on the given semester</returns>
        public Envelope<List<CourseInstanceDTO>> GetCourseInstancesBySemester(string language = null, string semester = null, int page = 1)
        {
            const string langIS = "is";

            const int ITEMS_PER_PAGE = 10;

            // Assign a default semester if no semester is given
            if (string.IsNullOrEmpty(semester))
            {
                semester = "20153";
            }

            // Assign a default language if no language is specified
            if (string.IsNullOrEmpty(language))
            {
                language = langIS;
            }

            // Construct the list of courses tought in the given semester
            // with the course name language set to the users preference
            var courses = (from c in _courseInstances.All()
                           join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
                           where c.SemesterID == semester
                           select new CourseInstanceDTO
                           {
                               Name = language == langIS? ct.Name : ct.NameEN,
                               TemplateID = ct.CourseID,
                               CourseInstanceID = c.ID,
                               MainTeacher = ""
                           }).OrderBy(dto => dto.CourseInstanceID).Skip((page - 1) * ITEMS_PER_PAGE).Take(ITEMS_PER_PAGE).ToList();

            // Find main teacher name
            foreach (var ciDTO in courses)
            {
                var mainTeacherRegistration = (from tr in _teacherRegistrations.All()
                    where tr.CourseInstanceID == ciDTO.CourseInstanceID
                    where tr.Type == TeacherType.MainTeacher
                    select tr).SingleOrDefault();

                if (mainTeacherRegistration != null)
                {
                    var mainTeacher = (from p in _persons.All()
                                       where p.SSN == mainTeacherRegistration.SSN
                                       select p).SingleOrDefault();

                    if (mainTeacher != null)
                    {
                        ciDTO.MainTeacher = mainTeacher.Name;
                    }
                }
            }

            // Total number of courses tought in the given semester
            var coursesTotalCount = (from c in _courseInstances.All()
                                     join ct in _courseTemplates.All() on c.CourseID equals ct.CourseID
                                     where c.SemesterID == semester
                                     select new object()).Count();

            // Get the total number of pages in the collection
            var pageCount = (int) (Math.Ceiling(((double)coursesTotalCount) / ((double)ITEMS_PER_PAGE)));

            // Construct the envelope to return
            var envelope = new Envelope<List<CourseInstanceDTO>>
            {
                Items = courses,
                Paging = new Envelope<List<CourseInstanceDTO>>.PagingInfo
                {
                    PageCount = pageCount,
                    PageNumber = page,
                    PageSize = ITEMS_PER_PAGE,
                    TotalNumberOfItems = coursesTotalCount
                }
            };

            return envelope;
        }