package rpicq;

/**
 * This class is a starting class for writing the RPIcq client.
 *
 * This class should do a few things:
 *	1. It should connect to your server in the constructor or in
 *		startClient().
 *	2. When the loginUser() method is called, it should tell the server
 *		that the user is available for chat.
 *	3. It should then keep a list of people on the system to talk to.
 *	4. If the user wants to talk to anyone, the startConversation()
 *		method is called.  At this point a new connection should
 *		be created.
 *	5. If someone wants to talk to the user, the you need to notice
 *	   	this request for a conversation, and create a new
 *		MessageSplitFrame, I have provided a method called
 *		conversationRequest() to help with this.
 *		This method takes the name of the user that wants to talk to
 *		you.  You may also want to pass in a parameter that represents
 *		the connection that you are going to use to send and recieve
 *		messages to that user on.  This may be of the class
 *		ConnectionSkeleton, but not necessarily.  It is up to you.
 *		It's important that there is something passed in that can be
 *		used to communicate to and from the other user.
 *	6. All connections should be claened up and closed when the
 *		sessionStop() method is called.
 *	7. The 4 methods: loginUser(), sessionStop(), startConversation(), and
 *		endConversation() are called from my GUI code.  You may want
 *		to call them yourself, but you probably don't want to.
 *
 * @author JJ Johns
 * @version 1
 **/

public class ClientSkeleton implements UIEventListener {

    private UserListFrame mMainFrame;

    public static void main(String argv[]) {
	try {
	    ClientSkeleton cs = new ClientSkeleton(argv[0], 47);
	    cs.startClient();
	}
	catch (Exception e) {e.printStackTrace(); }
    }

    public ClientSkeleton(String Name, int inPort ) {

	// necessary code
	mMainFrame = new UserListFrame("No one");
	mMainFrame.setUIEventListener( this );
	// end necessary code

	// you probably want to initialize the connection to the server
	// here
    }

    public void startClient() {

	// necessary code
	mMainFrame.setVisible(true);
	// end necessary code
	// note that the login user method will be called immediately after
	// this.
	
	// you might want to get the list of users from the server
    }

    /**
     * The UIListFrame calls this function to log in the user.
     * It should return true if the user enter's a good user name,
     * false otherwise.
     *
     * NOTE: The user name doesn't really matter.  You do not NEED
     * to really authenticate anyone if you don't want to.
     **/
    
    public boolean loginUser(String inUserName) {

	// you probably want to set the user name here.
	mMainFrame.setUserName( inUserName );
	return true;
    }


    /**
     * This is called when the user double clicks on a user's name to
     * start a conversation with them.
     **/
    public void startConversation(String thisUser, String remoteUser,
				  MessageSplitFrame frame) {
	// a new Connection should be started in here to handle messages to
	// and from this MessageSplitFrame


    }

    /**
     * This is called when a user closes the conversation window.
     **/
    public void endConversation(String thisUser, String remoteUser,
				MessageSplitFrame frame) {
	// any resouces created to handle this connection should
	// be recovered.

    }


    /**
     * In the event that someone wants to start a conversation with you,
     * your client needs to pop up a MessageSplitFrame for that conversation.
     *
     **/
    public void conversationRequest(String fromUser, ConnectionSkeleton con ) {

	// required code
	MessageSplitFrame msf = new MessageSplitFrame(mMainFrame.getUserName(),
						      fromUser, this);
	con.setMessageFrame(msf);
	msf.addMessageListener( con );
	// end required code
	

    }

    /**
     * This is called when the user closes the UserListFrame.
     * You should close network connections here, and do any other cleanup.
     **/
    public void sessionStop() {


	System.exit(0);
    }

}





