import objectdraw.*;

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

import java.io.*;

import java.net.*;

import javax.swing.*;


// A class that allows one to fetch an HTML file from the web and
// view its contents
//@width 800
//@height 600
public class Moozilla extends Controller implements ActionListener,
    ItemListener {
    // Width of area for URL entry
    private static final int URLFIELDWIDTH = 45;

    // Dimensions for HTML display area
    private static final int HTMLWIDTH = 60;
    private static final int HTMLHEIGHT = 24;

    // Dimensions for links display area
    private static final int LINKSWIDTH = 60;
    private static final int LINKSHEIGHT = 10;

    // Large font for GUI controls
    private Font bigFont = new Font("Times", Font.PLAIN, 16);

    // Area in which HTML is displayed
    private JTextArea htmlText;

    // The "load" button
    private JButton doit;

    // Url controls
    private JTextField urlName;
    private JComboBox favorites;

    // Set up all GUI controls and their listeners
    public void begin() {
        resize(800,600);
        Container panel = getContentPane();
        panel.setLayout(new FlowLayout());

        // Create a "bookmarks" menu
        panel.add(new JLabel("Favorites:"));
        favorites = new JComboBox();
        favorites.setFont(bigFont);
        favorites.addItem("http://www.yahoo.com/index.html");
        favorites.addItem("http://www.nytimes.com/index.html");
        favorites.addItem("http://www.cs.williams.edu/index.html");
        favorites.addItem("http://www.cs.williams.edu/~cs134/index.html");

        favorites.addItemListener(this);
        panel.add(favorites);

        // Create a text area into which a URL may be entered
        urlName = new JTextField("Enter URL", URLFIELDWIDTH);
        urlName.setFont(bigFont);
        panel.add(urlName);

        // Create the button used to start fetching a web page
        doit = new JButton("Fetch HTML");
        doit.setFont(bigFont);
        doit.addActionListener(this);
        panel.add(doit);

        // Create the text areas in which HTML and links will be displayed.
        htmlText = new JTextArea(HTMLHEIGHT, HTMLWIDTH);
        htmlText.setFont(bigFont);
        panel.add(new JScrollPane(htmlText), BorderLayout.CENTER);

        panel.validate();
    }

    // If item is selected from "favorites" menu, enter it as current URL
    public void itemStateChanged(ItemEvent e) {
        urlName.setText(favorites.getSelectedItem().toString());
    }

    // Fetch the given file from server.
    private String getWebPage(String server, String file) {
        String response = "";
        
        try {
            // open the socket to the server
            Socket tcpSocket = new Socket(server, 80);

            // construct the streams
            BufferedReader input =
                new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));

            PrintWriter output =
                new PrintWriter(new OutputStreamWriter(tcpSocket.getOutputStream()), true);

            // send the GET request
            output.println("GET " + file);
            
            // read input until the end of stream
            String line = input.readLine();
            while (line != null) {
                response = response + line + "\n";
                line = input.readLine();
            }

            tcpSocket.close();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex);
        }

        return response;
    }

    // When the "Fetch" button is pressed, 
    // try to get the HTML, display and search it.
    public void actionPerformed(ActionEvent e) {
        String urlString = urlName.getText();

        try {
            // break url into host and path
            URL url = new URL(urlString);
            String fullpage = getWebPage(url.getHost(), url.getPath());
            htmlText.setText(fullpage);
        } catch (MalformedURLException ex) {
            JOptionPane.showMessageDialog(this, e);
        }
    }
}

