#import "PhidgetPHSensorController.h"

CPhidgetPHSensorHandle ph;

@implementation PhidgetPHSensorController

int gotAttach(CPhidgetHandle phid, void *context) {
	[(id)context phidgetAdded];
	return 0;
}

int gotDetach(CPhidgetHandle phid, void *context) {
	[(id)context phidgetRemoved];
	return 0;
}

int gotPH(CPhidgetPHSensorHandle phid, void *context, double val) {
	[(id)context gotPHChange:val];
	return 0;
}

- (void)gotPHChange:(double)val
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	double potential;
	[phField setTitleWithMnemonic:[NSString stringWithFormat:@"%0.4f",val]];
	CPhidgetPHSensor_getPotential(ph, &potential);
	[potentialField setTitleWithMnemonic:[NSString stringWithFormat:@"%0.4f",potential]];
	[pool release];
}

- (IBAction)setChangeTrigger:(id)sender
{
	CPhidgetPHSensor_setPHChangeTrigger(ph, [triggerField floatValue]);
}

- (void)phidgetAdded
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	int serial, version;
	double trigger;
	const char *name;
	
	CPhidgetPHSensor_setPHChangeTrigger(ph, 0.05);

	CPhidget_getSerialNumber((CPhidgetHandle)ph, &serial);
	CPhidget_getDeviceVersion((CPhidgetHandle)ph, &version);
	CPhidget_getDeviceName((CPhidgetHandle)ph, &name);
	CPhidgetPHSensor_getPHChangeTrigger(ph, &trigger);
	
	[connectedField setTitleWithMnemonic:[NSString stringWithCString:name]];
	[serialField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", serial]];
	[versionField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", version]];
	[triggerField setTitleWithMnemonic:[NSString stringWithFormat:@"%0.2f", trigger]];
	
	[triggerField setEnabled:true];
	[triggerButton setEnabled:true];

	[pool release];
}

- (void)phidgetRemoved
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[connectedField setTitleWithMnemonic:@"Nothing"];
	[serialField setTitleWithMnemonic:@""];
	[versionField setTitleWithMnemonic:@""];
	[phField setTitleWithMnemonic:@""];
	[potentialField setTitleWithMnemonic:@""];
	[triggerField setTitleWithMnemonic:@""];
	
	[triggerField setEnabled:false];
	[triggerButton setEnabled:false];
	
	[pool release];
}

/*
* This gets run when the GUI gets displayed
*/
- (void)awakeFromNib
{
	int serial = -1, remote = 0;
	NSArray *args = [[NSProcessInfo processInfo] arguments];
	if([args count] > 1)
	{
		if([[args objectAtIndex:1] isEqualToString:@"remote"])
			remote = 1;
		serial = strtol([[args objectAtIndex:[args count]-1] UTF8String], NULL, 10);
		if(serial == 0) serial = -1;
	}
	
	[mainWindow setDelegate:self];
	
	CPhidgetPHSensor_create(&ph);
	
	CPhidgetPHSensor_set_OnPHChange_Handler(ph, gotPH, self);
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)ph, gotAttach, self);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)ph, gotDetach, self);
	
	if(remote)
		CPhidget_openRemote((CPhidgetHandle)ph, serial, NULL, NULL);
	else
		CPhidget_open((CPhidgetHandle)ph, serial);
}

- (void)windowWillClose:(NSNotification *)aNotification {
	CPhidget_close((CPhidgetHandle)ph);
	CPhidget_delete((CPhidgetHandle)ph);
	ph = NULL;
	[NSApp terminate:self];
}

@end