/*
 * FSDefineShape2.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

import java.util.*;

/**
 * FSDefineShape2 defines a shape to be displayed.
 * 
 * <p>
 * It extends the functionality of the FSDefineShape class by allowing more than
 * 255 fill or line styles to be specified.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSDefineShape2_0">type</a></td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>identifier</td>
 * <td>An unique identifier for this object, in the range 1..65535.</td>
 * </tr>
 * 
 * <tr>
 * <td>bounds</td>
 * <td>The bounding area inside which the shape is drawn.</td>
 * </tr>
 * 
 * <tr>
 * <td>fillStyles</td>
 * <td>An array of fill styles used to fill the area enclosed by the shape.</td>
 * </tr>
 * 
 * <tr>
 * <td>lineStyles</td>
 * <td>An array of line styles used to draw the outline of the shape.</td>
 * </tr>
 * 
 * <tr>
 * <td>shape</td>
 * <td>An FSShape object containing the shape records (FSLine, FSCurve and
 * FSShapeStyle objects) that describe how the shape is drawn and filled shape.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * The shape defines a path containing a mix of straight and curved edges and
 * pen move actions. A path need not be contiguous. When the shape is drawn the
 * FSShapeStyle object selects the line and fill styles, from the respective
 * array, to be used. FSShapeStyle objects can be defined in the shape at any
 * time to change the styles being used. The fill style used can either be a
 * solid colour, a bitmap image or a gradient. The line style specifies the
 * colour and thickness of the line drawn around the shape outline. For both
 * line and fill styles the selected style may be undefined, allowing the shape
 * to be drawn without an outline or left unfilled.
 * </p>
 * 
 * <h1 class="datasheet">Examples</h1>
 * 
 * <p>
 * This example creates a simple rectangle, 200 twips wide and 100 twips high,
 * drawing using a black outline 1 twip wide and filled with a solid red colour.
 * The origin of the shape is at the bottom left corner of the rectangle.
 * </p>
 * 
 * <pre>
 * // Create the bounding rectangle for the shape. The values also specify
 * // the coordinate range of the shape.
 * 
 * FSBounds bounds = new FSBounds(0, 0, 200, 100);
 * 
 * // Define the styles for the shape.
 * 
 * ArrayList fillStyles = new ArrayList();
 * ArrayList lineStyles = new ArrayList();
 * 
 * fillStyles.add(new FSSolidFill(new FSColor(255, 0, 0)));
 * 
 * lineStyles.add(new FSSolidLine(1, new FSColor(0, 0, 0)));
 * 
 * // Draw the shape.
 * 
 * FSShape shape = new FSShape();
 * 
 * // Define the line, fill styles and move to the shape's origin.
 * 
 * shape.add(new FSShapeStyle(1, 1, 0, 0, 0));
 * shape.add(new FSLine(200, 0));
 * shape.add(new FSLine(0, 100));
 * shape.add(new FSLine(-200, 0));
 * shape.add(new FSLine(0, -100));
 * 
 * FSDefineShape shape = new FSDefineShape(movie.newIdentifier(), bounds,
 * 				fillStyles, lineStyles, shape);
 * </pre>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSDefineShape2 class represents the DefineShape2 data structure from the
 * Macromedia Flash (SWF) File Format Specification. It was introduced in Flash
 * 2.
 * </p>
 */
public class FSDefineShape2 extends FSDefineObject
{
	private FSBounds bounds = null;
	private ArrayList fillStyles = null;
	private ArrayList lineStyles = null;
	private FSShape shape = null;

	/**
	 * Construct an FSDefineShape2 object, initialising it with values decoded
	 * from an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSDefineShape2(FSCoder coder)
	{
		super(DefineShape2, 0);
		decode(coder);
	}

	/**
	 * Constructs an FSDefineShape2 object.
	 * 
	 * @param anIdentifier
	 *            the unique identifier for the shape.
	 * @param aBounds
	 *            the bounding rectangle for the shape.
	 * @param fillStyleArray
	 *            the array of fill styles used in the shape.
	 * @param lineStyleArray
	 *            the array of line styles used in the shape.
	 * @param aShape
	 *            the shape to be drawn.
	 */
	public FSDefineShape2(int anIdentifier, FSBounds aBounds,
							ArrayList fillStyleArray, ArrayList lineStyleArray,
							FSShape aShape)
	{
		super(DefineShape2, anIdentifier);
		setBounds(aBounds);
		setFillStyles(fillStyleArray);
		setLineStyles(lineStyleArray);
		setShape(aShape);
	}

