/*
 * FSMovieObject.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 FSMovieObject is the root class for objects that represent each of the
 * tagged data structures that make up the Flash file format specification.
 * 
 * Each of the major data structures defined in the Flash file format
 * specification start with a header that contains the type identifying the data
 * structure followed by the number of bytes that the body of the encoded data
 * structure occupies in the file. The FSMovieObject manages the encoding and
 * decoding of this information.
 * 
 * This class is primarily used internally in the library however the getType()
 * method is useful when manipulating decoded Flash files. Rather than using the
 * runtime type checking provided in the Java environment the method can be used
 * to identify the class, derived from FSMovieObject, that represents each data
 * structure, improving performance.
 */

public abstract class FSMovieObject extends FSTransformObject
{
	/** Type used to identify ShowFrame objects. */
	public static final int ShowFrame = 1;

	/** Type used to identify DefineShape objects. */
	public static final int DefineShape = 2;

	/** Type used to identify PlaceObject objects. */
	public static final int PlaceObject = 4;

	/** Type used to identify RemoveObject objects. */
	public static final int RemoveObject = 5;

	/** Type used to identify DefineJPEGImage objects. */
	public static final int DefineJPEGImage = 6;

	/** Type used to identify DefineButton objects. */
	public static final int DefineButton = 7;

	/** Type used to identify JPEGTables objects. */
	public static final int JPEGTables = 8;

	/** Type used to identify SetBackgroundColor objects. */
	public static final int SetBackgroundColor = 9;

	/** Type used to identify DefineFont objects. */
	public static final int DefineFont = 10;

	/** Type used to identify DefineText objects. */
	public static final int DefineText = 11;

	/** Type used to identify DoAction objects. */
	public static final int DoAction = 12;

	/** Type used to identify FontInfo objects. */
	public static final int FontInfo = 13;

	/** Type used to identify DefineSound objects. */
	public static final int DefineSound = 14;

	/** Type used to identify StartSound objects. */
	public static final int StartSound = 15;

	/** Type used to identify SoundStreamHead objects. */
	public static final int SoundStreamHead = 18;

	/** Type used to identify SoundStreamBlock objects. */
	public static final int SoundStreamBlock = 19;

	// Flash 2
	// / Type used to identify ButtonSound objects.
	public static final int ButtonSound = 17;

	// / Type used to identify DefineImage objects.
	public static final int DefineImage = 20;

	// / Type used to identify DefineJPEGImage2 objects.
	public static final int DefineJPEGImage2 = 21;

	// / Type used to identify DefineShape2 objects.
	public static final int DefineShape2 = 22;

	// / Type used to identify ButtonColorTransform objects.
	public static final int ButtonColorTransform = 23;

	// / Type used to identify Protect objects.
	public static final int Protect = 24;

	// Flash 3
	// / Type used to identify Free objects.
	public static final int Free = 3;

	// / Type used to identify PlaceObject2 objects.
	public static final int PlaceObject2 = 26;

	// / Type used to identify RemoveObject2 objects.
	public static final int RemoveObject2 = 28;

	// / Type used to identify DefineShape3 objects.
	public static final int DefineShape3 = 32;

	// / Type used to identify DefineText2 objects.
	public static final int DefineText2 = 33;

	// / Type used to identify DefineButton2 objects.
	public static final int DefineButton2 = 34;

	// / Type used to identify DefineJPEGImage3 objects.
	public static final int DefineJPEGImage3 = 35;

	// / Type used to identify DefineImage2 objects.
	public static final int DefineImage2 = 36;

	// / Type used to identify DefineMovieClip objects.
	public static final int DefineMovieClip = 39;

	// / Type used to identify FrameLabel objects.
	public static final int FrameLabel = 43;

	// / Type used to identify SoundStreamHead2 objects.
	public static final int SoundStreamHead2 = 45;

	// / Type used to identify DefineMorphShape objects.
	public static final int DefineMorphShape = 46;

	// / Type used to identify DefineFont2 objects.
	public static final int DefineFont2 = 48;

	// Flash 4
	// / Type used to identify PathsArePostscript objects.
	public static final int PathsArePostscript = 25;

	// / Type used to identify DefineTextField objects.
	public static final int DefineTextField = 37;

	// / Type used to identify QuicktimeMovie objects.
	public static final int QuicktimeMovie = 38;

	// / Type used to identify SerialNumber objects.
	public static final int SerialNumber = 41;

	// / Type used to identify DefineBitsPtr objects.
	public static final int DefineBitsPtr = 1023;

	// Flash 5
	// / Type used to identify Export objects.
	public static final int Export = 56;

	// / Type used to identify Import objects.
	public static final int Import = 57;

	// / Type used to identify EnableDebugger objects.
	public static final int EnableDebugger = 58;

	// Flash 6
	// / Type used to identify Initialize objects.
	public static final int Initialize = 59;

	// / Type used to identify DefineVideo objects.
	public static final int DefineVideo = 60;

	// / Type used to identify VideoFrame objects.
	public static final int VideoFrame = 61;

	// / Type used to identify FontInfo2 objects.
	public static final int FontInfo2 = 62;

	// / Type used to identify EnableDebugger2 objects.
	public static final int EnableDebugger2 = 64;

	// Flash 7
	// / Type used to identify LimitScript objects.
	public static final int LimitScript = 65;

	// / Type used to identify TabOrder objects.
	public static final int TabOrder = 66;

	protected int type = 0;

	protected int length = 0;

	protected boolean extendLength = false;

	/**
	 * Constructs a movie object with the specified type.
	 * 
	 * @param aType
	 *            an identifier indicating the type of movie object.
	 */
	protected FSMovieObject(int aType)
	{
		type = aType;
	}

	/**
	 * Constructs a movie object by copying an existing one.
	 * 
	 * @param obj
	 *            an FSMovieObject object.
	 */
	protected FSMovieObject(FSMovieObject obj)
	{
		type = obj.type;
		length = obj.length;
		extendLength = obj.extendLength;
	}

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

	int getLength()
	{
		return length;
	}

	boolean getExtendLength()
	{
		return extendLength;
	}

	/**
	 * 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 == ((FSMovieObject) anObject).getType();
		}
		return result;
	}

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

	public int length(FSCoder coder)
	{
		coder.context[FSCoder.Type] = type;

		length = 0;

		return length;
	}

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

		coder.context[FSCoder.Type] = type;

		if (extendLength == false && length < 63)
		{
			coder.writeWord((type << 6) | length, 2);
		} else
		{
			coder.writeWord((type << 6) | 0x3F, 2);
			coder.writeWord(length, 4);
		}
	}

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

		type = coder.scanWord(2, false) >> 6;
		length = coder.scanWord(2, false) & 0x3F;

		coder.context[FSCoder.Type] = type;
		coder.setPointer(coder.getPointer() + 16);

		if (length == 0x3F)
		{
			length = coder.readWord(4, false);
			extendLength = true;
		}
	}
}
