using TaskManegementSystemOperations.Commands;
using System.Collections.Generic;
using System.Linq;
using TaskManegementSystemCore;
using TaskManegementSystemModels.Interfaces;
using TaskManegementSystemModels;

namespace TaskManegementSystemOperations
{
    public class Add_Comment_To_Task : ICommand
    {
        private readonly Factory factory;
        private readonly Repository repository;
        public Add_Comment_To_Task(Factory factory, Repository repository)
        {
            this.factory = factory;
            this.repository = repository;
        }
        public string Execute(IList<string> parameters)
        {
            string comment = parameters[0];
            string author = parameters[1];
            string taskName = parameters[2];
            ITask task = repository.AllTasks.FirstOrDefault(x => x.Title == taskName);
            return AddCommentToTask(comment, author, task);
           
        }

        public string AddCommentToTask(string content, string author, ITask task)
        {
            IMember commentAuthor = repository.People.FirstOrDefault(x => x.Name == author);

            commentAuthor.ActivityHistory.Add(new History($"Added comment to Task {task.Title}"));

            IComment comment = factory.CreateComment(content, author);
            task.Comments.Add(comment);
            return $"{author} added comment successfully to Task.";
        }
    }
}