/*************************************************
 * 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 SketchPanel;
import ChatPanel;
import RTP_Thread;
import SenderMCSocket;
import ReceiverMCSocket;
import DataHandler;
import PacketHandler;
import RTCP_Sender_Thread;
import RTCP_Thread;
import StatisticsWindow;
import StateInfo;

/** The MenuWindow class provides the main layout for the User Interface. **/
/** Sender and receiver multicast sockets, sender and receiver threads, **/
/** Chat Tool, Whiteboard and User Panel instances are also created. */
/** @param parent pointer to the LoginWindow class that called this main  **/
/** frame. **/
/** @param win_title string title for the main frame. **/
class MenuWindow extends Frame {
	RTCP_Thread rtcp_thread;
	RTCP_Sender_Thread rtcp_sender_thread;
	RTP_Thread rtp_thread;
	SenderMCSocket senderSock, rtcp_senderSock;
	ReceiverMCSocket rtp_recvrSock, rtcp_recvrSock;
	PacketHandler pkt_handler;
	DataHandler dataHandler;
	String mc_group = null;
	int mc_port = 0;

	/*String mc_group = "224.2.0.22";
	int mc_port = 1234;
	*/

	byte buf[] = new byte[1024];

	MenuBar mb;
	Menu m_file, m_edit, m_option, m_font, m_help;
	MenuItem file_open, file_exit;
	MenuItem edit_copy, edit_cut, edit_paste, edit_clear;
	MenuItem font_wb, font_chat;
	MenuItem option_uid;
	MenuItem Statistics_MenuItem;
	MenuItem Sup_MenuItem;
	MenuItem help_about;
	LoginWindow parent;
	StateInfo state_info;
	StatisticsWindow statWin;
	SuperSettingWindow supWin;
	OpenURLDialog openURLDlg;

	SketchPanel sketch;
	ChatPanel chat;
	UserPanel user_panel;

	public MenuWindow(LoginWindow parent, String win_title) {
		super();
		this.parent = parent;
		mc_group = parent.user_info.mc_addr;
		mc_port = parent.user_info.mc_port;
		state_info = new StateInfo();
		resize(700, 900);
		setBackground(Color.white);

		//instantiate and set menubar on frame
		mb = new MenuBar();
		setMenuBar(mb);

		//set title
		setTitle(win_title);

		//instatnitate menu
		m_file = new Menu("File", false);
		//m_edit = new Menu("Edit", false);
		m_font = new Menu("Font", false);
		m_option = new Menu("Option", false);
		m_help = new Menu("Help", false);

		//add to menu bar
		mb.add(m_file);
		//mb.add(m_edit);
		mb.add(m_font);
		mb.add(m_option);
		mb.add(m_help);

		//file menu creation
		file_open = new MenuItem("Open");
		file_exit = new MenuItem("Exit");
		m_file.add(file_open);
		m_file.add(file_exit);

		//edit menu creation
		edit_copy = new MenuItem("Copy");
		edit_cut = new MenuItem("Cut");
		edit_paste = new MenuItem("Paste");
		edit_clear = new MenuItem("Clear");
		/*m_edit.add(edit_copy);
		m_edit.add(edit_cut);
		m_edit.add(edit_paste);
		m_edit.add(edit_clear);
		*/

		//font menu creation
		font_wb = new MenuItem("Whiteboard");
		font_chat = new MenuItem("Chat");
		m_font.add(font_wb);
		m_font.add(font_chat);

		//option menu creation
		option_uid = new MenuItem("Change user Info");
		Statistics_MenuItem = new MenuItem("View Statistics");
		Sup_MenuItem = new MenuItem("Supervisor Options");
		m_option.add(Sup_MenuItem);
		m_option.add(Statistics_MenuItem);
		m_option.add(option_uid);
		mb.setHelpMenu(m_help);

		//help menu creation
		help_about = new MenuItem("About");
		m_help.add(help_about);

		/* brings up statistics window */
		statWin = new StatisticsWindow("View Statistics", this);
		statWin.show();

		/* creates RTP and RTCP sender and receiver multicast
		 * sockets. */
		senderSock = new SenderMCSocket(mc_group, mc_port, (byte) 1);
		rtp_recvrSock = new ReceiverMCSocket(mc_group, mc_port);
		rtcp_senderSock = new SenderMCSocket(mc_group, mc_port + 1, (byte) 1);
		rtcp_recvrSock = new ReceiverMCSocket(mc_group, mc_port + 1);

		/* instantiates RTP Packet Handler for assembling and segmenting
		 * RTP packets. Also instantiates Data Handler of which the Data
		 * Dispatcher dispatches received data to the appropriate modules:
		 * either Whiteboard or ChatTool module, based on a flag in the 
		 * received packets. */
		pkt_handler = new PacketHandler(this);
		dataHandler = new DataHandler(this);

		/* clears before starting sender and receiver threads. 
		 * Sender thread must be started before the receiver thread,
		 * otherwise, no RTCP packets to read. */
		stop_receiver_threads();
		stop_sender_threads();
		start_sender_thread();
		start_receiver_threads();

		sketch = new SketchPanel(this);
		chat = new ChatPanel(this);
		chat.msgTextBox.appendText("You are logged onto " + 
						parent.user_info.hostName + " as " + 
						parent.user_info.name + "\n");
		chat.msgTextBox.appendText("Your IP Address is  " + 
						parent.user_info.ipAddr + "\n");
		user_panel = new UserPanel(this);

		setLayout(new BorderLayout());
		add("Center", chat);
		add("South", sketch);
		add("East", user_panel);
		this.show();
	}

