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

/**
 * FSWith is a stack-based action and supports the with statement from the
 * ActionScript language.
 * 
 * <pre>
 *  with(_root.movieClip)
 *  {
 *  gotoAndPlay(&quot;frame&quot;);
 *  }
 * </pre>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>A code identifying the action when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>actions</td>
 * <td>An array of actions that will be executed for the specified movie clip.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * The FSWith action temporarily selects the movie clip allowing the following
 * stream of actions to control the movie clip's time-line.
 * </p>
 * 
 * <p>
 * The ActionScript shown above is represented (compiled) into the following
 * actions:
 * </p>
 * 
 * <pre>
 * clipActions.add(new FSPush(&quot;frame&quot;));
 * clipActions.add(new FSPush(1));
 * clipActions.add(new FSPush(&quot;gotoAndPlay&quot;));
 * clipActions.add(FSAction.ExecuteFunction());
 * 
 * // Get the movie clip 
 * 
 * actions.add(new FSPush(&quot;_root&quot;));
 * actions.add(FSAction.GetVariable());
 * actions.add(new FSPush(&quot;movieClip&quot;));
 * actions.add(FSAction.GetAttribute());
 * 
 * actions.add(new FSWith(clipActions));
 * 
 * </pre>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSWith class represents the ActionWith action in the Macromedia Flash
 * (SWF) File Format Specification. It was introduced in Flash 5. FSWith
 * supersedes the FSSetTarget action.
 * </p>
 */
public class FSWith extends FSActionObject
{
	private ArrayList actions = null;

	/*
	 * Variables used when encoding.
	 */
	private int actionsLength = 0;

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

	/**
	 * Constructs an FSWith object with an array of actions.
	 * 
	 * @param anArray
	 *            the array of action objects.
	 */
	public FSWith(ArrayList anArray)
	{
		super(With);
		setActions(anArray);
	}

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

		actions = new ArrayList(obj.actions.size());

		for (Iterator i = obj.actions.iterator(); i.hasNext();)
			actions.add(((FSActionObject) i.next()).clone());
	}

	/**
	 * Adds the action object to the array of actions.
	 * 
	 * @param anAction
	 *            an object belonging to a class derived from FSActionObject.
	 */
	public void add(FSActionObject anAction)
	{
		actions.add(anAction);
	}

	/**
	 * Get the array of actions that are executed for the movie clip target.
	 * 
	 * @return the array of action objects.
	 */
	public ArrayList getActions()
	{
		return actions;
	}

	/**
	 * Set the array of actions that will be executed for the movie clip target.
	 * 
	 * @param anArray
	 *            the array of action objects.
	 */
	public void setActions(ArrayList anArray)
	{
		actions = anArray;
	}

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

		anObject.actions = new ArrayList();

		for (Iterator i = actions.iterator(); i.hasNext();)
			anObject.actions.add(((FSActionObject) i.next()).clone());

		return anObject;
	}

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

		if (super.equals(anObject))
			result = actions.equals(((FSWith) anObject).getActions());

		return result;
	}

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

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

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

		length += 2;

		actionsLength = 0;

		for (Iterator i = actions.iterator(); i.hasNext();)
		{
			FSActionObject currentAction = (FSActionObject) i.next();

			actionsLength += currentAction.length(coder);
			actionsLength += (currentAction.getType() > 128) ? 3 : 1;
		}
		length += actionsLength;

		return length;
	}

	public void encode(FSCoder coder)
	{
		coder.beginObject(name());

		coder.writeWord(type, 1);
		coder.writeWord(length - actionsLength, 2);
		coder.writeWord(actionsLength, 2);

		for (Iterator i = actions.iterator(); i.hasNext();)
		{
			FSActionObject action = (FSActionObject) i.next();

			int objStart = coder.getPointer();
			int start = coder.getPointer()
							+ ((action.getType() > 128) ? 24 : 8);
			int next = start + (action.getLength() << 3);

			action.encode(coder);
			coder.setPointer(next);

			int delta = (coder.getPointer() - next) >> 3;

			if (delta != 0)
			{
				coder.context[FSCoder.CodingError] = 1;
				coder.context[FSCoder.TypeInError] = action.getType();
				coder.context[FSCoder.StartOfError] = objStart >>> 3;
				coder.context[FSCoder.ExpectedLength] = (next - objStart) >>> 3;
				coder.context[FSCoder.Delta] = delta;
			}
		}

		coder.endObject(name());
	}

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

		super.decode(coder);

		actionsLength = coder.readWord(2, false);

		length += actionsLength;

		actions = new ArrayList();

		while (bytesRead < actionsLength)
		{
			FSActionObject anAction = FSMovie.decodeAction(coder);

			bytesRead += anAction.getLength()
							+ ((anAction.getType() >= 128) ? 3 : 1);
			actions.add(anAction);
		}

		coder.endObject(name());
	}
}
