package CharacterExport
{
	
	import flash.net.FileReference;
	import flash.events.*;
	
	public class JSONPNGSetSaver {

		var name:String;
		var json:String;
		var pngs:Array;
		var sheetCount = 0;
		var doneCallback:Function = null;
		public function JSONPNGSetSaver(name:String, json:String, pngs:Array, callback:Function)
		{
			this.name = name;
			this.json = json;
			this.pngs = pngs;
			doneCallback = callback;
		}
		
		public function Save()
		{
			var fr:FileReference = new FileReference();
			fr.save(json, this.name+".json");
			fr.addEventListener(Event.COMPLETE, OutputNextFile);
		}
		
		public function OutputNextFile(e)
		{
			if (pngs.length > 0)
			{
				var pngSheet = pngs.shift();
				sheetCount++;
				var fr:FileReference = new FileReference();
				fr.save(pngSheet, this.name+"_"+((sheetCount < 10) ? "0" : "")+sheetCount+".png");
				fr.addEventListener(Event.COMPLETE, OutputNextFile);
			} else {
				doneCallback();
			}
		}

	}
	
}
