/* Author: Jeff Dalton <J.Dalton@ed.ac.uk>
 * Updated: Wed Nov 28 05:12:08 2001 by Jeff Dalton
 * Copyright: (c) 2001, AIAI, University of Edinburgh
 */

package ix.ichat;

import ix.icore.*;

import ix.ichat.ChatMessage;

import ix.util.*;


/** 
 * A class for I-Chat as a standalone application main program 
 */

public class IChat extends IXAgent {

    ChatFrame frame;

    // Some customizable fields
    protected String displayName = "I-Chat";
    protected String symbolName = "I-Chat";
    protected String ipcName = "ICHAT";

    public Object getAgentIPCName() { return ipcName; }

    public IChat() {
    }

    /**
     * Main program.
     */
    public static void main(String[] argv) {

	Util.printGreeting("I-Chat");

	new IChat().mainStartup(argv);

    }

    /**
     * Command-line argument processing for arguments used by all
     * versions of I-Chat.
     */
    protected void processCommandLineArguments() {
	if (Parameters.haveParameter("ipc-name")) {
	    ipcName = Parameters.getParameter("ipc-name");
	    Debug.noteln("Using IPC name", ipcName);
	}

	// N.B. need to do this after setting ipcName, because
	// super method sets up IPC.
	super.processCommandLineArguments();

	displayName = Parameters.getParameter("display-name", displayName);
	symbolName  = Parameters.getParameter("symbol-name", symbolName);

    }

    /**
     * Completes I-Chat setup and initialization.
     */
    public void startup() {
	frame = new ChatFrame(this, displayName);
	frame.setVisible(true);
    }

    /**
     * Handles chat messages in addition to the types of input handled
     * by the super method.
     */
    public void handleInput(IPC.InputMessage message) {
	Object contents = message.getContents();
        if (contents instanceof ChatMessage) {
	    handleNewChatMessage((ChatMessage)contents);
        }
	else {
	    super.handleInput(message);
        }
    }

    /**
     * Handles new chat messages.
     */
    public void handleNewChatMessage(ChatMessage message) {
	frame.newMessage(message);
    }


}

