/*************************************************
 *
 * Author: 	Evelyn Lai-Tee Cheok
 *			Department of Electrical Engineering
 *			Center for Telecommunications Research
 *			Schapiro Research Building
 *			Columbia University
 *
 * Email: 	laitee@ctr.columbia.edu
 *
 * Author: 	Chi-Hao Li	
 *			Department of Computer Science
 *			Columbia University
 *
 **************************************************/
import java.lang.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import SketchArea;
import ObjType;
import ObjBaseClass;

public class Circle extends ObjBaseClass implements DrawObject, Cloneable {

	SketchArea parent;
	public int x1, y1, r, cx, cy, old_x1, old_y1;
	public int width, height, old_width, old_height;
	int type, idx;
	int color = 0;
	public Color[] baseColors = new Color[20];
	Random rand_gen = new Random(System.currentTimeMillis());
	public int obj_id = 0;

	/**
         * Constructor
         *
         *@param parent referent to parent class SketchArea
         *@param c color code
         *@return none
         *@exception none
         */

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

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

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

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

	/**
	 * member function to be called when mouse down action
	 *
	 *@param  x x value
	 *@param  y y value
	 *@return true
	 *@exception none
	 */

	public boolean mouseDown(int x, int y) {
		cx = x;
		cy = y;

		return true;
	}

	/**
	 * public member function called when mouse up event
	 *
	 *@param x x value
	 *@param y y value
	 *@return true
	 *@exception none
	 */

	public boolean mouseUp(int x, int y) {

		r = (int) Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));

		x1 = cx - r;
		y1 = cy - r;

		send();	
		return true;
	}

	/** sends this circle object to the multicast network **/
	public void send() {
		assemble(ObjType.CIRCLE);
	}

	/**
	 * puclic member function called when mouse drag
	 *
	 *@param x x value
	 *@param y y value
	 *@return true
	 *@exception none
	 */

	public boolean mouseDrag(int x, int y) {

		r = (int) Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));

		old_x1 = x1;
		old_y1 = y1;

		x1 = cx - r;
		y1 = cy - r;

		old_width = width;
		old_height = height;

		width = r * 2;
		height = r * 2;

		return true;
	}

	/**
	 * move current object on the whetboard
	 *
	 *@param g graphics context
	 *@param x x value
	 *@param y y value
	 *@return void
	 *@exception none
	 */

	public void moving(Graphics g, int x, int y) {
		old_x1 = x1;
		old_y1 = y1;
		old_width = width;
		old_height = height;
		//clean(g);

		x1 = x;
		y1 = y;

		clear(g);
		//paint(g);
		assemble(ObjType.MOVE);
	}

	/**
	 * mouse up event handler while moving the object
	 *
	 *@param g graphics context
	 *@param x x value
	 *@param y y value
	 *@return void
	 *@exception none
	 */

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

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

	public void paint(Graphics g) {
		//setPenColor(g, color);
		g.setColor(baseColors[color]);
		g.drawOval(x1, y1, width, height);
	}

	/**
	 * clear the object on the display
	 *
	 *@param g graphics context
	 *@return void
	 *@exception none
	 */
	public void clear(Graphics g) {
		clean(g);
		paint(g);
	}

	/**
	 * the real function to clean up the object on the whiteboard
	 *
	 *@param g graphics context
	 *@return void
	 *@exception none
	 */

	public void clean(Graphics g) {
		g.setColor(parent.getBackground());
		g.drawOval(old_x1, old_y1, old_width, old_height);
	}

	/**
	 * function to erase the object while selected
	 *
	 *@param g graphics context
	 *@return void
	 *@exception none
	 */

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


	/**
	 * specify the color of the object
	 *
	 *@param g graphics context
	 *@param c color code
	 *@return void
	 *@exception none
	 */

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

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

	public int getType() {
		return type;
	}

	public int getObjID() {
		return obj_id;
	}

	/**
	 * assembles the Circle 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 Circle assemble = " + data_buf.length);

		 try {
			dataStream.writeByte(DataType.WBDataType);
			dataStream.writeByte(ObjectType);
			dataStream.writeInt(obj_id);
			if (ObjectType != ObjType.DELETE) {
				dataStream.writeByte((byte) color);
				dataStream.writeShort(x1);
				dataStream.writeShort(y1);
				dataStream.writeShort(width);
				dataStream.writeShort(height);
			}
		} catch(java.io.IOException e) {
			System.out.println("IOException in Circle Object assembling: " + e);
		}
		data_buf = byteStream.toByteArray();
		System.out.println("num. of valide bytes in the bytestream = "
				   + byteStream.size());
		System.out.println("DataType.WBDataType = " + DataType.WBDataType);
		System.out.println("ObjType.CIRCLE = " + ObjType.CIRCLE);
		BaseClassAssemble(data_buf);
	}

	/**
	 * retrieves Circle object's parameters from the received packet and
	 * display the circle 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 from 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_width = 0;
		int received_height = 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;
			received_x1 = dataStream.readUnsignedShort();
			received_y1 = dataStream.readUnsignedShort();
			received_width = dataStream.readUnsignedShort();
			received_height = dataStream.readUnsignedShort();
		} catch(java.io.IOException e) {
			System.out.println("IOException in reading Circle: " + e);
		}

		draw(received_x1, received_y1, received_width, received_height, col, obj_id_recvd);

	}

	/**
	 * function called to draw object 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;
		 width = xx2;
		 height = yy2;

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

}
