using Core.Entities;

namespace Core.Parsers;

public class AssignmentRequirementParser : CsvParserBase
{
	public AssignmentRequirementParser(FileSystemInfo csvFile, bool ignoreBlankLines = true) : base(csvFile, ignoreBlankLines)
	{
	}

	public AssignmentRequirement[] Parse(ICollection<EventDefinition> events, ICollection<Student> students)
	{
		var assumptions = new List<AssignmentRequirement>();

		CsvReader.Read();
		CsvReader.ReadHeader();
		var studentColumns =
			CsvReader.HeaderRecord.Select(h => h.Trim()).Where(h => !string.IsNullOrEmpty(h)).ToArray();

		var studentArray = 
            studentColumns
                .Select(c => students.FirstOrDefault(s => s.FirstName == c)).ToArray();

		while (CsvReader.Read())
		{
			var eventShortName= CsvReader.GetField(0);

			var evt = events.FirstOrDefault(e => e.ShortName == eventShortName);
			if (evt == null)
				throw new Exception($"Could not find eventDefinition named {eventShortName}");
			for (int i = 0; i <= studentArray.Length; i++)
			{
				var field = CsvReader.GetField(i + 1);
				switch (field)
				{
					case "x":
					case "X":
						assumptions.Add(new AssignmentRequirement(evt, studentArray[i], Requirement.Exclude));
						break;
					case "i":
					case "I":
						assumptions.Add(new AssignmentRequirement(evt, studentArray[i], Requirement.Include));
						break;
					default:
						break;
				}
				
			}
		}


		return assumptions.ToArray();
	}
}