#import <appkit/appkit.h>
#import "PrefController.h"
#import "IcaoDefinitions.h"
#import "IcaoObjects.h"

@implementation PrefController

- awakeFromNib
{
	[self readDefaults];
	[self setPrefToView:[generalView contentView]];
	return self;
}

- setPref:sender
{
	id newView = nil;
	
	switch ([[sender selectedCell] tag])
	{
		case 0:
			newView = [generalView contentView];
			break;
		case 1:
			newView = [startupView contentView];
			break;
		case 2:
			newView = [mapFileView contentView];
			break;
		case 3:
			newView = [toolsView contentView];
			break;
		case 4:
			newView = [colorsView contentView];
			break;
	}
	[self setPrefToView:newView];
	return self;
}


- setDefaultMapfile:sender
{
	OpenPanel *selectPanel;
	char      *types[]={"iwp","worldpart",NULL};
	char      *fileName = NULL, *fileDir = NULL;

	selectPanel = [OpenPanel new];
	[selectPanel setTitle:NXLS("Select worldpart for startup")];
	[selectPanel setPrompt:NXLS("File:")];
	[selectPanel chooseDirectories:NO];

	// length is always 4 chars and up if there is a file
	if (strlen([startupFile stringValue]) > 4)
		fileDir = strdup((char *)[startupFile stringValue]);
	if (fileDir)
		fileName = strdup(strrchr(fileDir, '/')+1);
	if ([selectPanel runModalForDirectory:fileDir
			file:fileName
			types:types] == NX_OKTAG)
	{
		if (access([selectPanel filename],R_OK))
		{
			NXRunAlertPanel(NXLS("Alert"),
					NXLS("Couldn't open %s, please check permissons."),
					NXLS("OK"),NULL,NULL,[selectPanel filename]);
			return NULL;
		}
		else
		{
			[startupFile setStringValue:[selectPanel filename]];
			[self takeDefaultWorldpartStringFrom:startupFile];
		}
	}
	else
		return NULL;
	return self;

}

- setPrefToView:theView
{
	NXRect boxRect, viewRect;
	
	[multiView getFrame:&boxRect];
	[theView getFrame:&viewRect];
	
	[multiView setContentView:theView];
	[multiView display];
	
	return self;
}


/*
 *  Setting and getting defaults
 */

- readDefaults
{
	const char		*ptr;
	int						val;
	
	ptr = NXGetDefaultValue(APPNAME, IPD_ONSTARTUP);
	if (ptr)
	{
		val = atoi(ptr);
		[onStartup selectCellWithTag:val];
	};
	ptr = NXGetDefaultValue(APPNAME, IPD_STARTUPFILE);
	if (ptr)
		[startupFile setStringValue:ptr];;
	ptr = NXGetDefaultValue(APPNAME, IPD_AUTOLAUNCH);
	if (ptr)
	{
		val = atoi(ptr);
		[hideOnAutoLaunch setState:val];
	};
		
	ptr = NXGetDefaultValue(APPNAME, IPD_PROJECTIONTYPE);
	if (ptr)
	{
		val = atoi(ptr);
		[projectionTypeDefault selectCellWithTag:val];
	};
	ptr = NXGetDefaultValue(APPNAME, IPD_ONLOADING);
	if (ptr)
	{
		val = atoi(ptr);
		[onLoading selectCellWithTag:val];
	};
			
	ptr = NXGetDefaultValue(APPNAME, IPD_OPTIMIZEMONO);
	if (ptr)
	{
		val = atoi(ptr);
		[optimizeMono setState:val];
	};
	
	ptr = NXGetDefaultValue(APPNAME, IPD_RUBBERUNIT);
	if (ptr)
	{
		val = atoi(ptr);
		[[[rubberUnit target] itemList] selectCellWithTag:val];
		[rubberUnit 
				setTitle:[[[[rubberUnit target] itemList] selectedCell] title]];
	}
	
	ptr = NXGetDefaultValue(APPNAME, IPD_AUTOUNIT);
	if (ptr)
	{
		val = atoi(ptr);
		[[[autoUnit target] itemList] selectCellWithTag:val];
		[autoUnit setTitle:[[[[autoUnit target] itemList] selectedCell] title]];
	}
	
	ptr = NXGetDefaultValue(APPNAME, IPD_RUNWAYUNIT);
	if (ptr)
	{
		val = atoi(ptr);
		[[[runwayUnit target] itemList] selectCellWithTag:val];
		[runwayUnit 
				setTitle:[[[[runwayUnit target] itemList] selectedCell] title]];
	}
	
	ptr = NXGetDefaultValue(APPNAME, IPD_ELEVATIONUNIT);
	if (ptr)
	{
		val = atoi(ptr);
		[[[elevationUnit target] itemList] selectCellWithTag:val];
		[elevationUnit 
				setTitle:[[[[elevationUnit target] itemList] selectedCell] title]];
	}
	
	ptr = NXGetDefaultValue(APPNAME, IPD_RANGEUNIT);
	if (ptr)
	{
		val = atoi(ptr);
		[[[rangeUnit target] itemList] selectCellWithTag:val];
		[rangeUnit 
				setTitle:[[[[rangeUnit target] itemList] selectedCell] title]];
	}
	

		
	return self;
}


