/*************************************************
 * Author: 	Evelyn Lai-Tee Cheok
 *			Department of Electrical Engineering
 *			Center for Telecommunications Research
 *			Schapiro Research Building
 *			Columbia University
 *
 * Email: 	laitee@ctr.columbia.edu
 *
 **************************************************/
import java.lang.*;
import java.awt.*;
import java.util.*;
import java.net.InetAddress;
import MenuWindow;
import UserInfo;

/** This is the main class of the whiteboard application.  **/
public class wb {
	public static void main(String[] args) {
		LoginWindow logwin = new LoginWindow(null);
		 logwin.show();
	}
}

/** This class implements the Login Session Dialog. **/
/** It prompts user for several entries before storing them into **/
/** the UserInfo class. **/
class LoginWindow extends Frame {
	UserInfo user_info = new UserInfo();
	boolean change_user_info = false;
	boolean stop = false;
	MyTextBox name;
	MyTextBox phone;
	MyTextBox note;
	MyTextBox loc;
	MyTextBox email;
	MyTextBox mc_addr;
	MyTextBox mc_port;

	public LoginWindow(UserInfo origin_user_info) {
		super("Login Session Dialog");
		String beginText = "Please enter the following information :";

		if (origin_user_info != null) {
			change_user_info = true;
			this.user_info = origin_user_info;
		} else
			 storeFixedUserInfo();

		GridBagLayout gridbag = new GridBagLayout();
		setLayout(gridbag);

		mc_addr = new MyTextBox(gridbag, 10, "REMAINDER",
				       10, 10, 5, 5);

		mc_port = new MyTextBox(gridbag, 10, "REMAINDER",
				        10, 10, 5, 5);

		name = new MyTextBox(gridbag, 10, "REMAINDER",
				       10, 10, 5, 5);

		email = new MyTextBox(gridbag, 10, "REMAINDER",
				        10, 10, 5, 5);

		if (change_user_info == true) {
			add(new MyLabel(gridbag, "Change particulars:",
				      Label.LEFT, "REMAINDER", 0, 0, 0, 0));
		} else
			 add(new MyLabel(gridbag, beginText, Label.LEFT,
					  "REMAINDER", 10, 10, 10, 10));

		add(new MyLabel(gridbag, "Multicast Address",
				Label.LEFT, "RELATIVE", 10, 5, 10, 10));
		add(mc_addr);

		add(new MyLabel(gridbag, "Multicast Port",
				Label.LEFT, "1", 10, 5, 10, 10));
		add(mc_port);

		add(new MyLabel(gridbag, "Name",
				Label.LEFT, "RELATIVE", 10, 5, 10, 10));
		add(name);

		add(new MyLabel(gridbag, "Email Address",
				Label.LEFT, "1", 10, 5, 10, 10));
		add(email);

		add(new MyButton(gridbag, "OK", "REMAINDER", 30, 60, 0, 60));
		if (change_user_info == true)
			updateUserInfo(origin_user_info);

		resize(350, 400);
	}

	/** function parses up to 5 tokens with delimiters as "/" and store **/
	/** in localHostInfo[]. **/
	/** Returns cname. **/

	public String getCName(String userAcctName, String localHostName) {
		String localHostInfo[] = new String[5];
		int i = 0;
		StringTokenizer st = new StringTokenizer(localHostName, "/", false);
		while (st.hasMoreTokens()) {
			localHostInfo[i] = st.nextToken();
			i++;
		}
		 return (userAcctName + localHostInfo[0]);
	}

	/** This function parses the hostname and IP address before storing **/
	/** them, together with the Canonical Name into the UserInfo class. **/

	public void storeFixedUserInfo() {
		String localHostInfo[] = new String[5];
		String localHostName = null;
		int i = 0;

		 try {
			localHostName = (InetAddress.getLocalHost()).toString();
		} catch(java.net.UnknownHostException e) {
			System.out.println("UnknownHostException in getting local host: " + e);
		}

		StringTokenizer st = new StringTokenizer(localHostName, "/", false);

		while (st.hasMoreTokens()) {
			localHostInfo[i] = st.nextToken();
			i++;
		}

		user_info.userAcctName = System.getProperty("user.name");

		if ((user_info.hostName = localHostInfo[0]) == null)
			System.out.println("no hostName parsed!");

		if ((user_info.ipAddr = localHostInfo[1]) == null)
			System.out.println("no IP Address parsed!");

		user_info.cname = user_info.userAcctName + "@" + user_info.hostName;
	}

	/** Function to update the changed name and email items **/
	public void updateUserInfo(UserInfo origin_user_info) {
		name.setText(origin_user_info.name);
		email.setText(origin_user_info.email);
	}

	/** Function to store user information provided at the Login  **/
	/** Dialog Session **/
	public void storeUserInfo() {
		String address = mc_addr.getText();
		String port = mc_port.getText();

		if ( address.length() == 0 || port.length() == 0 )  {
			InvalidPopupDlg popup = new InvalidPopupDlg(this);
			popup.show();
			stop = true;
			//System.out.println("Please enter valid values for mc addr and port");
		}
		else {
			user_info.mc_addr = address;
			user_info.mc_port = (new Integer(port)).intValue();
			user_info.name = name.getText();
			user_info.email = email.getText();
			stop = false;
		}
	}

	/** Event handler to trap the ACTION event on the Login Session Dialog **/
	/** and invokes the main user interface of this application **/

	public boolean action(Event e, Object what) {
		if (e.target instanceof Button) {
			if (e.arg == "OK") {
				storeUserInfo();
				if (this.isShowing())
					this.hide();
				if (change_user_info == true)
					change_user_info = false;
				else {
					if ( !stop ) {
						MenuWindow gui = new MenuWindow(this, 
							"Shared Whiteboard using Java Multicast and RTP");
					 gui.show();
					 }
				}
			}
			return true;
		}
		 return false;
	}
}

class InvalidPopupDlg extends Dialog {
	public InvalidPopupDlg(LoginWindow parent) {
		super(parent, "Invalid Arguments", false);
		GridBagLayout gridbag = new GridBagLayout();
		setLayout(gridbag);
		add( new MyLabel(gridbag, 
					"Please specify valid multicast address and port number",
					Label.CENTER, "REMAINDER", 10, 10, 10, 10) );
		add( new MyButton(gridbag, "OK", "REMAINDER", 20, 120, 10, 120) );
		resize(400, 200);
	}

	public boolean action(Event e, Object what) {
		if (e.arg.equals("OK")) {
			this.removeAll();
			this.hide();
			System.exit(0);
		}
		return true;
	}
}
