package com.gamepatriot.framework2d.implementation;

import android.graphics.Paint;
import android.graphics.Path;

import com.gamepatriot.androidframework.framework.AndroidBasicShape;
import com.gamepatriot.framework2d.implementation.Shape.ShapeType;

/**
 * The BasicShape retains a reference to a single shape draw instruction.
 * 
 * @author Pete Schmitz, May 11, 2013
 *
 */
public class BasicShape implements AndroidBasicShape {
	
	/** (Read-only) The {@link ShapeType type} of shape to be drawn. **/
	public ShapeType type;
	
	/** (Read-only) Stores the radius size of a circle when {@link ShapeType} circle, and font size when ShapeType text. **/
	public float radius;
	
	/** (Read-only) Stores the string to be drawn. Null if not type {@link ShapeType#TEXT TEXT}. **/
	public String text;
	
	/** (Read-only) Contains points used to draw the shape type ({x1, y1, x2, y2, ...}). **/
	public float[] points;
	
	/** (Read-only) The shape path defined for type {@link ShapeType#POLY POLY}; Otherwise null. **/
	public Path path;
	
	/** Reference to the supplied paint that this BasicShape will be drawn with. Altering this will adjust other BasicShapes that share the same paint; Forces {@link Shape}'s default paint when null. **/
	public Paint paint;
	
	public BasicShape(){
		
		//Initialize empty array
		points = new float[0];
	}

	@Override
	public void setAsLine(float $x1, float $y1, float $x2, float $y2, Paint $paint) {
		type = ShapeType.LINE;
		paint = $paint;
		text = null;
		path = null;
		
		registerPoints(new float[]{$x1, $y1, $x2, $y2});
	}

	@Override
	public void setAsRectangle(float $left, float $top, float $right, float $bottom, Paint $paint) {
		type = ShapeType.RECTANGLE;
		paint = $paint;
		text = null;
		path = null;
		
		registerPoints(new float[]{$left, $top, $right, $bottom});
	}

	@Override
	public void setAsCircle(float $x, float $y, float $radius, Paint $paint) {
		type = ShapeType.CIRCLE;
		radius = $radius;
		paint = $paint;
		text = null;
		path = null;
		
		registerPoints(new float[]{$x, $y});
	}

	@Override
	public void setAsPoly(float[] $points, Paint $paint) {
		type = ShapeType.POLY;
		paint = $paint;
		text = null;
		
		registerPoints($points);
		buildPath();
	}

	@Override
	public void setAsText(String $text, float $size, float $x, float $y, Paint $paint) {
		type = ShapeType.TEXT;
		text = $text;
		radius = $size;
		paint = $paint;
		path = null;
		
		registerPoints(new float[]{$x, $y});
	}
	
	@Override
	public void buildPath(){
		buildPath(0f, 0f);
	}
	
	@Override
	public void buildPath(float $offsetX, float $offsetY){
		int $i;
		
		if (path == null) path = new Path();
		else path.reset();
		
		path.moveTo(points[0] + $offsetX, points[1] + $offsetY);
		
		for ($i = 1; $i < points.length/2; $i++){
			path.lineTo(points[$i * 2] + $offsetX, points[$i * 2 + 1] + $offsetY);
		}
		
		path.lineTo(points[0] + $offsetX, points[1] + $offsetY);
	}
	
	@Override
	public Paint getPaint() {
		return paint;
	}
	
	@Override
	public Paint setPaint(Paint $paint) {
		Paint $old = paint;
		paint = $paint;
		
		return $old;
	}
	
	/**
	 * Set the reference points to be used during drawing calls.
	 * @param $points		Array of points designated for use in {@link Shape#draw(android.graphics.Canvas) Shape.draw()}.
	 */
	private void registerPoints(float[] $points){
		points = $points;
	}

	@Override
	public void setText(String $text) {
		text = $text;
	}
	
	
	
}