- takeOnStartupValueFrom:sender
{
	int				val;
	char			strg[80];
	
	val = [[sender selectedCell] tag];
	sprintf(strg, "%i", val);
	NXWriteDefault(APPNAME, IPD_ONSTARTUP, strg);
	return self;
}
	
- takeDefaultWorldpartStringFrom:sender
{
	NXWriteDefault(APPNAME, IPD_STARTUPFILE, [sender stringValue]);
	return self;
}

- takeHideOnAutoLaunchValueFrom:sender
{
	int				val;
	char			strg[80];
	
	val = [sender state];
	sprintf(strg, "%i", val);
	NXWriteDefault(APPNAME, IPD_AUTOLAUNCH, strg);
	return self;
}

- takeProjectionDefaultValueFrom:sender
{
	int				val;
	char			strg[80];
	
	val = [[sender selectedCell] tag];
	sprintf(strg, "%i", val);
	NXWriteDefault(APPNAME, IPD_PROJECTIONTYPE, strg);
	return self;
}

- takeOnLoadingValueFrom:sender
{
	int				val;
	char			strg[80];
	
	val = [[sender selectedCell] tag];
	sprintf(strg, "%i", val);
	NXWriteDefault(APPNAME, IPD_ONLOADING, strg);
	return self;
}

- takeOptimizeMonoValueFrom:sender
{
	int				val;
	char			strg[80];
	
	val = [sender state];
	sprintf(strg, "%i", val);
	NXWriteDefault(APPNAME, IPD_OPTIMIZEMONO, strg);
	return self;
}

- takeRubberUnitFrom:sender
{
	char strg[10];
	
	sprintf(strg, "%i", [[sender selectedCell] tag]);
	NXWriteDefault(APPNAME, IPD_RUBBERUNIT, strg);
	return self;
}

- takeAutoUnitFrom:sender
{
	char strg[10];
	
	sprintf(strg, "%i", [[sender selectedCell] tag]);
	NXWriteDefault(APPNAME, IPD_AUTOUNIT, strg);
	return self;
}
- takeRunwayUnitValueFrom:sender
{
	char strg[10];
	
	sprintf(strg, "%i", [[sender selectedCell] tag]);
	NXWriteDefault(APPNAME, IPD_RUNWAYUNIT, strg);
	return self;
}

- takeElevationUnitValueFrom:sender
{
	char strg[10];
	
	sprintf(strg, "%i", [[sender selectedCell] tag]);
	NXWriteDefault(APPNAME, IPD_ELEVATIONUNIT, strg);
	return self;
}

- takeRangeUnitValueFrom:sender
{
	char strg[10];
	
	sprintf(strg, "%i", [[sender selectedCell] tag]);
	NXWriteDefault(APPNAME, IPD_RANGEUNIT, strg);
	return self;
}


@end

@implementation PrefController(NXNibNotification)


@end

