#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;
}

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

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

	[pool release];
}

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

	CPhidget_getSerialNumber((CPhidgetHandle)weight, &serial);
	CPhidget_getDeviceVersion((CPhidgetHandle)weight, &version);
	CPhidget_getDeviceName((CPhidgetHandle)weight, &name);
	CPhidget_getDeviceID((CPhidgetHandle)weight, &devid);
	CPhidgetWeightSensor_getWeightChangeTrigger(weight, &trigger);
	
	[connectedField setStringValue:[NSString stringWithCString:name]];
	[serialField setIntValue:serial];
	[versionField setIntValue:version];
	[triggerField setStringValue:[NSString stringWithFormat:@"%0.2f",trigger]];
	
	[sensorBox setHidden:FALSE];
	[settingsBox setHidden:FALSE];
	
	NSRect frame = [mainWindow frame];
	int heightChange = frame.size.height - 316;
	frame.origin.y += heightChange;
	frame.size.height -= heightChange;
	[mainWindow setMinSize:frame.size];
	[mainWindow setFrame:frame display:YES animate:NO];
	
	[self setPicture:version:devid];
	[pool release];
	[mainWindow display];
}

- (void)phidgetRemoved
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[connectedField setStringValue:@"Nothing"];
	[serialField setStringValue:@""];
	[versionField setStringValue:@""];
	
	[sensorBox setHidden:TRUE];
	[settingsBox setHidden:TRUE];
	
	NSRect frame = [mainWindow frame];
	int heightChange = frame.size.height - 195;
	frame.origin.y += heightChange;
	frame.size.height -= heightChange;
	[mainWindow setMinSize:frame.size];
	[mainWindow setFrame:frame display:YES animate:NO];
	
	[self setPicture:0:0];
	
	[pool release];
	[mainWindow display];
}

/*
* 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];
}

- (void)setPicture:(int)version:(CPhidget_DeviceID)devid
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSString *imgPath;
	
	switch(devid)
	{
		case PHIDID_WEIGHTSENSOR:
			imgPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"1050_0" ofType:@"icns"];
			break;
		default:
			imgPath = nil;
			break;
	}
	
	NSImage *img = [[NSImage alloc]  initByReferencingFile:imgPath];
	
	//otherwise the images are just painted over each other - and the transparency causes trouble
	[pictureBox setImage:nil];
	[pictureBox display];
	if(imgPath!=nil)
		[NSApp setApplicationIconImage: img];
	[pictureBox setImage:img];
	[pictureBox display];
	
	[pool release];
}
@end