﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StudentsAndCourses.Models;
using System.Linq;

namespace School.Tests
{
    [TestClass]
    public class CourseTests
    {
        [TestMethod]
        public void CourseConstructor_ShouldCreateCourse_IfStringNameIsPassed()
        {
            var course = new Course("Jabba");

            Assert.IsInstanceOfType(course, typeof(Course));
        }

        [TestMethod]
        public void JoinCourse_ShouldAddStudentToCourseCorrectly ()
        {
            var course = new Course("6 bez 10!");
            var student = new Student("goshko", 99999);

            while (course.Coursists.Count < 29)
            {
                course.JoinCourse(student);
            }


            course.JoinCourse(student);

            var actual = course.Coursists.Last();
            var expected = student;

            Assert.AreSame(expected, actual);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void Joincourse_ShouldThrowException_IfThereAreMoreThan30Students ()
        {
            var course = new Course("6 bez 10!");
            var student = new Student("goshko", 99999);

            while (course.Coursists.Count<30)
            {
                course.JoinCourse(student);
            }

            course.JoinCourse(student);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void LeaveCourse_ShouldThrowException_IfStudentThatIsNotEnrolledTriesToLeaveCourse ()
        {
            var course = new Course("6 bez 10!");
            var student = new Student("goshko", 99999);
            var studentTwo = new Student("peshko", 98889);

            course.JoinCourse(student);
            course.LeaveCourse(studentTwo);
        }

        [TestMethod]
        public void LeaveCourse_ShouldRemoveStudentFromCourseList ()
        {
            var course = new Course("6 bez 10!");
            var student = new Student("goshko", 99999);
            var studentTwo = new Student("peshko", 98889);

            course.JoinCourse(student);
            course.JoinCourse(studentTwo);

            course.LeaveCourse(student);

            var expected = studentTwo;
            var actual = course.Coursists.First();

            Assert.AreSame(expected, actual);
        }
    }
}
