/* 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.
*/


var gCanceled = false;

// ------------------------------------------
// GetMajorClassString
// ------------------------------------------
	
function GetMajorClassString(type)
{
	var classString = "";
	switch(type)
	{
		case 0:
			classString = "$$$/Bluetooth/MajorClass/Miscellaneous=Miscellaneous";
			break;
		case 1:
			classString = "$$$/Bluetooth/MajorClass/Computer=Computer";
			break;
		case 2:
			classString = "$$$/Bluetooth/MajorClass/Phone=Phone";
			break;
		case 3:
			classString = "$$$/Bluetooth/MajorClass/LANAccess=LAN Acces Point";
			break;
		case 4:
			classString = "$$$/Bluetooth/MajorClass/Audio=Audio";
			break;
		case 5:
			classString = "$$$/Bluetooth/MajorClass/Peripheral=Peripheral";
			break;
		case 6:
			classString = "$$$/Bluetooth/MajorClass/Imaging=Imaging";
			break;
		default:
			classString = "$$$/Bluetooth/MajorClass/Unclassified=Unclassified";
			break;
	}
	return localize(classString);
}

// ------------------------------------------
// inArray
// ------------------------------------------
	
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;
}

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

	if(file != null)
	{
		var files = task.getElement("files").list;
		files = toArray(files);
		if(file.length > 0)
			task.getElement("btnRemove").enabled = true;
		for(var i = 0; i < file.length; i++)
		{
			var f = new File(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]);
	}
	if(tmpFiles.length <= 0)
		task.getElement("btnRemove").enabled = false;
	task.getElement("files").list = tmpFiles;
}

// ------------------------------------------
// doCancel
// ------------------------------------------
	
function doCancel()
{
	Bluetooth.cancel();
	gCanceled = true;
	task.statusDialog.end();
}

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

// ------------------------------------------
// doRescan
// ------------------------------------------
	
function doRescan()
{
	if(Bluetooth.isAvailable())
	{
		gCanceled = false;
		task.statusDialog.start("doCancel", "doDone");
		task.statusDialog.title = localize("$$$/Bluetooth/SearchBluetoothDevices=Search Bluetooth Devices");
		task.statusDialog.text = localize("$$$/Bluetooth/SearchingForDevices=Searching for Bluetooth devices...");
		
		task.statusDialog.enableCancelButton(true);
		task.statusDialog.enableDoneButton(false);
		
		Bluetooth.searchDevices();
			
		while(! gCanceled && Bluetooth.isSearching()) { task.statusDialog.modalDuring(); }
		
		if(!gCanceled)
			task.statusDialog.end();
			
		if(! gCanceled)
		{
			var count = Bluetooth.getDeviceCount();
			var names = new Array();
			for(var i = 0; i < count; i++)
			{
				var address = Bluetooth.getDeviceAddressByIndex(i);
				var name = Bluetooth.getDeviceNameByIndex(i);
				var type = Bluetooth.getDeviceMajorClassByIndex(i);
				
				var l = new ListItem;
				l.name = name;
				l.address = address;
				l.type = GetMajorClassString(type);
				if(name.length <= 0)
					l.name = address;
					
				names.push(l);
			}
			task.getElement("devices").list = names;
		}
	}
	else
		alert(localize("$$$/Bluetooth/NotAvialable=Bluetooth is not available."));
}

// ------------------------------------------
// 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(selectedFiles[i]);
			
			var item = new ListItem;
			item.filename = file.displayName;
			item.path = file.fsName;
			resources.push(item);
		}
		task.getElement("files").list = resources;
	}
}

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

function execute()
{
	if(Bluetooth.isAvailable())
	{
		var devices = task.getElement("devices").selectedItems;
		var files = task.getElement("files").list;
		
		gCanceled = false;
		task.statusDialog.start("doCancel", "doDone");
		task.statusDialog.title = localize("$$$/Bluetooth/SendToBluetoothDevice=Send to Bluetooth Device");
		
		
		task.statusDialog.enableCancelButton(true);
		task.statusDialog.enableDoneButton(false);
		var error = false;
		
		if(files.length == 0)
		{
			task.statusDialog.text = localize("$$$/Bluetooth/NoSourceFiles=No source file selected.");
			error = true;
		}
		else if(devices.length == 0)
		{
			task.statusDialog.text = localize("$$$/Bluetooth/NoDevices=No device selected.");
			error = true;
		}
	
		
		for(var i = 0;( i < devices.length) && ! gCanceled && ! error; i++)
		{
			var listItem = devices[i];
			var device = new Object;
			device.address = listItem.address;
			device.name = listItem.name;
			var header = "$$$/Bluetooth/OperationStopped=The operation stopped with device ^Q%1^Q.\n%2 Devices following in the queue will not be served.";
			for(var j = 0; (j < files.length) && ! gCanceled && ! error; j++)
			{
				var file = files[j];
				var f = new File(file.path);
				if(f.exists)
				{
					task.statusDialog.text = localize("$$$/Bluetooth/SendingTo=Processing file %1 of %2\nSending to %3...", j +1, files.length, device.name);
					
					Bluetooth.sendFile(device.address, file.path);
					while(! gCanceled && Bluetooth.isSending()) { task.statusDialog.modalDuring(); }
					var status = Bluetooth.getStatus();
					var msg = "";
				
					switch(status)
					{
						case 0: 
						{
							msg = "$$$/Bluetooth/SendingDone=File(s) successfully sent.";	
							task.statusDialog.text = localize(msg);
							break;
						}
						case 1: 
						{
							msg = localize("$$$/Bluetooth/CouldNotConnect=Could not connect to device.");
							task.statusDialog.text = localize(header, device.name, msg);
							error = true;
							break;
						}
						case 2: 
						{
							msg = localize("$$$/Bluetooth/SendingCanceled=Sending canceled by device.");
							task.statusDialog.text = localize(header, device.name, msg);
							error = true;
							break;
						}
					}
				}
				else
				{
					msg = localize("$$$/Bluetooth/FileNotExisting=Source file doesn't exist.");
					task.statusDialog.text = localize(header, device.name, msg);
					error = true;
				}
			}
		}
		task.statusDialog.enableDoneButton(true);
		task.statusDialog.enableCancelButton(false);
		while(! gCanceled) { task.statusDialog.modalDuring(); }
	}
	else
		alert(localize("$$$/Bluetooth/NotAvialable=Bluetooth is not available."));
}

