import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Library extends JFrame {
    JPanel northPanel = new JPanel(new GridLayout(2,2));
    JTextArea display = new JTextArea(8,40);
    Vector bookList = new Vector();
    JButton addButton = new JButton("Add New Item");
    JButton allButton = new JButton("Show All Books");
    JLabel searchLabel = new JLabel("Enter Keyword :");
    JTextField searchTextField= new JTextField();
    
    // The frame for input books
    JFrame inputBookFrame = new JFrame();
    JLabel inputAuthorLabel = new JLabel("Name :");
    JLabel inputTitleLabel = new JLabel("Title :");
    JTextField inputAuthorTextField = new JTextField(30);
    JTextField inputTitleTextField = new JTextField(30);
    JButton inputAddButton = new JButton("Add");
    
    
    public Library() {
	display.setLineWrap(true);
	display.setWrapStyleWord(true);
	display.setEditable(false);
	
	northPanel.add(searchLabel);
	northPanel.add(searchTextField);
	northPanel.add(addButton);
	northPanel.add(allButton);
	getContentPane().add(northPanel, BorderLayout.NORTH);
	getContentPane().add(new JScrollPane(display));
	

	
	setUpInputBookFrame();
	
	addButton.addActionListener(new AddButtonListener());
	allButton.addActionListener(new ShowAllButtonListener());
	searchTextField.addActionListener(new SearchListener());
    }
    
    public void setUpInputBookFrame() {
	JPanel tempWestPanel = new JPanel(new GridLayout(0,1));
	JPanel tempCenterPanel = new JPanel(new GridLayout(0,1));
	tempWestPanel.add(inputAuthorLabel);
	tempWestPanel.add(inputTitleLabel);
	tempCenterPanel.add(inputAuthorTextField);
	tempCenterPanel.add(inputTitleTextField);
	inputBookFrame.getContentPane().add(tempCenterPanel);
	inputBookFrame.getContentPane().add(tempWestPanel, BorderLayout.WEST);
	inputBookFrame.getContentPane().add(inputAddButton, BorderLayout.SOUTH);
	
	inputAddButton.addActionListener(new InputAddButtonListener());
    }
    
    // add the book to the date base
    public void addBook(Book book) {
	// add to the bookList
	bookList.add(book);
    }
    
    public void showAll() {
	display.setText("");
	Book tempBook = null;
	for(int i = 0; i < bookList.size(); i++) {
	    display.append((i+1) + ".\n");
	    // the follow line casts Object to Book
	    tempBook = (Book)bookList.get(i); 
	    display.append(tempBook.showInfo());
	}
    }
    
    
    class AddButtonListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    inputBookFrame.pack();
	    inputBookFrame.setVisible(true);
	}
    }
    
    class InputAddButtonListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    addBook(new Book(inputAuthorTextField.getText(),
	                     inputTitleTextField.getText()));
	    inputBookFrame.setVisible(false);
	    inputAuthorTextField.setText("");
	    inputTitleTextField.setText("");
	}
    }
    
    class ShowAllButtonListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    showAll();
	}
    }
    
    class SearchListener implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    String keyword = searchTextField.getText();
	    display.setText("");
	    int counter = 0;
	    Book tempBook;
	    for(int i = 0; i < bookList.size(); i++ ){
		tempBook = (Book)bookList.get(i);
		if(tempBook.doesTitleContain(keyword) || 
		  tempBook.doesAuthorContain(keyword)) {
		    counter++;
		    display.append(counter + ".\n");
		    display.append(tempBook.showInfo());
		  }
	    }
	    
	    if(counter == 0) {
		display.append("No book matches the keywork is found\n");
	    }
	}
    }
    
    public static void main(String[] args) {
        Library lib = new Library();
	lib.pack();
	lib.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	lib.setVisible(true);
    }
}
	
	