	/**
	 * Constructs an FSDefineShape object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSDefineShape object.
	 */
	public FSDefineShape2(FSDefineShape2 obj)
	{
		super(obj);
		bounds = new FSBounds(obj.bounds);

		fillStyles = new ArrayList();

		for (Iterator i = obj.fillStyles.iterator(); i.hasNext();)
			fillStyles.add(((FSFillStyle) i.next()).clone());

		lineStyles = new ArrayList();

		for (Iterator i = obj.lineStyles.iterator(); i.hasNext();)
			lineStyles.add(((FSLineStyle) i.next()).clone());

		shape = new FSShape(obj.shape);
	}

	/**
	 * Add a FSSolidLine object to the array of line styles.
	 * 
	 * @param aLineStyle
	 *            and FSSolidLine object.
	 */
	public void add(FSLineStyle aLineStyle)
	{
		lineStyles.add(aLineStyle);
	}

	/**
	 * Add the fill style object to the array of fill styles.
	 * 
	 * @param aFillStyle
	 *            and FSFillStyle object.
	 */
	public void add(FSFillStyle aFillStyle)
	{
		fillStyles.add(aFillStyle);
	}

	/**
	 * Gets the bounding rectangle for the shape.
	 * 
	 * @return the bounding rectangle for the shape.
	 */
	public FSBounds getBounds()
	{
		return bounds;
	}

	/**
	 * Gets the array fill styles.
	 * 
	 * @return the fill styles used in the shape.
	 */
	public ArrayList getFillStyles()
	{
		return fillStyles;
	}

	/**
	 * Gets the array line styles.
	 * 
	 * @return the line styles used in the shape.
	 */
	public ArrayList getLineStyles()
	{
		return lineStyles;
	}

	/**
	 * Gets the shape.
	 * 
	 * @return the shape to be drawn.
	 */
	public FSShape getShape()
	{
		return shape;
	}

	/**
	 * Sets the bounding rectangle.
	 * 
	 * @param aBounds
	 *            set the bounding rectangle for the shape.
	 */
	public void setBounds(FSBounds aBounds)
	{
		bounds = aBounds;
	}

	/**
	 * Sets the fill styles.
	 * 
	 * @param anArray
	 *            set the fill styles for the shape.
	 */
	public void setFillStyles(ArrayList anArray)
	{
		fillStyles = anArray;
	}

	/**
	 * Sets the line styles.
	 * 
	 * @param anArray
	 *            set the line styles for the shape.
	 */
	public void setLineStyles(ArrayList anArray)
	{
		lineStyles = anArray;
	}

	/**
	 * Sets the shape.
	 * 
	 * @param aShape
	 *            set the shape to be drawn.
	 */
	public void setShape(FSShape aShape)
	{
		shape = aShape;
	}

	public Object clone()
	{
		FSDefineShape2 anObject = (FSDefineShape2) super.clone();

		anObject.bounds = (bounds != null) ? (FSBounds) bounds.clone() : null;

		anObject.fillStyles = new ArrayList();

		for (Iterator i = fillStyles.iterator(); i.hasNext();)
			anObject.fillStyles.add(((FSFillStyle) i.next()).clone());

		anObject.lineStyles = new ArrayList();

		for (Iterator i = lineStyles.iterator(); i.hasNext();)
			anObject.lineStyles.add(((FSLineStyle) i.next()).clone());

		anObject.shape = (shape != null) ? (FSShape) shape.clone() : null;

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSDefineShape2 typedObject = (FSDefineShape2) anObject;

			if (bounds != null)
				result = bounds.equals(typedObject.bounds);
			else
				result = bounds == typedObject.bounds;

			if (fillStyles != null)
				result = result && fillStyles.equals(typedObject.fillStyles);
			else
				result = result && fillStyles == typedObject.fillStyles;

			if (lineStyles != null)
				result = result && lineStyles.equals(typedObject.lineStyles);
			else
				result = result && lineStyles == typedObject.lineStyles;

			if (shape != null)
				result = result && shape.equals(typedObject.shape);
			else
				result = result && shape == typedObject.shape;
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "identifier", identifier);
			Transform.append(buffer, "bounds", bounds, depth);
			Transform.append(buffer, "fillStyles", fillStyles, depth);
			Transform.append(buffer, "lineStyles", lineStyles, depth);
			Transform.append(buffer, "shape", shape, depth);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		int numberOfFillBits = FSCoder.size(fillStyles.size(), false);
		int numberOfLineBits = FSCoder.size(lineStyles.size(), false);

		super.length(coder);

		length += bounds.length(coder);
		length += (fillStyles.size() >= 255) ? 3 : 1;

