/* ADOBE SYSTEMS INCORPORATED
* Copyright 2008 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
* terms of the Adobe license agreement accompanying it.  If you have received this file from a 
* source other than Adobe, then your use, modification, or distribution of it requires the prior 
* written permission of Adobe.
*/

const kCHUNK = 65535;

var gCanceled = false;


var PATH_SEP = "";
if(Folder.fs == "Windows")
	PATH_SEP = "\\";
else if(Folder.fs == "Macintosh")
	PATH_SEP = "/";


// ------------------------------------------
// toArray
// ------------------------------------------
	
function inArray(list, elem)
{
	var found = false;
	for(var i = 0; (i < list.length) && ! found; i++)
	{
		if(elem.path == list[i].path)
			found = true;
	}
	return found;
}

// ------------------------------------------
// toArray
// ------------------------------------------

function toArray(o)
{
	var a = new Array();
	for(var i = 0; i < o.length; i++)
		a.push(o[i]);
	
	return a;
}

// ------------------------------------------
// onSelectionChanged
// ------------------------------------------

function onSelectionChanged()
{
	var files  = task.getElement("files").selectedItems;
	task.getElement("btnRemove").enabled = (files.length == 0) ? false : true;
}

// ------------------------------------------
// doAddFile
// ------------------------------------------

function doAddFile()
{
	var file = project.openResourcesDialog();

	if(file != null)
	{
		var files = task.getElement("files").list;
		files = toArray(files);
		
		for(var i = 0; i < file.length; i++)
		{
			var f = new File(encodeURI(file[i]));
			var item = new ListItem;
			item.filename = f.displayName;
			item.path = f.fsName;
			if(! inArray(files, item))
				files.push(item);
		}
		task.getElement("files").list = files;
	}
}

// ------------------------------------------
// doRemoveFile
// ------------------------------------------

function doRemoveFile()
{
	var selectedFiles = task.getElement("files").selectedItems;
	var all = task.getElement("files").list;
	var tmpFiles = new Array();
	for(i = 0; i < all.length; i++)
	{
			var found = false;
			for(j = 0; (j < selectedFiles.length) && ! found; j++)
			{
				if(all[i].path == selectedFiles[j].path)
					found = true;
			}
			if(! found)
				tmpFiles.push(all[i]);
	}
	task.getElement("files").list = tmpFiles;
}

// ------------------------------------------
// doSelectTargetFolder
// ------------------------------------------

function doSelectTargetFolder()
{
	var dir = Folder.selectDialog(localize("$$$/SendTo/SelectFolder=Select target folder"));
	if(dir != null)
	{
		task.getElement("target").text = dir.fsName;
	}
}

// ------------------------------------------
// doCancel
// ------------------------------------------

function doCancel()
{
	task.statusDialog.end();
	gCanceled = true;
}

// ------------------------------------------
// doDone
// ------------------------------------------

function doDone()
{
	task.statusDialog.end();
	gCanceled = true;
}

// ------------------------------------------
// nextFileChunk
// ------------------------------------------

function nextFileChunk(source, destination)
{
	var data = source.read (kCHUNK);
	var status = destination.write (data);

	return ! source.eof;
}
	

// ------------------------------------------
// install
// ------------------------------------------

function install()
{
	task.getElement("btnRemove").enabled = false;
}

// ------------------------------------------
// configure
// ------------------------------------------

function configure(proTask)
{
	if(proTask)
	{
		task.getElement("btnRemove").enabled = true;
		var selectedFiles = project.selectedResources;
		var resources = new Array;
		for(var i = 0; i < selectedFiles.length; i++)
		{
			var file = new File(encodeURI(selectedFiles[i]));
			
			var item = new ListItem;
			item.filename = file.displayName;
			item.path = file.fsName;
			resources.push(item);
		}
		task.getElement("files").list = resources;
	}
	else
	{
		task.getElement("btnRemove").enabled = false;
	}
}

// ------------------------------------------
// execute
// ------------------------------------------

function execute()
{
	var files  = task.getElement("files").list;
	var target  = task.getElement("target").text;
	var folder = new Folder(target);
	var msg = "";
	gCanceled = false;
	
	task.statusDialog.start("doCancel", "doDone");
	task.statusDialog.title = localize("$$$/SendTo/Copying=Copying Files");

	
	task.statusDialog.enableCancelButton(true);
	task.statusDialog.enableDoneButton(false);
	var error = false;
	
	if(files.length == 0)
	{
		task.statusDialog.text = localize("$$$/SendTo/NoSourceFiles=No source files selected.");
		error = true;
	}
	var header = "$$$/SendTo/OperationStopped=The operation stopped with file ^Q%1^Q.\n%2 Files following in the queue will not be transmitted.";
	if(folder.exists)
	{
		for(var i = 0; (i < files.length) && ! error && ! gCanceled; i++)
		{
			var file = new File(encodeURI(files[i].path));
			var displayName = decodeURI(file.name);
			task.statusDialog.text = localize("$$$/SendTo/CopyingFile=Processing file %1 of %2\nCopying ^Q%3^Q...", i +1 , files.length, displayName);
		
			if(file.exists)
			{
				
				var fileName = folder.fsName + PATH_SEP + file.name;
				var destination = new File(fileName);
				
				if(file.open ("r") && destination.open("w"))
				{
					file.encoding = "binary";
					destination.encoding = "binary";
					
					while(! gCanceled && nextFileChunk(file, destination)) 
					{
						task.statusDialog.modalDuring();
					}
					file.close();
					destination.close();
					task.statusDialog.text = localize("$$$/SendTo/CopyingSuccess=File(s) successfully copied.");
					if(gCanceled)
					{
						destination.remove();
					}
				}
				else
				{
					msg = localize("$$$/SendTo/CopyingFailed=Copying failed.");
					task.statusDialog.text = localize(header, displayName, msg);
					error = true;
				}
			}
			else
			{
				msg = localize("$$$/SendTo/FileNotExisting=Source file doesn't exist.");
				task.statusDialog.text = localize(header, displayName, msg);
				error = true;
			}
		}
	}
	else 
	{
		msg = localize("$$$/SendTo/FolderNotExisting=Target folder doesn't exist.");
		task.statusDialog.text = msg;
	}
	task.statusDialog.enableDoneButton(true);
	task.statusDialog.enableCancelButton(false);
	while(! gCanceled) {task.statusDialog.modalDuring();}
	
}





