package jsbook.chapter10.collab;

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;

public class MessageFrame extends Frame 
        implements ActionListener {
    private Label topText;
    private TextArea displayArea;
    private Label messageLabel;
    private TextField messageTextField;
    private Button sendButton;
    private Label statusLabel;
    private String username;
    private String friend;
    private Messenger messenger;
        
    public MessageFrame(Messenger messenger, String username, 
            String friend) { 
        this.messenger = messenger;
        this.username = username;
        this.friend = friend;
        
        setLayout(null);
        setSize(350,300);
        
        // add label for chat info:
        topText = new Label("",Label.LEFT);
        topText.setBounds(12,30,150,24);
        add(topText);

        // add text area for the messages:
        displayArea = new TextArea();
        displayArea.setBounds(24,60,300,180);
        displayArea.setBackground(Color.white);
        displayArea.setEditable(false);
        displayArea.setText("");
        add(displayArea);
        
        // add label and text entry field for the message:
        messageLabel = new Label("Message:",Label.LEFT);
        messageLabel.setBounds(24,252,60,24);
        add(messageLabel);

        messageTextField = new TextField();
        messageTextField.setBounds(84,252,180,24);
        messageTextField.setText("");
        add(messageTextField);

        // add a "Send Message" button:
        sendButton = new Button();
        sendButton.setLabel("Send");
        sendButton.setBounds(264,252,60,24);
        add(sendButton); 
        
        // add label for the status:
        statusLabel = new Label("",Label.LEFT);
        statusLabel.setBounds(12,255,250,24);
        add(statusLabel);
                
        setBackground(java.awt.Color.lightGray);
        
        sendButton.addActionListener(this);
        messageTextField.addActionListener(this);
        
        // allows the window to be closed:
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });  
        
        setTopText("Chat with " + friend);
        setTitle(username);
        pack();
        setVisible(true);
        messageTextField.requestFocus();
    }
    
    public void addMessage(String name, Date time, String content) {
        if ("".equals(content)) {
            messageTextField.requestFocus();
            return;
        }
        displayArea.append(name + " (" +  
                DateFormat.getTimeInstance(DateFormat.SHORT).format(time) +
                "): " + content + "\n");
    }
        
    public void actionPerformed(ActionEvent event) {
        String content = messageTextField.getText();
        
        if (content.equals("")) {
            setStatus("Enter a message.");
            return;
        } 
        
        messageTextField.setText("");
        
        // add the message here
        addMessage(username, new Date(), content);
        ChannelMessageEntry message = 
            new ChannelMessageEntry(friend, username, content);
        Channel channel = new Channel(messenger, friend);
        channel.append(message);
    }
    
    public void setStatus(String msg) {
        statusLabel.setText(msg);
    }
    
    public void setTopText(String msg) {
        topText.setText(msg);
    }
}
