import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
import java.rmi.server.*;

public class Exec12_31Client {
  public static void main(String[] args) {
    if (args.length!=1) {
      System.out.println("Usage: java Exec12_31Client serverIP");
      System.exit(-1);
    }

    theFrame=new ClientFrame(args[0]);
    theFrame.setTitle("Client");
    theFrame.setSize(400, 300);
    theFrame.show();
  }

  protected static Frame theFrame;
}

class ClientFrame extends Frame implements ActionListener, ChatClient {
  public ClientFrame(String serverIP) {
    setLayout(new BorderLayout());

    northPanel.setLayout(new FlowLayout());
    northPanel.add(inputArea);
    northPanel.add(sendButton);
    sendButton.addActionListener(this);
    add("North", northPanel);

    centerPanel.setLayout(new FlowLayout());
    centerPanel.add(displayArea);
    displayArea.setEditable(false);
    add("Center", centerPanel);

    southPanel.setLayout(new FlowLayout());
    southPanel.add(clearInputButton);
    southPanel.add(clearDisplayButton);
    southPanel.add(quitButton);
    clearInputButton.addActionListener(this);
    clearDisplayButton.addActionListener(this);
    quitButton.addActionListener(this);
    add("South", southPanel);

    try {
      UnicastRemoteObject.exportObject(this);
      serverObject=(ChatServer) Naming.lookup("//"+serverIP+"/ChatServer");
      serverObject.register(this);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
    }
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource()==sendButton) {
      // When send button is clicked, send the message to the server
      try {
        serverObject.sendMessage(this, inputArea.getText());
      } catch (RemoteException e1) {
        e1.printStackTrace();
        System.exit(-1);
      }
    } else if (e.getSource()==clearInputButton) {
      inputArea.setText("");
    } else if (e.getSource()==clearDisplayButton) {
      displayArea.setText("");
    } else if (e.getSource()==quitButton) {
      try {
        serverObject.unregister(this);
      } catch (RemoteException e1) {
      }
      hide();

      System.exit(0);
    }
  }

  /**
   * Show the message send from server
   */
  public void showMessage(String message) throws RemoteException {
    displayArea.append(message+"\r\n");
  }

  public void setClientID(int clientID) throws RemoteException {
    this.clientID=clientID;
  }

  public int getClientID() throws RemoteException {
    return clientID;
  }

  ChatServer serverObject;
  protected int clientID;

  protected Panel northPanel=new Panel();
  protected TextArea inputArea=new TextArea("", 3, 40);
  protected Button sendButton=new Button("Send");

  protected Panel centerPanel=new Panel();
  protected TextArea displayArea=new TextArea("", 8, 40);

  protected Panel southPanel=new Panel();
  protected Button clearInputButton=new Button("Clear Input");
  protected Button clearDisplayButton=new Button("Clear Display");
  protected Button quitButton=new Button("Quit");
}