		for (Iterator styleIterator = fillStyles.iterator(); styleIterator
						.hasNext();)
			length += ((FSTransformObject) styleIterator.next()).length(coder);

		length += (lineStyles.size() >= 255) ? 3 : 1;

		for (Iterator styleIterator = lineStyles.iterator(); styleIterator
						.hasNext();)
			length += ((FSTransformObject) styleIterator.next()).length(coder);

		coder.context[FSCoder.ArrayCountExtended] = 1;
		coder.context[FSCoder.NumberOfFillBits] = numberOfFillBits;
		coder.context[FSCoder.NumberOfLineBits] = numberOfLineBits;

		length += shape.length(coder);

		coder.context[FSCoder.ArrayCountExtended] = 0;
		coder.context[FSCoder.NumberOfFillBits] = 0;
		coder.context[FSCoder.NumberOfLineBits] = 0;

		return length;
	}

	public void encode(FSCoder coder)
	{
		int numberOfFillBits = FSCoder.size(fillStyles.size(), false);
		int numberOfLineBits = FSCoder.size(lineStyles.size(), false);

		super.encode(coder);

		bounds.encode(coder);

		if (fillStyles.size() >= 255)
		{
			coder.writeWord(0xFF, 1);
			coder.writeWord(fillStyles.size(), 2);
		} else
		{
			coder.writeWord(fillStyles.size(), 1);
		}

		for (Iterator styleIterator = fillStyles.iterator(); styleIterator
						.hasNext();)
			((FSTransformObject) styleIterator.next()).encode(coder);

		if (lineStyles.size() >= 255)
		{
			coder.writeWord(0xFF, 1);
			coder.writeWord(lineStyles.size(), 2);
		} else
		{
			coder.writeWord(lineStyles.size(), 1);
		}

		for (Iterator styleIterator = lineStyles.iterator(); styleIterator
						.hasNext();)
			((FSTransformObject) styleIterator.next()).encode(coder);

		coder.context[FSCoder.ArrayCountExtended] = 1;
		coder.context[FSCoder.NumberOfFillBits] = numberOfFillBits;
		coder.context[FSCoder.NumberOfLineBits] = numberOfLineBits;

		shape.encode(coder);

		coder.context[FSCoder.ArrayCountExtended] = 0;
		coder.context[FSCoder.NumberOfFillBits] = 0;
		coder.context[FSCoder.NumberOfLineBits] = 0;

		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		int fillStyleCount = 0;
		int lineStyleCount = 0;

		super.decode(coder);

		int start = coder.getPointer() - 16; // account for identifier

		bounds = new FSBounds(coder);
		fillStyleCount = coder.readWord(1, false);

		if (fillStyleCount == 0xFF)
			fillStyleCount = coder.readWord(2, false);

		fillStyles = new ArrayList(fillStyleCount);

		fillStyles = new ArrayList();

		for (int i = 0; i < fillStyleCount; i++)
		{
			switch (coder.scanWord(1, false))
			{
				case FSFillStyle.Solid:
					fillStyles.add(new FSSolidFill(coder));
					break;
				case FSFillStyle.Linear:
					fillStyles.add(new FSGradientFill(coder));
					break;
				case FSFillStyle.Radial:
					fillStyles.add(new FSGradientFill(coder));
					break;
				case FSFillStyle.Tiled:
					fillStyles.add(new FSBitmapFill(coder));
					break;
				case FSFillStyle.Clipped:
					fillStyles.add(new FSBitmapFill(coder));
					break;
				case FSFillStyle.Unsmoothed_Tiled:
					fillStyles.add(new FSBitmapFill(coder));
					break;
				case FSFillStyle.Unsmoothed_Clipped:
					fillStyles.add(new FSBitmapFill(coder));
					break;
				default:
					break;
			}
		}

		lineStyleCount = coder.readWord(1, false);

		if (lineStyleCount == 0xFF)
			lineStyleCount = coder.readWord(2, false);

		lineStyles = new ArrayList(lineStyleCount);

		for (int i = 0; i < lineStyleCount; i++)
			lineStyles.add(new FSSolidLine(coder));

		coder.context[FSCoder.ArrayCountExtended] = 1;

		int bytesDecoded = (coder.getPointer() - start) >> 3;

		if (coder.context[FSCoder.DecodeShapes] == 1)
			shape = new FSShape(coder);
		else
			shape = new FSShape(coder, length - bytesDecoded);

		coder.context[FSCoder.ArrayCountExtended] = 0;

		coder.endObject(name());
	}
}
