
#import <defaults/defaults.h>
#import "PrefControl.h"
#import "ToyView.h"
#import "ToyWin.h"
#import "ToyWinPCD.h"

#define  OWNER		"ToyViewer"
#define  pcdSIZE	"pcdSIZE"
#define  pcdBRIGHT	"pcdBRIGHT"
#define  adINTERVAL	"adINTERVAL"
#define  originUL	"originUL"
#define  winPOSITION	"winPOSITION"
#define  timedALERT	"timedALERT"
#define  updateSVC	"updateSVC"

static const char *winPosKey[] = { "Auto", "Fix", "ScanFix" };

PrefControl *thePreference = NULL;


@implementation PrefControl

- init
{
	const char *p;

	p = NXGetDefaultValue(OWNER, pcdSIZE);
	pcdSizeValue = p ? atoi(p) : 1;	/* Base/4 */
	p = NXGetDefaultValue(OWNER, pcdBRIGHT);
	pcdBrightValue = p ? atoi(p) : 1; /* Normal */
	[ToyWinPCD setBase:pcdSizeValue bright:pcdBrightValue];

	p = NXGetDefaultValue(OWNER, adINTERVAL);
	adIntervalValue = p ? atoi(p) : 2500; /* 2.5 sec */

	p = NXGetDefaultValue(OWNER, originUL);
	origSWValue = p ? (*p == 'y' || *p == 'Y') : NO;  /* UpperLower */
	(void)[ToyView setOriginUpperLeft: origSWValue];

	p = NXGetDefaultValue(OWNER, winPOSITION);
	windowPosValue = p ? ((*p == 'F') ? 1 : ((*p == 'S') ? 2 : 0)) : 0;
	(void)[ToyWin displayOverKeyWindow: (windowPosValue == pos_Fix)];

	p = NXGetDefaultValue(OWNER, timedALERT);
	timedAltValue = (p && (*p == 'y' || *p == 'Y')) ? 1 : 0;
	if (timedAltValue)
		timedAlert(YES);

	p = NXGetDefaultValue(OWNER, updateSVC);
	updateSvcValue = p ? (*p != 'n' && *p != 'N') : YES;

	thePreference = self;
	return self;
}

- makeKeyAndOrderFront:sender
{
	if (panel == nil) {
		[NXApp loadNibSection:"Preference.nib" owner:self];
		[pcdSize selectCellWithTag: pcdSizeValue];
		[pcdBright selectCellWithTag: pcdBrightValue];
		[adIntSlider setIntValue: adIntervalValue];
		[self changeSlider: adIntSlider];
		[origSW selectCellWithTag: (origSWValue ? 1 : 0)];
		[positionSW selectCellWithTag: windowPosValue];
		[timedAltSW setState: timedAltValue];
		[updateSvcSW setState: (updateSvcValue ? 1 : 0)];
	}
	[panel makeKeyAndOrderFront:sender];
	return self;
}

/* Local Method */
- writeDefault: (const char *)name value:(int)val
{
	char buf[16];
	sprintf(buf, "%d", val);
	NXWriteDefault(OWNER, name, buf);
	return self;
}

- changeValue:sender
{
	int val;
	BOOL bval, pcdflag;

	pcdflag = NO;
	val = [pcdSize selectedTag];
	if (val != pcdSizeValue) {
		pcdflag = YES;
		[self writeDefault: pcdSIZE value:(pcdSizeValue = val)];
	}
	val = [pcdBright selectedTag];
	if (val != pcdBrightValue) {
		pcdflag = YES;
		[self writeDefault: pcdBRIGHT value:(pcdBrightValue = val)];
	}
	if (pcdflag)
		[ToyWinPCD setBase:pcdSizeValue bright:pcdBrightValue];

	bval = ([origSW selectedTag] != 0);
	if (bval != origSWValue) {
		origSWValue = bval;
		NXWriteDefault(OWNER, originUL, (bval ? "YES" : "NO"));
		(void)[ToyView setOriginUpperLeft: origSWValue];
	}
	val = [positionSW selectedTag];
	if (val != windowPosValue) {
		windowPosValue = val;
		NXWriteDefault(OWNER, winPOSITION, winPosKey[windowPosValue]);
		(void)[ToyWin displayOverKeyWindow: (windowPosValue == pos_Fix)];
	}
	val = [timedAltSW state];
	if (val != timedAltValue) {
		timedAltValue = val;
		NXWriteDefault(OWNER, timedALERT, (val ? "YES" : "NO"));
		timedAlert(timedAltValue);
	}
	bval = ([updateSvcSW state] != 0);
	if (bval != updateSvcValue) {
		updateSvcValue = bval;
		NXWriteDefault(OWNER, updateSVC, (bval ? "YES" : "NO"));
	}

	return self;
}

- changeSlider:sender
{
	int	val;
	char	buf[16];

	/* adIntervalValue (1 = 0.1 sec) */
	val = ([sender intValue] + 50) / 100 * 100;
	adIntervalValue = val;
	sprintf(buf, "%d.%d", val / 1000, val % 1000);
	[adIntText setStringValue: buf];
	return self;
}

- (int) autoDisplayInterval
{
	static int oldvalue = 0;
	if (oldvalue != adIntervalValue) {
		oldvalue = adIntervalValue;
		[self writeDefault: adINTERVAL value: adIntervalValue];
	}
	return adIntervalValue;
}

- (int) windowPosition
{
	return windowPosValue;
}

- (BOOL) isUpdatedServices
{
	return updateSvcValue;
}

@end
