/*************************************************
 * Author: 	Chi-Hao Li
 *			Department of Computer Science
 *			Columbia University
 *
 * 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.io.*;
import java.util.*;
import SketchArea;
import ObjType;
import SenderMCSocket;
import PacketHandler;


public class Line extends ObjBaseClass implements DrawObject, Cloneable {

	SketchArea parent;
	public int x1, y1, old_x, old_y;
	public int x2, y2;
	public int type;
	public int color = 0;
	public Color[] baseColors = new Color[20];
	public int obj_id = 0;
	Random rand_gen = new Random(System.currentTimeMillis());

	/**
	 * constructor
	 *
	 *@param parent SketchArea object reference
	 *@param c color type
	 *@return none
	 *@exception none
	 */

	public Line(SketchArea parent, int c) {

		super(parent.parent.senderSock, parent.parent.pkt_handler);
		this.parent = parent;
		type = ObjType.LINE;
		baseColors = parent.colorpanel.getBaseColors();
		color = c;
		obj_id = (int) (rand_gen.nextFloat() * 2000);
	}

	/**
	 * constructor
	 *
	 *@param parent SketchArea reference
	 *@return none
	 *@exception none
	 */

	public Line(SketchArea parent) {
		super(parent.parent.senderSock, parent.parent.pkt_handler);
		this.parent = parent;
		type = ObjType.LINE;
		baseColors = parent.colorpanel.getBaseColors();
	}

	/** clones a line object by making an exact copy of this line object
	  * with values equal to the original values, except that the starting
	  * and ending points of the newly cloned line are translated by a value of
	  * TRANSLATE_X and TRANSLATE_Y defined in the ObjBaseClass.java.
	  * @return a cloned object.
	  **/
	public Object clone() {
		try {
			Line cloneline = (Line)super.clone();
			cloneline.x1 += TRANSLATE_X;
			cloneline.y1 += TRANSLATE_Y;
			cloneline.x2 += TRANSLATE_X;
			cloneline.y2 += TRANSLATE_Y;
			cloneline.obj_id = (int)(rand_gen.nextFloat() * 2000);
			return cloneline;
		} catch (CloneNotSupportedException e) {
			System.out.println("CLoneNotSupportedException : "+e);
			throw new InternalError(e.toString());
		}
	}

	/**
	 * event handler for mouse down while drawing
	 *
	 *@param x x value
	 *@param y y value
	 *@return true
	 *@exception none
	 */

	public boolean mouseDown(int x, int y) {

		x1 = x;
		y1 = y;

		x2 = x1;
		y2 = y1;

		return true;
	}

	/**
	 * event handler for mouse up when drawing objects
	 *
	 *@param x x value
	 *@param y y value
	 *@return true
	 *@exception none
	 */

	public boolean mouseUp(int x, int y) {

		x2 = x;
		y2 = y;
		//assemble(ObjType.LINE);
		send();
		return true;
	}

	public void send() {
		assemble(ObjType.LINE);
	}

	/**
	 * event handler for mouse drag when drawing the objects
	 *
	 *@param x x value
	 *@param y y value
	 *@return true
	 *@exception none
	 */

	public boolean mouseDrag(int x, int y) {
		old_x = x2;
		old_y = y2;

		x2 = x;
		y2 = y;

		return true;
	}

	/**
	 * function called when moving the object
	 *
	 *@param x x value
	 *@param y y value
	 *@return void
	 *@exception none
	 */


	public void moving(Graphics g, int x, int y) {
		old_x = x2;
		old_y = y2;
		clean(g);
		x2 += x - x1;
		y2 += y - y1;
		x1 = x;
		y1 = y;
		paint(g);
		assemble(ObjType.MOVE);
	}

	public void recvd_moved_obj(Graphics g, int recvd_x1, int recvd_y1,
				     int recvd_x2, int recvd_y2) {
		old_x = x2;
		old_y = y2;
		clean(g);
		x1 = recvd_x1;
		y1 = recvd_y1;
		x2 = recvd_x2;
		y2 = recvd_y2;
		paint(g);
	}

	/**
	 * set parameter after locating the last point
	 *
	 *@param g graphics context
	 *@param x x value
	 *@return y y value
	 *@exception none
	 */


	public void moveUP(Graphics g, int x, int y) {
		x2 += x - x1;
		y2 += y - y1;
		x1 = x;
		y1 = y;
		paint(g);
	}

	/**
	 * set drawing color
	 *
	 *@param g graphics context
	 *@param c color
	 *@return void
	 *@exception none
	 */

	public void setPenColor(Graphics g, int c) {
		g.setColor(baseColors[c]);
		//g.setXORMode(Color.white);
	}

	/**
	 * draw object which is passed from network
	 *
	 *@param xx1 start point
	 *@param yy1 start point
	 *@param xx2 end point
	 *@param yy2 end point
	 *@return void
	 *@exception none
	 */

	public void draw(int xx1, int yy1, int xx2, int yy2, int col, int obj_id_recvd) {
		int oldColor;
		 x1 = xx1;
		 y1 = yy1;
		 x2 = xx2;
		 y2 = yy2;

		 this.color = col;
		 paint(parent.offscreenG);
		 parent.repaint();
		 obj_id = obj_id_recvd;
		 parent.addStack((Object) this);
	}

	/**
	 * draw the object by pen color
	 *
	 *@param g graphics context
	 *@return void
	 *@exception none
	 */

	public void paint(Graphics g) {
		g.setColor(baseColors[color]);
		System.out.println("*************** Color to set pen color =" + color);
		g.drawLine(x1, y1, x2, y2);
	}

	/**
	 * clear up object on the whiteboard
	 *
	 *@param g Graphics context
	 *@return void
	 *@exception none
	 */

	public void clear(Graphics g) {
		clean(g);
		paint(g);
	}

	public void clean(Graphics g) {
		g.setColor(parent.getBackground());
		g.drawLine(x1, y1, old_x, old_y);
	}

	public void erase(Graphics g, boolean send_del_req) {
		g.setColor(parent.getBackground());
		g.drawLine(x1, y1, x2, y2);
		if (send_del_req == true)
			assemble(ObjType.DELETE);
	}


	/**
	 * assembles the Line object's parameters based on application-specific
	 * format before calling superclass's BaseClassAssemble for assembling
	 * this resulting packet with the RTP header. This function is called
	 * by mouseUp Event.
	 *
	 *@author Lai-Tee Cheok
	 *
	 *@return void
	 *@exception none
	 */

	public void assemble(int ObjectType) {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		DataOutputStream dataStream = new DataOutputStream(byteStream);
		 byte[] data_buf = new byte[1024];
		 System.out.println("len of data_buf in line assemble = " + data_buf.length);
		 System.out.println("OBJECT ID sent = " + obj_id);
		 try {
			dataStream.writeByte(DataType.WBDataType);
			dataStream.writeByte(ObjectType);
			dataStream.writeInt(obj_id);
			if (ObjectType != ObjType.DELETE) {
				dataStream.writeByte((byte) color);
				System.out.println("COLOR to send ===> " + color);
				dataStream.writeShort(x1);
				dataStream.writeShort(y1);
				dataStream.writeShort(x2);
				dataStream.writeShort(y2);
			}
		} catch(java.io.IOException e) {
			System.out.println("IOException in Line Object assembling: " + e);
		}

		System.out.println("***************Obj id sent = " + obj_id + " ****");
		data_buf = byteStream.toByteArray();
		System.out.println("num. of valide bytes in the bytestream = " + byteStream.size());
		System.out.println("DataType.WBDataType = " + DataType.WBDataType);
		BaseClassAssemble(data_buf);
	}

	/**
	 * retrieves Line object's parameters from the received packet and
	 * display the line on the drawing canvas of the whiteboard.
	 *
	 * @author: Lai-Tee Cheok
	 *
	 *@param buf buffer that holds parameters in the form of byte array.
	 *@return void
	 *@exception none
	 */

	public void processInData(byte[] buf) {
		/*
		 * the received parameters should not be assigned as global
		 * variables form so that these values won't clash with the
		 * global variables that we want to send out.
		 */
		int received_x1 = 0;
		int received_y1 = 0;
		int received_dest_x = 0;
		int received_dest_y = 0;
		int col = 0;
		int obj_id_recvd = 0;
		ByteArrayInputStream byteStream = new ByteArrayInputStream(buf);
		DataInputStream dataStream = new DataInputStream(byteStream);

		 try {
			obj_id_recvd = dataStream.readInt();
			col = dataStream.readByte() & 255;
			System.out.println("COLOR received ######### " + col);
			received_x1 = dataStream.readUnsignedShort();
			received_y1 = dataStream.readUnsignedShort();
			received_dest_x = dataStream.readUnsignedShort();
			received_dest_y = dataStream.readUnsignedShort();
		} catch(java.io.IOException e) {
			System.out.println("IOException in reading Obj_type: " + e);
		}

		System.out.println("OBJECT ID received = " + obj_id_recvd);
		draw(received_x1, received_y1, received_dest_x, received_dest_y, col, obj_id_recvd);
	}

	/**
	 * return type of the object
	 *
	 *@return type
	 *@exception none
	 */

	public int getType() {
		return type;
	}

	public int getObjID() {
		return obj_id;
	}

}
