package chat;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import util.net.*;


/**
 * Demo program to show many of the awt/swing events that
 * are generated/consumed. Illustrates XXXListeners for several
 * types of events including semantic events (e.g., ActionEvent)
 * and low-level events (e.g., FocusEvent)
 *
 * This code is meant to be fuctional rather than elegant, though
 * hopefully elegance is present to some degree.
 *
 * @author Robert Duvall
 */
public class ClientGUI extends JFrame implements InputHandler
{
    private JTextField myInput;
    private JTextArea myInputHistory;
    private JTextArea myOutput;
    private util.net.Client myClient;


    public ClientGUI (String hostName, int port)
    {
        myClient = new util.net.Client(hostName, port);
        myClient.addInputHandler(this);
        // myClient.addInputHandler(new util.net.PrintInputHandler());

        getContentPane().add(makeInputPanel(), BorderLayout.NORTH);
        getContentPane().add(makeHistoryPanel(), BorderLayout.CENTER);
        getContentPane().add(makeOutputPanel(), BorderLayout.SOUTH);

        setTitle("Chat it Up!");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        show();
    }

    public void handleInput (Object o)
    {
        myOutput.append(o.toString() + "\n");
    }

    private JComponent makeInputPanel ()
    {
        JPanel top = new JPanel();
        top.add(makeTextField(30, new BroadcastInput()));
	top.add(makeButton("Send", new BroadcastInput()));
	top.add(makeButton("Clear", new ActionListener()
	    {
                public void actionPerformed(ActionEvent e)
                {
                    myInput.setText("");
                    myInputHistory.setText("");
                    myOutput.setText("");
                }
            }));
	return top;
    }

    private JComponent makeHistoryPanel ()
    {
	myInputHistory = makeTextArea(30, 40);
	return new JScrollPane(myInputHistory);
    }

    private JComponent makeOutputPanel ()
    {
	myOutput = makeTextArea(30, 40);
	return new JScrollPane(myOutput);
    }

    private JButton makeButton (String label, ActionListener action)
    {
        JButton button = new JButton(label);
        button.addActionListener(action);
        return button;
    }

    private JTextField makeTextField (int numCharsWide, ActionListener action)
    {
	myInput = new JTextField(numCharsWide);
        myInput.addActionListener(action);
        return myInput;
    }

    private JTextArea makeTextArea (int rows, int columns)
    {
        JTextArea textArea = new JTextArea(rows, columns);
        textArea.setEditable(false);
	return textArea;
    }


    private class BroadcastInput implements ActionListener
    {
	public void actionPerformed(ActionEvent e)
	{
            String message = myInput.getText();

            myInput.setText("");
            myClient.send(message);
            myInputHistory.append(message + "\n");
	}
    }


    public static void main (String[] args)
    {
        String hostname = "localhost";
        if (args.length > 0)
        {
            hostname = args[0];
        }

        new ClientGUI(hostname, Constants.PORT);
    }
}
