import java.util.*;
import java.lang.*;
public class StudentGroupEditor 
{
    void writeLn(String input)
    {
        System.out.println(input);
    }
    void write(String input)
    {
        System.out.print(input);
    }
    
    public void run()
    {
        Utility util = new Utility();
        String userInput = util.readUserInput("How many students should there be in the group? ");
        OptionalInt studentCountResult = util.parseInt(userInput);
        if (!studentCountResult.isPresent() || studentCountResult.getAsInt() < 0)
        {
            writeLn("The number you entered is not a valid positive integer. Terminating...");
            return;
        }
        
        int studentCount = studentCountResult.getAsInt();
        writeLn("The group will be having " + studentCount + " student(s)");
        writeLn("You will now fill in the information for each one of them, have fun!");
        writeLn("");
        
        Student[] studentGroup = new Student[studentCount];
        for (int i = 0; i < studentCount; i++)
        {
            String firstName = util.readUserInput("Enter first name: ");
            String lastName = util.readUserInput("Enter last name: ");
            OptionalInt studentNumberResult = util.parseInt(util.readUserInput("Enter student number: "));
            if (!studentNumberResult.isPresent())
            {
                writeLn("The student number you entered was not valid.");
                return;
            }
            
            int studentNumber = studentNumberResult.getAsInt();
            
            studentGroup[i] = new Student(firstName, lastName, studentNumber);
            writeLn("Added: " + studentGroup[i].toString());
            writeLn("");
        }
        util.printStudents(studentGroup);
        Boolean editingStudentInfo = true;
        while (editingStudentInfo) 
        {
            writeLn("Enter the student number of the student you wish to update or a negative number to exit");
            userInput = util.readUserInput("Student number: ");
            OptionalInt studentNumberResult = util.parseInt(userInput);
            if (!studentNumberResult.isPresent())
            {
                writeLn("The student number you entered doesn't seem to be valid, try again");
                continue;
            }
            int studentNumber = studentNumberResult.getAsInt();
            if (studentNumber < 0) 
            {
                
                editingStudentInfo = false;
                continue;
            }
            
            Optional<Student> student = util.findStudent(studentGroup, studentNumber);
            if (!student.isPresent()) 
            {
                writeLn("Could not find a student with the student number you entered, try again.");
                writeLn("");
                continue;
            }
            Student foundStudent = student.get();
            writeLn("Student found: " + foundStudent.toString());
            
            foundStudent.setFirstName(util.readUserInput("Enter new first name: "));
            foundStudent.setLastName(util.readUserInput("Enter new last name: "));
            writeLn("The group was updated!");
            util.printStudents(studentGroup);
        }
    }
}