using Core.Entities;
namespace Core.Parsers;

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

    public StudentParser(StreamReader reader, bool ignoreBlankLines = true) : base(reader, ignoreBlankLines)
    {
    }

    public Student[] Parse()
    {
        var s = new List<Student>();

        CsvReader.Read();
        CsvReader.ReadHeader();

		while (CsvReader.Read())
		{
			var name = CsvReader.GetField("Student Name");
			if (string.IsNullOrEmpty(name))
				continue;

            var (firstName, lastName) = Student.ParseNameParts(name);


            var stateId = CsvReader.GetField("State ID")?.Trim();
			var regionalId = CsvReader.GetField("Regional ID")?.Trim();
			var nationalId = CsvReader.GetField("National ID")?.Trim();
			var grade = CsvReader.GetField("Grade");
			var tsaYearsStr = CsvReader.GetField("TSA year");
			var tsaYear = int.Parse(tsaYearsStr?[..1] ?? "1");

			var student = new Student
            {
                FirstName = firstName,
                LastName = lastName,
                Grade = Convert.ToInt32(grade),
                TsaYear = tsaYear,
                StateId = stateId,
                RegionalId = regionalId,
                NationalId = nationalId
            };
            s.Add(student);
		}

        return s.ToArray();
    }
}