#import "Aquarium.h"
#import "Fish.h"
#import "Thinker.h"
#import <libc.h>
#import <objc/List.h>
#import <dpsclient/wraps.h>
#import <appkit/appkit.h>

static BOOL prefLoaded = FALSE;
static char moduleDir[MAXPATHLEN];
@implementation AquariumView

- oneStep
{
	[fishList makeObjectsPerform:@selector(erase)];
	
	if(!restart_flag){
		[fishList makeObjectsPerform:@selector(move)];
	}
	else {
		restart_flag = NO;
		[self restart];
	}
	
	return self;
}

- initFrame:(NXRect *)frameRect
{
	[super initFrame:frameRect];
	[self restart];	
	return self;
	
}

- restart
{
	int x;
	float r = 0.0, g = 0.0, b = 0.0;
	const char *ptr;
	int fishNumber = 6;
	
	ptr = NXGetDefaultValue([NXApp appName], "AquariumColor");
    if (ptr)  sscanf (ptr, "%f %f %f", &r, &g, &b );

	aquariumColor = NXConvertRGBToColor(r,g,b);
	NXSetColor(aquariumColor);

	if (fishList == nil)
		fishList = [List allocFromZone:[self zone]] ;

	if ([fishList count] > 0)
		[fishList freeObjects];

	ptr = NXGetDefaultValue([NXApp appName], "AquariumFishNumber");
    if (ptr)  sscanf (ptr, "%d", &fishNumber );

	fishList = [[List allocFromZone:[self zone]] init];
	for (x=0;x<fishNumber;x++)
		[fishList addObject:[[Fish allocFromZone:[self zone]] init:self]];

	return self;
}

//-free 
//{
//	[[fishList freeObjects] free];
//	return [super free];
//}
	

- drawSelf:(const NXRect *)rects :(int)rectCount 
{
	if (!rects || !rectCount) return self;

	NXSetColor(aquariumColor);
	NXRectFill(rects);

	return self;
}

- newViewSize 
{
	[fishList makeObjectsPerform:@selector(viewDidResize)];
	return self;
}

- (const char *)windowTitle
{
	return "Fish Are So Relaxing";
}

- didLockFocus
{
	return self;
}

- sizeTo:(NXCoord)width :(NXCoord)height
{
	[super sizeTo:width :height];
	[self newViewSize];
	return self;
}

- (BOOL)useBufferedWindow
{
		return YES;
}

-inspector:sender
{
	char buf[MAXPATHLEN];
		
	sprintf(moduleDir,"%s",[sender moduleDirectory:"Aquarium"]);

	if (!sharedInspectorPanel) {
		sprintf(buf,"%s/AquariumView.nib",moduleDir);
        [NXApp loadNibFile:buf owner:self withNames:NO];

	}
	
	return sharedInspectorPanel;
}


- changeFishNumber:sender
{
	int i;
	char str[4];

	i = [sender floatValue];
    sprintf(str, "%d", i);
	[fishNumberForm setStringValue: str at:0];
	return self;
}

- changeFishSpeed:sender
{
	int i;
	char str[4];

	i = [sender floatValue];
    sprintf(str, "%d", i);
	[fishSpeedForm setStringValue: str at:0];
	return self;
}

- changeDissolveFactor:sender
{
	int i;
	char str[4];

	i = [sender floatValue];
    sprintf(str, "%d", i);
	[dissolveForm setStringValue: str at:0];
	return self;
}


	
- loadPref:sender
{
	char buf[MAXPATHLEN];
	float r = 0.0, g = 0.0, b = 0.0, x;
	int i = 6;	
	const char *ptr;
	char str[4];
	char fishType[10] = "salt"; 

	if (!prefLoaded) 
	{
		sprintf(buf,"%s/AquariumPref.nib",moduleDir);
        [NXApp loadNibFile:buf owner:self withNames:YES];
		prefLoaded = TRUE;

		ptr = NXGetDefaultValue([NXApp appName], "AquariumColor");
   		if (ptr)  sscanf (ptr, "%f %f %f", &r, &g, &b );
		[colorWell setColor:NXConvertRGBToColor(r,g,b)];
		
		ptr = NXGetDefaultValue([NXApp appName], "AquariumFishNumber");
  		if (ptr)  sscanf (ptr, "%d", &i );
		x = i;
		[fishNumberSlider setFloatValue: x];
		sprintf(str, "%d", i);
		[fishNumberForm setStringValue: str];	

		ptr = NXGetDefaultValue([NXApp appName], "AquariumFishSpeed");
  		if (ptr)  sscanf (ptr, "%d", &i );
		x = i;
		[fishSpeedSlider setFloatValue: x];
		sprintf(str, "%d", i);
		[fishSpeedForm setStringValue: str];	

		ptr = NXGetDefaultValue([NXApp appName], "AquariumFishType");
  		if (ptr)  sscanf (ptr, "%s", fishType );		
		if(!strcmp(fishType,"salt")) [fishTypeMatrix selectCellAt:0 :0];
		else [fishTypeMatrix selectCellAt:0 :1];

		i = 0;
		ptr = NXGetDefaultValue([NXApp appName], "AquariumFxType");
  		if (ptr)  sscanf (ptr, "%d", &i);
		[fxMatrix selectCellAt:i :0];
			
		i = 3;
		ptr = NXGetDefaultValue([NXApp appName], "AquariumDissolveFactor");
  		if (ptr)  sscanf (ptr, "%d", &i);
		x=i;
		[dissolveSlider setFloatValue: x];
		sprintf(str, "%d", i);
		[dissolveForm setStringValue: str];	
		
	}
	
	[NXGetNamedObject("Preferences", self) makeKeyAndOrderFront:self];
	
	return self;
}

- cancelPref:sender;
{
	[[colorWell window] orderOut:self];
	prefLoaded = FALSE;	
	return self;
}

-setPref:sender
{
    float r,g,b;
    char str[100];
	
	NXWriteDefault([NXApp appName], "AquariumFishNumber", 
	[fishNumberForm stringValueAt:0]);

	NXWriteDefault([NXApp appName], "AquariumFishSpeed", 
	[fishSpeedForm stringValueAt:0]);

    NXConvertColorToRGB([colorWell color], &r, &g, &b);
    sprintf(str, "%5.3f %5.3f %5.3f", r, g, b );
	NXWriteDefault([NXApp appName], "AquariumColor", str);

	if([fishTypeMatrix selectedCol] == 0)
		NXWriteDefault([NXApp appName], "AquariumFishType", "salt")	;		
	else
		NXWriteDefault([NXApp appName], "AquariumFishType", "fresh");			
	
	sprintf(str,"%d",[fxMatrix selectedRow]);
	NXWriteDefault([NXApp appName], "AquariumFxType", str);

	NXWriteDefault([NXApp appName], "AquariumDissolveFactor", 
	[dissolveForm stringValueAt:0]);
		
	[self cancelPref:self];
	restart_flag = YES;
	return self;
}
	
				
@end
