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.
 *
 * Binary Search Version
 *
 * It reads the phone book from an ordered
 * 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(600,115);
        
        JPanel p = new JPanel();
        byNameButton = new JButton("Search By Name");

        criteria = new JTextField("", 20);

        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("ordered-students.txt");
    }

    // 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());
        }
    }

    // use binary search to find the student in the array
    // of students
    private int binarySearch(String match, int low, int high) {
        if (low > high) {
            return -1;
        } else {
            int mid = (low + high) / 2;
            String midName = students[mid].getName();

            if (match.equals(midName)) {
                return mid;
            } else if (midName.compareTo(match) < 0) {
                return binarySearch(match, mid + 1, high);
            } else {
                return binarySearch(match, low, mid - 1);
            }
        }
    }

    // In this version, just call binary search
    // with the appropriate low and high
    private int indexOfStudent(String match) {
        return binarySearch(match, 0, count - 1);
    }

     public void actionPerformed(ActionEvent event) {
        int index = indexOfStudent(criteria.getText());

        if (index != -1) {
            phoneNumber.setText("597-" + students[index].getPhone());
        } else {
            phoneNumber.setText("???");
        }
    }
}
