#import <IOBluetooth/Bluetooth.h>
#import <IOBluetooth/objc/IOBluetoothOBEXSession.h>
#import <IOBluetooth/objc/OBEXFileTransferServices.h>
#import <IOBluetoothUI/objc/IOBluetoothDeviceSelectorController.h>

#import "SpamController.h"

@implementation SpamController

- (IBAction) actionSelectDevice: (id) sender
{
	if ( [self selectAndOpenFTPDevice] )
	{
		[mButtonSend setEnabled: TRUE];
	}
	
}

- (IBAction)actionSelectImage:(id)sender
{
NSLog(@"actionSelectImage before sending-------------------");
	if([mBluetoothDevice isConnected])
		{
		NSLog(@"mBluetoothDevice isConnected"); 
		}
		
		if([mOBEXSession hasOpenTransportConnection])
		{
		NSLog(@"mOBEXSession hasOpenTransportConnection"); 
		}		
		
		if([mTransferServices isBusy])
		{
		NSLog(@"mTransferServices isBusy"); 
		}

		if([mTransferServices isConnected])
		{
		NSLog(@"mTransferServices isConnected"); 
		}
	
	
	
	//mImageFilename = @"/Users/chris/Desktop/spam.jpg";
	
	NSOpenPanel *openPanel = [NSOpenPanel openPanel];
	
	[openPanel runModal];
	
	id filenames = [openPanel filenames];
	if(filenames == nil || [filenames count] == 0)
	{
		[mButtonSend setEnabled: FALSE];
		return;
	}
	
	mImageFilename = [NSString stringWithString: [filenames objectAtIndex: 0]];
	[mImageFilename retain];
	
	NSImage *anImage = [[NSImage alloc] initWithContentsOfFile: mImageFilename];
	[mImage setImage: anImage];
	
	
	NSLog(@"image selected");
}
//////////////////////////////////////////////////////////////
- (IBAction)actionSend:(id)sender
{		
		
	OBEXError err = [mTransferServices sendFile: mImageFilename];
	
	if(err != kOBEXSuccess)
	{
		switch(err)
		{
			case kOBEXSessionBusyError:
				NSLog(@"kOBEXSessionBusyError");
				break;
				
			case kOBEXBadArgumentError: 
				NSLog(@"kOBEXBadArgumentError");
				break;
				
			default:
				NSLog(@"unknown error");
		}
	}
}

//////////////////////////////////////////////////////////////////////
- (BOOL) selectAndOpenFTPDevice //
{
	IOBluetoothDeviceSelectorController *deviceSelector;
	NSArray * deviceArray; //
	
	BOOL res = TRUE;//
		
		deviceSelector = [IOBluetoothDeviceSelectorController deviceSelector];
		
		if ( deviceSelector == nil )
		{
			NSLog( @"Error - unable to allocate IOBluetoothDeviceSelectorController.\n" );
			res = FALSE; //
			goto exit;//
		}
		
		
		
		
		if ( [deviceSelector runModal] != kIOBluetoothUISuccess )
		{
			NSLog( @"User has cancelled the device selection.\n" );
			res = FALSE; // 
			goto exit; //
		}
		
		
		deviceArray = [deviceSelector getResults];
		
		
		if ( ( deviceArray == nil ) || ( [deviceArray count] == 0 ) )
		{
			NSLog( @"Error - no selected device.  ***This should never happen.***\n" );
			res = FALSE; //
			goto exit; //
		}
		mBluetoothDevice = [deviceArray objectAtIndex: 0];
		
		if( !mBluetoothDevice ){ res = FALSE; goto exit; }
		[mBluetoothDevice  retain];
		
		
		
		mOBEXSession = [IOBluetoothOBEXSession withDevice: mBluetoothDevice channelID: 10];
		
		
		if( !mOBEXSession ){ res = FALSE; goto exit; }
		[mOBEXSession retain];
		
		
		
		mTransferServices = [OBEXFileTransferServices withOBEXSession: mOBEXSession];
		
		[mTransferServices retain];
		
		
		
		
		OBEXError err = [mTransferServices connectToFTPService];
		
		
		
		
		if(err != kOBEXSuccess)
		{
			NSLog(@"error during connection establishment");
			
			switch(err)
			{
				case kOBEXSessionBusyError:
					NSLog(@"kOBEXSessionBusyError");
					break;
					
				case kOBEXSessionAlreadyConnectedError:
					NSLog(@"kOBEXSessionBusyError");
					break;
					
				case kOBEXNoResourcesError:
					NSLog(@"kOBEXSessionBusyError");
					break;
					
				default: NSLog(@"unknown error");
			}
			
			res = FALSE;
			
			goto exit;
		} else 
		{
			NSLog(@"connectToFTPService returned kOBEXSuccess");
		}
		
		NSLog(@"selectAndOpenFTPDevice----------------------");
		
		
		if([mBluetoothDevice isConnected])
		{
		NSLog(@"mBluetoothDevice isConnected"); 
		}
		
		if([mOBEXSession hasOpenTransportConnection])
		{
		NSLog(@"mOBEXSession hasOpenTransportConnection"); 
		}		
		
		if([mTransferServices isBusy])
		{
		NSLog(@"mTransferServices isBusy"); 
		}

		if([mTransferServices isConnected])
		{
		NSLog(@"mTransferServices isConnected"); 
		}
		
exit:
			
			if( !res )
			{
				
				if( mTransferServices ){	[mTransferServices release];	mTransferServices = nil;}
				if( mOBEXSession ){			[mOBEXSession release];			mOBEXSession = nil; }
				if( mBluetoothDevice ){		[mBluetoothDevice release];		mBluetoothDevice = nil; }
			}
		
		
		//////////
		/*
		 OBEXError erro = [mTransferServices sendFile: @"/Users/davide/Squeak3.7/pietro.jpeg"];
		 if(erro != kOBEXSuccess)
		 {
			 switch(erro)
			 {
				 case kOBEXSessionBusyError:
					 NSLog(@"kOBEXSessionBusyError");
					 break;
					 
				 case kOBEXBadArgumentError: 
					 NSLog(@"kOBEXBadArgumentError");
					 break;
					 
				 default:
					 NSLog(@"unknown error");
			 }
		 }*/
		
		///////////////
		
		
		
		return res;
}



@end
