/*************************************************
 *
 * 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 Oval extends ObjBaseClass implements DrawObject, Cloneable {

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

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

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

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

	public boolean mouseDown(int x, int y) {
		x1 = x;
		y1 = y;
		return true;
	}

	public boolean mouseUp(int x, int y) {

		width = x - x1;
		height = y - y1;
		send();
		return true;
	}

	/** Sends this oval object across the multicast network **/
	public void send() {
		assemble(ObjType.OVAL);
	}

	public boolean mouseDrag(int x, int y) {
		old_width = width;
		old_height = height;

		width = x - x1;
		height = y - y1;

		return true;
	}

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

	public void moving(Graphics g, int x, int y) {
		old_width = width;
		old_height = height;

		clean(g);

		x1 = x;
		y1 = y;

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


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

		paint(g);
	}

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

	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);
	}

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

	/**
	 * assembles the Oval 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 Oval 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 Oval object's parameters from the received packet and
	 * display the oval 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);

	}

	public int getType() {
		return type;
	}

	public int getObjID() {
		return obj_id;
	}

	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);
	}

}
