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

/**
 * The FSActionObject is a base class for the classes that represent the
 * different types of actions defined in the Flash file format specification.
 * The function of the class is the same as the FSMovieObject class however the
 * format of the encoded binary data is different.
 * 
 * This class is primarily used internally in the library however the getType()
 * method is useful when manipulating decoded Flash files. Rather than use
 * run-time type inspection provided by the programming environment the method
 * can be used to identify the action that a given object represents. The
 * getType() method is essential when examining FSAction objects which represent
 * a large number byte-codes which perform stack-based operations such as add,
 * subtract, etc.
 */
public abstract class FSActionObject extends FSTransformObject
{
	/** Type identifying a GotoFrame action. */
	public static final int GotoFrame = 129;

	/** Type identifying a GetUrl action. */
	public static final int GetUrl = 131;

	// Flash 3
	/** Type identifying a WaitForFrame action. */
	public static final int WaitForFrame = 138;

	/** Type identifying a SetTarget action. */
	public static final int SetTarget = 139;

	/** Type identifying a GotoLabel action. */
	public static final int GotoLabel = 140;

	// Flash 4
	/** Type identifying a WaitForFrame2 action. */
	public static final int WaitForFrame2 = 141;

	/** Type identifying a Push action. */
	public static final int Push = 150;

	/** Type identifying a Jump action. */
	public static final int Jump = 153;

	/** Type identifying a GetUrl2 action. */
	public static final int GetUrl2 = 154;

	/** Type identifying an If action. */
	public static final int If = 157;

	/** Type identifying a Call action. */
	public static final int Call = 158;

	/** Type identifying a GotoFrame2 action. */
	public static final int GotoFrame2 = 159;

	// Flash 5
	/** Type identifying a RegisterCopy action. */
	public static final int RegisterCopy = 135;

	/** Type identifying a Table action. */
	public static final int Table = 136;

	/** Type identifying a With action. */
	public static final int With = 148;

	/** Type identifying a NewFunction action. */
	public static final int NewFunction = 155;

	// Flash 7
	/** Type identifying a NewFunction2 action. */
	public static final int NewFunction2 = 142;

	/** Type identifying a ExceptionHandler action. */
	public static final int ExceptionHandler = 143;

	protected int type = 0;

	protected int length = 0;

	protected FSActionObject(int aType)
	{
		type = aType;
	}

	protected FSActionObject(FSActionObject obj)
	{
		type = obj.type;
		length = obj.length;
	}

	/**
	 * Gets the code used that identifies the type of the action when it is
	 * encoded.
	 * 
	 * @return the code used to denote the type of action performed.
	 */
	public int getType()
	{
		return type;
	}

	int getLength()
	{
		return length;
	}

	/**
	 * Length is used to calculate the then of the action when it is encoded to
	 * the binary Flash file format. This method can be used to calculate the
	 * size of offsets and jumps for FSIf and FSJump actions.
	 * 
	 * @return the length of the encoded action in bytes.
	 */
	public int length()
	{
		int encodedLength = (type > 128) ? 3 : 1;

		encodedLength += length(new FSCoder(FSCoder.LITTLE_ENDIAN, new byte[0]));

		return encodedLength;
	}

	/**
	 * Returns true if anObject is equal to this one. Objects are considered
	 * equal if they would generate identical binary data when they are encoded
	 * to a Flash file.
	 * 
	 * @return true if this object would be identical to anObject when encoded.
	 */
	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			result = type == ((FSActionObject) anObject).type;
		}
		return result;
	}

	public int length(FSCoder coder)
	{
		length = 0;

		return length;
	}

	public void encode(FSCoder coder)
	{
		coder.beginObject(name());
		coder.writeWord(type, 1);

		if (type >= 128)
			coder.writeWord(length, 2);
	}

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

		type = coder.readWord(1, false);

		if (type >= 128)
			length = coder.readWord(2, false);
		else
			length = 0;
	}
}
