/*************************************************
 * 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.Enumeration;
import java.util.Hashtable;
import MenuWindow;
import MyList;
import MyButton;
import UserInfo;
import UserStatisticsDlg;

/** User Panel class implements the user list on the interface. It **/
/** consists of functions for manipulating a hashtable that contains the **/
/** canonical names of all users participating in the current session, and **/
/** updates the user list accordingly. User can view information of other **/
/** on the list by selecting that user's canonical name and clicking on the**/
/** OK button. **/

public class UserPanel extends Panel {
	MenuWindow main_parent;
	MyList userPanelList;
	MyButton OKBtn;
	Hashtable user_list = new Hashtable();
	UserInfo selectedUserInfo = null;
	UserStatisticsDlg userStatDlg;

	public UserPanel(MenuWindow parent) {
		super();
		this.main_parent = parent;
		GridBagLayout gridbag = new GridBagLayout();
		setLayout(gridbag);
		setBackground(Color.lightGray);

		userPanelList = new MyList(gridbag, 10, false,
					     	"REMAINDER", 0, 0, 0, 0);
		OKBtn = new MyButton(gridbag, "OK", "REMAINDER",
				       					40, 20, 10, 20);
		add(userPanelList);
		add(OKBtn);
		show();
	}


	/** Function to update the hashtable once an RTCP packet is received. **/
	/** Problem of the hashtable storing multiple copies of information of **/
	/** the same user is eliminated by declaring the user's canonical name **/
	/** as the unique key for the hashtable. **/

	public void updateUserList(String cname, UserInfo other_users_info) {
		String key = cname;
		user_list.put(key, other_users_info);
		updateUserPanel();
	}


	/** Function to remove canonical name from the hashtable when a BYE **/
	/** packet has been received, indicating that the user leaving the  **/
	/** current multicast session. **/
	/** @SSRC	the SSRC of the user leaving the group **/

	public void removeFmList(int SSRC) {
		String user_cname = null;
		for (Enumeration obj = user_list.elements(); obj.hasMoreElements();) {
			UserInfo info = (UserInfo) obj.nextElement();
			if (info.SSRC == SSRC) {
				user_cname = info.cname;
				break;
			}
		}
		if ( user_list.containsKey(user_cname) == true)
			user_list.remove(user_cname);
		updateUserPanel();
	}


	/** Function to update the user list appearing on the user interface **/
	/** every time the hashtable changes, so as to update user of the **/
	/** latest list of particpating users. **/ 

	public void updateUserPanel() {
		userPanelList.clear();
		for (Enumeration obj = user_list.elements(); obj.hasMoreElements();) {
			UserInfo user_info = (UserInfo) (obj.nextElement());
			userPanelList.addItem(user_info.cname);
		}
	}


	/** Provides offset of the user panel from the boundary **/	

	public Insets insets() { 
		return (new Insets(0, 0, 0, 30));
	}


	/** Event handler to trap the ACTION_EVENT on the OK button of the **/
	/** User Panel so as to bring out a Statistics window to display user **/
	/** information received. **/

	public boolean handleEvent(Event e) {
		switch (e.id) {
			case Event.LIST_SELECT:
				selectedUserInfo = (UserInfo)user_list.get(userPanelList.getSelectedItem());
			break;
			case Event.LIST_DESELECT:
			break;
			case Event.ACTION_EVENT:
			if (e.target instanceof Button) {
				String label = ((Button) e.target).getLabel();
				if (label.equals("OK")) {
					if (selectedUserInfo != null) {
						userStatDlg = new UserStatisticsDlg("User Information",
												main_parent, selectedUserInfo);
						userStatDlg.pack();
						userStatDlg.show();
						selectedUserInfo = null;
					} else
						 System.out.println("Please select user to view statistics");
				}
			}
			break;
		}
		return true;
	}
}


/** UserStatisticsDlg class simply implements the Statistics window for **/
/** displaying information of other users received via the receiver **/
/** multicast socket. **/

/*class UserStatisticsDlg extends Frame {
	public UserStatisticsDlg(String title, MenuWindow main_parent,
				  						UserInfo selectedUserInfo) {
		super(title);
		GridBagLayout gridbag = new GridBagLayout();
		setLayout(gridbag);
		add(new MyLabel(gridbag, "Name",
				  Label.LEFT, "RELATIVE", 10, 10, 5, 5));
		add(new MyLabel(gridbag, selectedUserInfo.name,
				  Label.LEFT, "REMAINDER", 10, 5, 5, 10));
		add(new MyLabel(gridbag, "Email Address",
				  Label.LEFT, "1", 5, 10, 10, 5));
		add(new MyLabel(gridbag, selectedUserInfo.email,
				  Label.LEFT, "REMAINDER", 0, 0, 0, 0));
		add(new MyButton(gridbag, "OK", "2", 10, 20, 10, 20));
		resize(800, 400);
	}
	*/

	/** Action to handle the ACTION EVENT on the OK button to simply **/
	/** close the Statistics window **/

	/*public boolean action(Event e, Object what) {
		if (e.target instanceof MyButton) {
			String label = ((MyButton) e.target).getLabel();
			if (label.equals("OK")) {
				System.out.println("OK Btn pressed@@@@");
				this.hide();
				removeAll();
			}
			return true;
		}
		 return false;
	}
}
	*/
