package CharacterExport
{
	import flash.utils.*;
	import GraphicExport.*;
	import flash.display.BitmapData;
	import flash.geom.*;
	import flash.display.Sprite;

	public class AnimSetExportBuilder
	{
		protected var animations:Array;
		protected var output:ByteArray = new ByteArray();
		public function get Output():ByteArray { return output; }
		
		public static const OUTPUT_VERSION = 2;
		
		public function AnimSetExportBuilder(animations:Array)
		{
			output.endian = Endian.LITTLE_ENDIAN;
			this.animations = animations;
			
			output.writeUnsignedInt(OUTPUT_VERSION);
			
			output.writeUnsignedInt(animations.length);
			
			for (var i in animations)
			{
				var seq:CharacterSequence = animations[i];
				output.writeUTF(seq.AvatarName);
				output.writeUTF(seq.AnimationName);
				
				var pieces:Array = seq.Exporter.Library.GetAllUsedPieces();
			
				output.writeUnsignedInt(seq.Length);
				
				var ps:Array = seq.PieceSequences;					
				output.writeUnsignedInt(ps.length);
				for (var k = 0; k < ps.length; k++)
				{
					var pieceSequence:PieceSequence = ps[k];
					var piece:Piece = pieceSequence.GetPiece();
					var pieceIndex = pieces.indexOf(piece);
					
					//output.writeUnsignedInt(pieceIndex);
					output.writeUTF(piece.Name);
					
					for (var l = 0; l < seq.Length; l++)
					{
						var info:PieceFrameInfo = pieceSequence.GetFrame(l + 1);
						if (info && info.Present)
						{
							output.writeBoolean(true);
							output.writeInt(info.Depth);
							var decompose = CharacterExporter.Decompose(info.Transform);
							output.writeDouble(decompose.rot);
							output.writeDouble(decompose.sx);
							output.writeDouble(decompose.sy);
							output.writeDouble(info.Transform.tx);
							output.writeDouble(info.Transform.ty);
						} else {
							output.writeBoolean(false);
						}
					}
				}
			}
			output.compress();
		}
	}
}