	/** Event handler to trap events of clicking on the menu items **/
	public boolean handleEvent(Event e) {
		if (e.target instanceof MenuItem) {
			if ((e.arg).equals("Change user Info")) {
				LoginWindow changeUserInfoDlg =
				new LoginWindow(parent.user_info);
				 changeUserInfoDlg.show();
			} else if ((e.arg).equals("Supervisor Options")) {
				supWin = new SuperSettingWindow("Set Supervisor Options", this);
				supWin.show();
			} else if ((e.arg).equals("Exit")) {
				System.out.println("EEEXXXIIITTT menu pressed ");
				state_info.exit = true;
			} else if ((e.arg).equals("Open")) {
				openURLDlg = new OpenURLDialog("Shared Web Page");
				openURLDlg.show();
			}
			 return true;
		}
		 return false;
	}

	public void openURL() {
		Runtime myRuntime = Runtime.getRuntime();

		String cmdLine[] = new String[10];
		for(int i=0; i<10; i++)
			cmdLine[i] = null;

		cmdLine[0] = "Netscape";
		cmdLine[1] = "-remote";
		cmdLine[2] = "'openURL(http://comet.ctr.columbia.edu/~laitee)'";

		String cmd = "netscape -remote 'openURL(http://comet/~laitee)'";
		System.out.println("$$ In openURL");
		String cmd2 = "Netscape -remote 'openURL(http://comet.ctr.columbia.edu/~laitee)'";
		String cmd3 = "netscape &";

		try {	
			//myRuntime.exec(cmd2, null);
			myRuntime.exec(cmd3);
		} catch (java.io.IOException e) {
			System.out.println("IOException : "+e);
		}

		System.out.println("$$ END of openURL");
	}

	/** Exits from the entire application. Called after a BYE packet **/
	/** has been sent by user. **/
	public void exit() {
		this.hide();
		System.exit(0);
	}

	/** Function to start RTCP sender thread **/
	public void start_sender_thread() {
		if (rtcp_sender_thread == null) {
			rtcp_sender_thread = new RTCP_Sender_Thread(this);
			rtcp_sender_thread.start();
		}
	}

	/** Function to start RTP and RTCP receiver threads **/
	public void start_receiver_threads() {
		if (rtp_thread == null) {
			rtp_thread = new RTP_Thread(this);
			rtp_thread.start();
		}
		if (rtcp_thread == null) {
			rtcp_thread = new RTCP_Thread(this);
			rtcp_thread.start();
		}
	}

	/** Function to stop RTP and RTCP receiver threads **/
	public void stop_receiver_threads() {
		if (rtp_thread != null) {
			rtp_thread.stopthread();
			rtp_thread = null;
		}
		if (rtcp_thread != null) {
			rtcp_thread.stopthread();
			rtcp_thread = null;
		}
	}

	/** Function to stop RTCP sender thread **/
	public void stop_sender_threads() {
		if (rtcp_sender_thread != null) {
			rtcp_sender_thread.stopthread();
			rtcp_sender_thread = null;
		}
	}

	/** Sets the preferred size the whole application **/ 
	/*public Dimension PreferredSize() {
		return new Dimension(500, 700);
	}
	*/

	/** Sets the minimum size of the whole application to the preferred **/
	/** size **/
	/*public Dimension MinimumSize() {
		return preferredSize();
	}
	*/

}

