package uiquiz;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

//3.1
public class SchoolManager {

    //3.2
    private School schoolArr[] = new School[30];
    private int size = 0;

    //3.5
    public SchoolManager() {

        try {
            Scanner scFile = new Scanner(new File("Participants.txt"));

            while (scFile.hasNext()) {
                String line = scFile.nextLine();

                Scanner scLine = new Scanner(line).useDelimiter("#");

                String schoolName = scLine.next();
                String schoolArea = scLine.next();
                String studID = scLine.next();
                int studIQ = scLine.nextInt();

                Student pupil = new Student(studID, studIQ);      //we create a student object and store the student's data (id number & iq) in there (in its fields).

                if (fetchSchool(schoolName) == null) {     //in other words: if the school is not yet stored in the array.

                    Student [] learner = new Student[10];
                    schoolArr[size] = new School(schoolName, schoolArea,learner); //we then create a new object for the school and store the school name and area in its fields.

                    schoolArr[size].insertStudent_ID_IQ(pupil);                // we place the student's data in the student's array (for that school).
                    size++;
                } else {
                    fetchSchool(schoolName).insertStudent_ID_IQ(pupil);

                }
            }

        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Error. The text file could not be found.");
        }

    }

    //3.3
    public String fetchArea(String schoolName) {
        String areaOfSchool = "";

        boolean found = false;

        for (int i = 0; i < size; i++) {
            String institute = schoolArr[i].getSchool();
            if (institute.equals(schoolName)) {     //OR: if (schoolArr[i].getSchool().equals(schoolName)
                areaOfSchool = schoolArr[i].getArea();
            }
        }
        return areaOfSchool;

    }

    //3.4
    public School fetchSchool(String schoolName) {
        School college = null;

        boolean found = false;

        for (int i = 0; i < size; i++) {
            String institute = schoolArr[i].getSchool();
            if (institute.equals(schoolName)) {     //OR: if (schoolArr[i].getSchool().equals(schoolName)

                college = schoolArr[i];
            }
        }
        return college;

    }

    //3.6
    public String displaySchool() {
        String output = "";

        for (int i = 0; i < size; i++) {
            output = output + schoolArr[i].toString();
        }

        return output;
    }

    //3.7
    public int getMaxIQ(String schoolName) {

        int highestIQ = 0;

        for (int i = 0; i < size; i++) {
            String institute = schoolArr[i].getSchool();
            if (institute.equals(schoolName)) {     //OR: if (schoolArr[i].getSchool().equals(schoolName)
                highestIQ = schoolArr[i].maxIQ();
            }
        }
        return highestIQ;
    }
    
    

    //3.8
    public String schoolsAboveAverage() {      //Like the displaySchool method but using other methods instead of toString.
        String output = "";
 

        for (int i = 0; i < size; i++) {
            output = output + schoolArr[i].getSchool() + " : " + schoolArr[i].countAboveAvg() + "\n";
        }

        return output;
    }

}
