/*************************************************
 * 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.awt.*;
import java.lang.*;
import java.io.*;
import MyLabel;
import MyTextBox;
import DataType;
import SenderMCSocket;
import PacketHandler;

/** Besides implementing the user interface, Chat Panel class contains **/
/** components for sending the user-entered text string over the multicast **/
/** network. It also provides function to receive chat string from other **/
/** users over the multicast socket and display them on the text display**/
/** area.**/
public class ChatPanel extends Panel {
	MenuWindow main_parent;
	MyTextBox textbox;
	MyTextArea textarea;
	MyTextArea msgTextBox;
	SenderMCSocket senderSock;
	PacketHandler pkt_handler;
	String userName;
	StateInfo state_info;
	MyButton HiBtn;
	MyButton HelloBtn;
	MyButton HowAreYouBtn;

	public ChatPanel(MenuWindow parent) {
		super();
		this.main_parent = parent;
		this.userName = parent.parent.user_info.name;
		this.senderSock = parent.senderSock;
		this.pkt_handler = parent.pkt_handler;
		this.state_info = parent.state_info;
		GridBagLayout gridbag = new GridBagLayout();
		setLayout(gridbag);
		setBackground(Color.lightGray);

		MyLabel label = new MyLabel(gridbag, "Enter text here: ",
					    Label.LEFT, "1", 10, 10, 10, 5);
		textbox = new MyTextBox(gridbag, 30, "REMAINDER", 10, 5, 10, 10);
		textarea = new MyTextArea(gridbag, 8, 1, "REMAINDER",
					    10, 10, 10, 10);
		textarea.setEditable(false);
		msgTextBox = new MyTextArea(gridbag, 5, 1, "REMAINDER",
					      10, 10, 10, 10);
		msgTextBox.setEditable(false);
		HiBtn = new MyButton(gridbag, "Hi !", "1", 0, 20, 0, 20);
		HelloBtn = new MyButton(gridbag, "Hello !", "1", 0, 0, 0, 0);
		HowAreYouBtn = new MyButton(gridbag, "How are you?",
					      "REMAINDER", 0, 30, 0, 30);

		add(label);
		add(textbox);
		add(textarea);
		add(msgTextBox);
		add(HiBtn);
		add(HelloBtn);
		add(HowAreYouBtn);
	}

	/** updates the RTP packet size received **/
	public void updateRTPSize() {
		msgTextBox.appendText("RTP packet size sent : "
				  + main_parent.state_info.RTP_packet_size);
	}

	/** processes incoming chat string from other multicast users **/
	/** by displaying string onto the text display area. The text string **/
	/** already contains return character such that we need not explicitly **/
	/** append one to the end of the received string. **/
	public void processInData(String chatStr) {
		textarea.appendText(chatStr);
	}

	/** provides an offset placement from the original container **/
	public Insets insets() {
		return (new Insets(0, 0, 0, 0));
	}

	/** Event handler to handle the WINDOW_DESTROY event and the event **/
	/** of user pressing the <RETURN> key after entering text string. Such **/
	/** ACTION_EVENT will invoke function SendChatStr which in turn calls **/
	/** on the assembler function of the RTP Packet Handler to assemble **/
	/** the string into an RTP packet before sending them over the sender **/
	/** multicast socket. **/
	public boolean handleEvent(Event e) {
		switch (e.id) {
			case Event.ACTION_EVENT:
			if (e.target instanceof MyTextBox) {
				String text = ((MyTextBox) e.target).getText();
				if (text != null) {
					String displayText = userName + " : " + text + "\n";
					textarea.appendText(displayText);
					sendChatStr(displayText);
				} else
					System.out.println("Text is NULL!!\n");
			} else if (e.target instanceof MyButton) {
				String text = ((MyButton) e.target).getLabel();
				String displayText = userName + " : " + text + "\n";
				textarea.appendText(displayText);
				sendChatStr(displayText);
			}
			 return true;
			case Event.WINDOW_DESTROY:
			 return true;
			default:
		}
		 return false;
	}

	/** Calls assembler of RTP Packet Handler for assembling the **/
	/** user-entered chat string before sending to the network **/
	public void sendChatStr(String str) {
		int bufsize = str.length() * 2 + 1;
		byte data_buf[] = new byte[bufsize];
		byte pkt_buf[] = new byte[1024];
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		DataOutputStream dataStream = new DataOutputStream(byteStream);

		 try {
			dataStream.writeByte(DataType.ChatToolDataType);
			dataStream.writeUTF(str);
		} catch(java.io.IOException e) {
			System.out.println("IOException in sendChatStr: " + e);
		}
		data_buf = byteStream.toByteArray();
		System.out.println("=========data_buf.length =" + data_buf.length);

		if ((pkt_buf = pkt_handler.assemble(data_buf)) != null) {
			System.out.println("In sendchatStr: pkt_buf.length = "
					   + pkt_buf.length);
			senderSock.sendByteData(pkt_buf);
		} else
			System.out.println("Error in assemble, buf returned is NULL !!!\n");
	}
}
