import squint.*;
import javax.swing.*;

/*
 * A simple SMTP Client that connects to the
 * cs.williams.edu mail server and delivers a
 * message.
 */
public class SMTPClient extends GUIManager {
    // Size of the program's window
    private final int WINDOW_WIDTH = 450;
    private final int WINDOW_HEIGHT = 500;

    // amount of space to allow in a text field for an email address
    private final int ADDR_WIDTH = 13;

    // how many characters wide program's text areas should be
    private final int AREA_WIDTH = 35;

    // how many lines tall the message area should be
    private final int MESSAGE_LINES = 15;

    // how many lines tall the log area should be
    private final int LOG_LINES = 5;

    // address of SMTP server to use
    private final String SMTP_SERVER = "cs.williams.edu";

    // Standard port for connection to an SMTP server
    private final int SMTP_PORT = 25;

    // Fields for to and from addresses
    private JTextField from;
    private JTextField to;

    // Area in which user can type message body
    private JTextArea message;

    // Area in which responses from server will be logged
    private JTextArea log;

    /**
     * Place fields and text areas on screen to enable user to
     * enter mail together with a send button.
     */
    public SMTPClient() {
        // Create window to hold all the components
        this.createWindow(WINDOW_WIDTH, WINDOW_HEIGHT);

        JPanel curPane = new JPanel();

        curPane.add(new JLabel("To:"));
        to = new JTextField(ADDR_WIDTH);
        curPane.add(to);
        contentPane.add(curPane);

        curPane = new JPanel();
        curPane.add(new JLabel("From:"));
        from = new JTextField(ADDR_WIDTH);
        curPane.add(from);
        contentPane.add(curPane);

        contentPane.add(new JLabel("Enter your message below"));

        message = new JTextArea(MESSAGE_LINES, AREA_WIDTH);
        contentPane.add(new JScrollPane(message));

        contentPane.add(new JButton("Send"));

        log = new JTextArea(LOG_LINES, AREA_WIDTH);
        contentPane.add(new JScrollPane(log));
        log.setEditable(false);
    }

    /*
     * Send a message when the button is clicked
     */
    public void buttonClicked() {
        NetConnection toServer;
        toServer = new NetConnection(SMTP_SERVER, SMTP_PORT);
        log.append(toServer.in.nextLine() + "\n");

        toServer.out.println("HELO " + this.getHostName());
        log.append(toServer.in.nextLine() + "\n");

        toServer.out.println("MAIL FROM: <" + from.getText() + ">");
        log.append(toServer.in.nextLine() + "\n");

        toServer.out.println("RCPT TO: <" + to.getText() + ">");
        log.append(toServer.in.nextLine() + "\n");

        toServer.out.println("DATA");
        log.append(toServer.in.nextLine() + "\n");

        toServer.out.println(message.getText());
        toServer.out.println(".");
        log.append(toServer.in.nextLine() + "\n");

        toServer.out.println("QUIT");
        log.append(toServer.in.nextLine() + "\n");

        toServer.close();
    }
}
