/*
 * FSDefineMovieClip.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.*;

/**
 * FSDefineMovieClip defines a movie clip that animates shapes within a movie.
 * 
 * <p>
 * It contains an array of movie objects that define the placement of shapes,
 * buttons, text and images and the order in which they are displayed through a
 * time-line that is separate from the parent movie.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>identifier</td>
 * <td>A unique identifier, in the range 1..65535, for the movie clip.</td>
 * </tr>
 * 
 * <tr>
 * <td>objects</td>
 * <td>An array of FSMovieObject objects that define the commands that are
 * executed by the Flash Player to animate the movie clip.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * Although a movie clip contains the commands that instructs the Flash Player
 * on how to animate the clip it cannot contain any new definitions of objects.
 * All definitions must be in the main movie. All objects referred to by the
 * movie clip must be also defined in the main movie before they can be used.
 * </p>
 * 
 * <p>
 * When using the FSDefineMovieClip object can only contain objects from the
 * following classes: FSShowFrame, FSPlaceObject, FSPlaceObject2,
 * FSRemoveObject, FSRemoveObject2, FSDoAction, FSStartSound, FSFrameLabel,
 * FSSoundStreamHead, FSSoundStreamHead2 or FSSoundStreamBlock. Other objects
 * are not allowed.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSDefineMovieClip class represents the DefineSprite tag from the
 * Macromedia Flash (SWF) File Format Specification. It was introduced in Flash
 * 3.
 * </p>
 */
public class FSDefineMovieClip extends FSDefineObject
{
	private ArrayList objects = null;

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

	/**
	 * Constructs an FSDefineMovieClip object with the unique identifier. The
	 * Array of objects used to control the movie is empty.
	 * 
	 * @param anIdentifier
	 *            a unique identifier for the movie clip.
	 */
	public FSDefineMovieClip(int anIdentifier)
	{
		super(DefineMovieClip, anIdentifier);
		setObjects(new ArrayList());
	}

	/**
	 * Constructs an FSDefineMovieClip object with the unique identifier and
	 * array of movie objects.
	 * 
	 * @param anIdentifier
	 *            a unique identifier for the movie clip.
	 * @param anArray
	 *            the array of movie objects.
	 */
	public FSDefineMovieClip(int anIdentifier, ArrayList anArray)
	{
		super(DefineMovieClip, anIdentifier);
		setObjects(anArray);
	}

	/**
	 * Constructs an FSDefineShape object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSDefineShape object.
	 */
	public FSDefineMovieClip(FSDefineMovieClip obj)
	{
		super(obj);

		objects = new ArrayList();

		for (Iterator i = obj.objects.iterator(); i.hasNext();)
			objects.add(((FSMovieObject) i.next()).clone());
	}

	/**
	 * Adds the movie object to the array of actions.
	 * 
	 * @param anObject
	 *            an object belonging to a class derived from FSMovieObject.
	 */
	public void add(FSMovieObject anObject)
	{
		objects.add(anObject);
	}

	/**
	 * Gets the array of movie objects.
	 * 
	 * @return the array of movie objects.
	 */
	public ArrayList getObjects()
	{
		return objects;
	}

	/**
	 * Sets the array of movie objects.
	 * 
	 * @param anArray
	 *            the array of movie objects.
	 */
	public void setObjects(ArrayList anArray)
	{
		objects = anArray;
	}

	/**
	 * Gets the total number of frames in the movie clip.
	 * 
	 * @return the number of frames.
	 */
	public int getFrameCount()
	{
		return numberOfFrames();
	}

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

		anObject.objects = new ArrayList();

		for (Iterator i = objects.iterator(); i.hasNext();)
			anObject.objects.add(((FSMovieObject) i.next()).clone());

		return anObject;
	}

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

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

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

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

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "objects", objects, depth);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		super.length(coder);

		length += 4;

		for (Iterator i = objects.iterator(); i.hasNext();)
		{
			FSMovieObject object = (FSMovieObject) i.next();

			int objectLength = object.length(coder);

			length += (object.getExtendLength() || objectLength >= 63) ? objectLength + 6
							: objectLength + 2;
		}
		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);

		coder.writeWord(numberOfFrames(), 2);
		FSMovie.encodeObjects(coder, objects);
		coder.writeWord(0, 2);
		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);

		/* frameCount */coder.readWord(2, false);

		objects = new ArrayList();

		FSMovieObject object = null;

		while ((object = FSMovie.decodeObject(coder)) != null)
		{
			objects.add(object);
		}

		coder.endObject(name());
	}

	private int numberOfFrames()
	{
		int numberOfFrames = 0;

		for (Iterator i = objects.iterator(); i.hasNext();)
		{
			FSMovieObject currentObject = (FSMovieObject) i.next();

			if (currentObject.getType() == FSMovieObject.ShowFrame)
				numberOfFrames += 1;
		}
		return numberOfFrames;
	}
}
