using Core.Entities;

namespace Core.Parsers;

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

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

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

		var studentArray = studentColumns.Select(c => students.First(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 event 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 AssignmentAssumption(evt, studentArray[i], Assumption.Exclude));
						break;
					case "i":
					case "I":
						assumptions.Add(new AssignmentAssumption(evt, studentArray[i], Assumption.Include));
						break;
					default:
						break;
				}
				
			}
		}


		return assumptions.ToArray();
	}
}