#import "OOPSTimerInspector.h"
#import <appkit/appkit.h>
#import "OOPSTimer.h"

@implementation OOPSTimerInspector

- init
{
    char buf[MAXPATHLEN + 1];
    id bundle;
    
    [super init];
    
    bundle = [NXBundle bundleForClass:[OOPSTimer class]];
    [bundle getPath:buf forResource:"OOPSTimerInspector" ofType:"nib"];
    [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
    return self;
}

- (BOOL)wantsButtons 
{
	return NO; 
}

- updatePriorityField:sender
{
	int aValue;

	if ((sender != priorityField) && (sender != priorityMatrix)) return nil;
	[priorityField setBackgroundGray:0.6666];
	[priorityField  setEditable:NO];
	switch ([[sender selectedCell]tag]) {
		case 0:
			[priorityField setIntValue:1];
			break;
		case 1:
			[priorityField setIntValue:5];
			break;
		case 2:
			[priorityField setIntValue:10];
			break;
		case 3:
			[priorityField setBackgroundGray:1.0];
			[priorityField setEditable:YES];
			break;
		case 10:
			aValue = [priorityField intValue];
			if (aValue < 2) {
				[priorityMatrix setState:1 at:0 :0];
				[priorityField setIntValue:1];
			} else if (aValue == 5) {
				[priorityMatrix setState:1 at:1 :0];
			} else if (aValue == 10) {
				[priorityMatrix setState:1 at:2 :0];
			} else if (aValue < 31) {
				[priorityMatrix setState:1 at:3 :0];
				[priorityField setBackgroundGray:1.0];
				[priorityField setEditable:YES];
			} else {
				[priorityMatrix setState:1 at:0 :0];
				[priorityField setIntValue:1];
			}
			break;
	}
	return self;
}

- updateRepeatField:sender;
{
	if ((sender != repeatNrField) && (sender != repeatNrSlider)) return nil;
	if ([sender tag]==0) {
		[repeatNrSlider setIntValue:[sender intValue]];
	} else {
		[repeatNrField setIntValue:[sender intValue]];
	}
	if ([repeatNrField intValue] == 0 ) {
		[repeatNrField setStringValue:"Forever"];
	}
	return self;
}

- updateIntervalField:sender;
{
	if ((sender != intervalField) && (sender != intervalSlider)) return nil;
	if ([sender tag]==0) {
		[intervalSlider setFloatValue:[sender floatValue]];
	} else {
		[intervalField setIntValue:[sender intValue]];
	}
	return self;
}

- revert:sender
{
	[repeatNrField setIntValue:[object numberOfRepeatsLeft]];
	[self updateRepeatField:repeatNrField];
	[intervalField setFloatValue:[object repeatTimeInterval]];
	[self updateIntervalField:intervalField];
	[priorityField setIntValue:[object priority]];
	[self updatePriorityField:priorityField];
	[tagField setIntValue:[object tag]];
	[autoStartSwitch setState:[object autoStart]];
	return [super revert:sender];
}

- ok:sender
{
	[self updateRepeatField:sender];
	[object setNumberOfRepeats:[repeatNrField intValue]];
	[self updateIntervalField:sender];
	[object setRepeatTimeInterval:[intervalField floatValue]];
	[self updatePriorityField:sender];
	[object setPriority:[priorityField intValue]];
	[object setTag:[tagField intValue]];
	[object setAutoStart:[autoStartSwitch state]];
	return [super ok:sender];
}


@end
