import objectdraw.*;

import java.awt.*;
import java.awt.event.*;

import java.io.*;

import javax.swing.*;


/*
 * This class implements a simple online
 * phone book for students.
 *
 * It reads the phone book from a
 * file of "student:number" lines.
 */
//@width 600
//@height 115
public class PhoneBook extends Controller implements ActionListener {
    // max number of students
    private static final int MAX = 5000;

    // array of students
    private Student[] students = new Student[MAX];

    // size of student array
    private int count = 0;

    // the name to match
    private JTextField criteria;

    // search button
    private JButton byNameButton;

    // field in which to display the phone number
    private JTextField phoneNumber;

    public void begin() {
        
        resize(400,115);
        
        JPanel p = new JPanel();
        byNameButton = new JButton("Search By Name");

        criteria = new JTextField("", 16);

        JPanel pp = new JPanel();
        pp.add(byNameButton);
        pp.add(criteria);
        getContentPane().add(pp, BorderLayout.NORTH);

        pp = new JPanel();

        phoneNumber = new JTextField(10);
        pp.add(new JLabel("Phone:"));
        pp.add(phoneNumber);

        getContentPane().add(pp, BorderLayout.SOUTH);

        byNameButton.addActionListener(this);
        criteria.addActionListener(this);

        validate();
        loadStudents("students.11.txt");
    }

    
    
    
    
    // lookup the desired student by name
    // in the array
    public void actionPerformed(ActionEvent event) {
        int index = indexOfStudent(criteria.getText(), 0);

        if (index != -1) {
            phoneNumber.setText("597-" + students[index].getPhone());
        } else {
            phoneNumber.setText("???");
        }
    }
    
    
    
    // Use linear search to find the student
    // with the given name, starting at index
    // "start".  This is implemented recursively,
    // although it could be done with a loop as well.
    private int indexOfStudent(String match, int start) {
        if (start >= count) {
            return -1;
        } else if (students[start].getName().equals(match)) {
            return start;
        } else {
            return indexOfStudent(match, start + 1);
        }
    }

    
    
    // Read the contents of the students file
    private void loadStudents(String file1) {
        try {
            BufferedReader input = new BufferedReader(new FileReader(file1));
            String line = input.readLine();

            // each line has the form "name:number"
            while (line != null) {
                int colon = line.indexOf(":");
                String name = line.substring(0, colon);
                String ph = line.substring(colon + 1);
                Student s = new Student(name, Integer.parseInt(ph));
                students[count] = s;
                count++;
                line = input.readLine();
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Error: " + e.getMessage());
        }
    }

}
