package test;

import com.wilko.jaim.*;


/**
 * Example test code for Jaimlib. Login to AIM, set up a chat room,
 * and invite a buddy.
 *
 * Run this program with:
 *   java test.JaimChatTest yourname yourpass chatroomname buddyname
 */

public class JaimChatTest implements JaimEventListener {

  // Hostname and port number to connect to.
  //
  private static String HOST = "toc.oscar.aol.com";
  private static int PORT = 9898;

  private String chatRoomName = null;
  private String chatRoomID = null;

  // Our connection to the AIM server.
  //
  private JaimConnection connection;


  public JaimChatTest(String name, String pass) {
    try {
      connection = new JaimConnection(HOST, PORT);
      connection.connect();
      connection.watchBuddy(name);
      connection.logIn(name, pass, 15000);
      connection.addEventListener(this);
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(1);
    }
  }

  public void setupChatRoom(String name) {
    chatRoomName = name;
    try {
      connection.joinChat(name);
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(1);
    }
  }

  public void inviteBuddy(String buddy, String message) {
    try {
      connection.inviteToChat(chatRoomID, buddy, message);
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(1);
    }
  }

  public void sendMessage(String msg) {
    try {
      connection.chatSend(chatRoomID, msg);
      System.out.println("Message successfully sent.");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void close() {
    connection.logOut();
  }


  // This method is requied by the JaimEventListener interface. It
  // processes notification events from the JaimConnection object.
  // We don't do much here except note that they arrived. (The useful
  // information, though, is in the TocResponse.)
  // 
  public void receiveEvent(JaimEvent ev) {
    System.out.println("Received " + ev.getTocResponse().getClass());

    if (ev.getTocResponse().getClass() == ChatJoinTocResponse.class) {
      ChatJoinTocResponse cjtr = (ChatJoinTocResponse) ev.getTocResponse();
      System.out.println("join chat " + cjtr.getChatRoomID());
      chatRoomID = cjtr.getChatRoomID();
    }
  }


  public static void main(String args[]) {
    if (args.length != 4) {
      System.err.println("Usage: java test.JaimChatTest yourname yourpassword chatroomname buddyname");
      System.exit(1);
    }

    // log in
    //
    System.out.println("setting up...");
    JaimChatTest test = new JaimChatTest(args[0], args[1]);
    try {
      Thread.sleep(5000);
    } catch (Exception ex) {
    }

    // set up the chat room
    //
    System.out.println("setting up chat room...");
    test.setupChatRoom(args[2]);
    try {
      Thread.sleep(5000);
    } catch (Exception ex) {
    }

    // issue the invitation
    System.out.println("inviting buddy...");
    test.inviteBuddy(args[3], "Come join this chat");
    try {
      Thread.sleep(20000);
    } catch (Exception ex) {
    }

    // send some messages to the chat room, just for debugging purposes.
    // issue ten messages, five seconds apart
    //
    for (int i = 0; i < 10; i++) {
      System.out.println("sending... (" + i + ")");
      test.sendMessage("hello... " + i);
      try { Thread.sleep(5000); } catch (Exception ex) {}
    }
    test.close();
  }

}
