package  {

	//import flash.filesystem.File;
	import flash.utils.*;
	import flash.net.*;
	import flash.events.*;
	
	public class FileExportSet {

		var names = [];
		var data = [];

		public function FileExportSet() {};
	
		public function Add(filename, fileData)
		{
			names.push(filename);
			data.push(fileData);
		}
		
		public function AddJSONPNGSet(jsonPNGSet:JSONPNGSet)
		{
			jsonPNGSet.JSONData.files = [];
			for (var i = 0; i < jsonPNGSet.PNGSheets.length; i++)
			{
				var numExt = (jsonPNGSet.PNGSheets.length > 1) ? "_"+i : "";
				Add(jsonPNGSet.AssetName+numExt+".png", jsonPNGSet.PNGSheets[i]);
				jsonPNGSet.JSONData.files.push(jsonPNGSet.AssetName+numExt+".png");
			}
			
			Add(jsonPNGSet.AssetName+".json", jsonPNGSet.JSONString);
		}
		
		public function AddFileExportSet(fileExportSet)
		{
			for (var i = 0; i < fileExportSet.names.length; i++)
			{
				this.names.push(fileExportSet.names[i]);
				this.data.push(fileExportSet.data[i]);
			}
		}

		var completeCallback = null;
		
		/* Save Local */
		public function Save(callback)
		{
			completeCallback = callback;
			OutputNextFile(null);
		}
		
		public function OutputNextFile(e:Event)
		{
			if (names.length > 0)
			{
				var name = this.names.shift();
				var data = this.data.shift();
				var fr:FileReference = new FileReference();
				
				var pathParts = name.split("/");
				if (pathParts.length > 1) name = pathParts[pathParts.length - 1];

				pathParts = name.split("\\");
				if (pathParts.length > 1) name = pathParts[pathParts.length - 1];				
				
				fr.save(data, name);
				fr.addEventListener(Event.COMPLETE, OutputNextFile);
			} else {
				if(completeCallback) completeCallback();
			}
		}
		
		/* Upload to Server */
		var uploadURL;
		var uploadFolder;
		public function Upload(uploadURL, uploadFolder, callback)
		{
			this.uploadURL = uploadURL;
			this.uploadFolder = uploadFolder;
			completeCallback = callback;
			UploadNextFile();
		}
		
		public function UploadNextFile()
		{

			if (names.length > 0)
			{
				var filename = this.names.shift();
				var data = this.data.shift();
				
				var loader = new URLLoader();
				
				var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
				if (!uploadFolder.charAt(uploadFolder.length - 1) == "/") uploadFolder = uploadFolder + "/";
				var uploadRequest:URLRequest = new URLRequest(this.uploadURL+"?name="+uploadFolder+filename);
				uploadRequest.requestHeaders.push(header);
				uploadRequest.method = URLRequestMethod.POST;
				uploadRequest.data = data;
				
				loader.addEventListener(Event.COMPLETE, onLoaded);
				loader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
				
				loader.load(uploadRequest);
				
				_strace("uploading "+filename);
				
			} else {
				if(completeCallback) completeCallback();
			}
		}
		
		public function onLoaded(event)
		{
			_strace("Upload Complete");
			UploadNextFile();
		}
	
		function catchIOError(event:IOErrorEvent)
		{
			_strace("Error caught: "+event.type);
		}		
		

	}
	
}
