/*
 * @(#)ShapeContainer.java	1.0 11-Apr-07
 *
 * IIT Bombay, 
 * Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.
 */
package classes.container;

import types.Command;
import types.ShapeCoordinates;
import interfaces.CommandColleague;
import interfaces.ICommandMediator;
import interfaces.IShape;
import interfaces.IShapeContainer;


/**
 * Holds the Shape class object
 *
 * @author  Prathab, Mukesh
 * @version 1.0, 21-Mar-07
 * @see interfaces.CommandColleague
 */
public class ShapeContainer extends CommandColleague implements IShapeContainer {
	public ShapeCoordinates shapeCoordinates;
	public IShape[] shapeList;
	public int CONTAINER_SIZE = 4;
	public int NO_OF_ROWS = 2;
	public int NO_OF_COLS = 2;
	public boolean isPermanentShape;
	
	/** 
	 * Initializes with shapecoordinates and mediator object
	 * @see types.ShapeCoordinates
	 * @see interfaces.ICommandMediator
	 */
	public ShapeContainer(ShapeCoordinates shapeCoordinates,ICommandMediator commandMediator) {
		super(commandMediator);
		this.shapeCoordinates = shapeCoordinates;
		shapeList = new IShape[CONTAINER_SIZE];
	}
	
	/**
	 * Draws the container by encapuslating the drawing information
	 * using <tt>Command</tt> object. Uses the commandMediator object 
	 * to send the command to <tt>CommandContainer</tt> object
	 */
	public void drawShape() {
		Command cmd_1 = new Command();
		cmd_1.methodName = new String("drawRect");
		cmd_1.args = new String[4];
		cmd_1.args[0] = String.valueOf(shapeCoordinates.x);
		cmd_1.args[1] = String.valueOf(shapeCoordinates.y);
		cmd_1.args[2] = String.valueOf(shapeCoordinates.width);
		cmd_1.args[3] = String.valueOf(shapeCoordinates.height);
		commandMediator.sendCommand(cmd_1,isPermanentShape);
		
		Command cmd_2 = new Command();
		cmd_2.methodName = new String("drawRect");
		cmd_2.args = new String[4];
		cmd_2.args[0] = String.valueOf(shapeCoordinates.x);
		cmd_2.args[1] = String.valueOf(shapeCoordinates.y);
		cmd_2.args[2] = String.valueOf(shapeCoordinates.width/2);
		cmd_2.args[3] = String.valueOf(shapeCoordinates.height/2);
		commandMediator.sendCommand(cmd_2,isPermanentShape);
		
		Command cmd_3 = new Command();
		cmd_3.methodName = new String("drawRect");
		cmd_3.args = new String[4];
		cmd_3.args[0] = String.valueOf(shapeCoordinates.x + shapeCoordinates.width/2);
		cmd_3.args[1] = String.valueOf(shapeCoordinates.y + shapeCoordinates.height/2);
		cmd_3.args[2] = String.valueOf(shapeCoordinates.width/2);
		cmd_3.args[3] = String.valueOf(shapeCoordinates.height/2);
		commandMediator.sendCommand(cmd_3,isPermanentShape);
	}
	
	/**
	 * Adds the given shape object to container
	 * @param shape shape to be added
	 * @return <tt>true</tt> if the shape object is added
	 */
	public boolean addShape(IShape shape) {
		int widthInc = shapeCoordinates.width/NO_OF_COLS;
  	  	int heightInc = shapeCoordinates.height/NO_OF_ROWS;
  	  	int delta = 15;
  	  	
		for(int i=0, yw=0; i< NO_OF_COLS; i++, yw+=heightInc)
  		  for(int j=0, xw=0; j< NO_OF_ROWS;j++, xw+=widthInc) {
  			  int index = (i* (NO_OF_COLS -1) ) +(i+j);
  			if(shapeList[index] == null) {
  				shape.changeShapeXYCoordinates(shapeCoordinates.x + xw + delta,
  						shapeCoordinates.y + yw + delta);
  				shapeList[index] = shape;
  				return true;
  	    	  }
  		  }

		return false;
	}
	
	/**
	 * Removes the given shape from the container
	 * @param shape shape to be removed
	 * @return <tt>true</tt> if the shape object is removed
	 */
	public boolean removeShape(IShape shape) {
		for(int i=0; i< CONTAINER_SIZE ; i++) {
			if(shapeList[i]!= null && shapeList[i].equals(shape)) {shapeList[i]= null;return true;}
		}
		return false;
	}
	
	/**
	 * Returns the shape object for the given index
	 * @param index index to be retrieved in the ShapeList
	 * @return <tt>IShape</tt> object type of the given index
	 */
	public IShape getShape(int index) {
		if(index >= 0 && index < CONTAINER_SIZE) return shapeList[index];
		else return null;
	}
}
