﻿namespace Academy.Commands.Creating
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Abstract;
    using Core.Contracts;
    using Core.Factories;

    public class CreateLectureCommand : Command
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CreateLectureCommand"/> class.
        /// </summary>
        /// <param name="academyFactory">Academy factory.</param>
        /// <param name="engine">Engine.</param>
        public CreateLectureCommand(IAcademyFactory academyFactory, IEngine engine)
            : base(academyFactory, engine)
        {
        }

        /// <summary>
        /// Create lecture.
        /// </summary>
        /// <param name="parameters">Parameters for lecture.</param>
        /// <exception cref="ArgumentException"/>
        /// <returns>String with information.</returns>
        public override string Execute(IList<string> parameters)
        {
            var seasonId = int.Parse(parameters[0]);
            var courseId = int.Parse(parameters[1]);
            var name = parameters[2];
            var date = parameters[3];
            var trainerUsername = parameters[4];

            var course = this.Engine
                .Seasons[seasonId]
                .Courses[courseId];

            var trainer = this.Engine
                .Trainers
                .Single(x => x.Username.ToLower() == trainerUsername.ToLower());

            var lecture = this.AcademyFactory.CreateLecture(name, date, trainer);

            course
                .Lectures
                .Add(lecture);

            return $"Lecture with ID {course.Lectures.Count - 1} was created in course {seasonId}.{course.Name}.";
        }
    }
}
