import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.Math;

/**
 * The ParticipantReader class contains code to read lines of 
 * (participant_name, integer_ranking) pairs from a file and create
 * an ordered list of those pairs.  Every line in the file must contain a
 * full (participant_name, integer_ranking) pair.
 * 
 * @author Alyce Brady
 * @version 12 December 2025
 */
public class ParticipantReader 
{

    /** Constructs an object that that will read in participant
     * information from a file.
     */
    public ParticipantReader()
    {
    }

    /** Reads in participant info from a file.
     *    @param filename  the name of the file containing participant info
     *    @return an ordered list of participants
     *    @throws IOException if the file could not be read or contains
     *                        invalid data
     */
    public K_OrderedList<ParticipantInfo> readParticipantInfo(String fileName)
            throws IOException
    {
        int lineNumber = 0;
        K_OrderedList<ParticipantInfo> participants = new K_OrderedList<>();

        // Create a scanner to read through the file.
        try (Scanner scanner =
                new Scanner(new BufferedReader(new FileReader(fileName))))
        {
            // Loop while there are more lines.
            while (scanner.hasNextLine())
            {
                lineNumber++;
                String line = scanner.nextLine();
                parseLine(line, lineNumber, participants);
            }
            return participants;
        } catch (IOException e)
        {
            // Re-throw if the I/O itself failed (e.g., file not found).
            throw e;
        }
    }

    /** Parses participant info in a line of input.
     *    @param line a line of input
     *    @param lineNumber the line number for error reporting
     *    @param participants list of participants to add to
     *    @throws IOException if the file could not be read or contains
     *                        invalid data
     */
    private void parseLine(String line, int lineNumber,
                           K_SimpleList<ParticipantInfo> participants)
            throws IOException
    {
        try (Scanner sc = new Scanner(line))
        {
            // Use commas as delimeters to separate names and values.  Any
            // spaces before or after the comma are considered part of the
            // delimiter, not part of the name or value being read in.
            sc.useDelimiter("\\s*,\\s*");

            while ( sc.hasNext() )
            {
                // Read name and ranking for one participant.
                String name = sc.next();
                int ranking = sc.nextInt();

                // Add the information to list of participants.
                participants.add(new ParticipantInfo(name, ranking));
            }
        }
        catch (NoSuchElementException e)
        {
            // CREATE A NEW EXCEPTION with more information
            // AND chaining the original exception 'e' as the CAUSE.
            String newMessage = "Line " + lineNumber +
                                " has mismatched (name, value) pair(s).";
            throw new IOException(newMessage, e); // <-- EXCEPTION CHAINING

        }
    }

}
