#import "PhidgetWeightSensorController.h"

CPhidgetWeightSensorHandle weight;

@implementation PhidgetWeightSensorController

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

int gotDetach(CPhidgetHandle phid, void *context) {
	[(id)context phidgetRemoved];
	return 0;
}
int gotWeight(CPhidgetWeightSensorHandle phid, void *context, double val) {
	[(id)context gotWeightChange:val];
	return 0;
}

- (void)gotWeightChange:(double)Weight
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	[weightField setTitleWithMnemonic:[NSString stringWithFormat:@"0.2f kg", Weight]];
	[weightSlider setIntValue:(int)Weight];

	[pool release];
}

- (void)phidgetAdded
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	int serial, version;

	CPhidget_getSerialNumber((CPhidgetHandle)weight, &serial);
	CPhidget_getDeviceVersion((CPhidgetHandle)weight, &version);
	
	[connectedField setTitleWithMnemonic:@"PhidgetWeightSensor Connected"];
	[serialField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", serial]];
	[versionField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", version]];

	[pool release];
}

- (void)phidgetRemoved
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[connectedField setTitleWithMnemonic:@"PhidgetWeightSensor Removed"];
	[serialField setTitleWithMnemonic:@""];
	[versionField setTitleWithMnemonic:@""];
	[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];
	
	CPhidgetWeightSensor_create(&weight);
	
	CPhidgetWeightSensor_set_OnWeightChange_Handler(weight, gotWeight, self);
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)weight, gotAttach, self);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)weight, gotDetach, self);
	
	if(remote)
		CPhidget_openRemote((CPhidgetHandle)weight, serial, NULL, NULL);
	else
		CPhidget_open((CPhidgetHandle)weight, serial);
}

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

@end