package
{
	public class Job
	{
		private var callback;
		
		private var id:int;
		private var name:String;
		
		private var details;
		
		private var graphics:Array = new Array();
		
		public function GetID():int { return this.id; }
		public function Job(id:int, graphicsDetails:Array)
		{
			this.id = id;
			for (var i in graphicsDetails)
			{
				var graphic:GraphicExportDef = new GraphicExportDef(this, graphicsDetails[i], i);
				if (graphic.valid)
				{
					graphics.push(graphic);
				}
			}
			Exporter.Instance.Trace("new job: " + id);
		}
		public function FailMessage(err:String)
		{
			Exporter.Instance.Print("Failure: job " + id + ": " + err);
		}
		
		private var goIndex = -1;
		public function Go(callback)
		{
			this.callback = callback;
			
			goIndex = -1;
			
			CheckNextGraphic();
		}
		
		private function CheckNextGraphic()
		{
			goIndex++;
			if (goIndex < graphics.length)
			{
				graphics[goIndex].Go(CheckNextGraphic);
			}
			else				
			{
				Done();
			}
		}

		public function Done()
		{
			Exporter.Instance.Trace("Job.Done() " + id);
			this.callback();
		}
		
		public function GetCompletedData()
		{
			return {id:id};
		}
	}
}