// DefaultManager.m
// Part of Risk by Mike Ferris

// adding new defaults requires adding define statements,
// changing the default vector, changing setAction,
// changing revertAction, and adding any new functionality to the panel

#import "DefaultManager.h"
#import "LanguageApp.h"
#import "LineView.h"
#import <appkit/NXColorWell.h>
#import <appkit/TextField.h>
#import <appkit/Button.h>
#import <appkit/Slider.h>
#import <appkit/color.h>
#import <defaults/defaults.h>
#import <stdlib.h>
#import <strings.h>
#import <stdio.h>

#define APPNAME "RiskByMikeFerris"
// add default aliases here
#define BORDERWIDTH "countryBorderWidth"
#define BORDERCOLORS "countryBorderColors"
#define PLAYERNAMES "playerNames"
#define PLAYERCOLORS "playerColors"
#define SETUPOPTIONS "setupOptions"
#define STATUSCARDSETSOPTION "statusCardSetsOption"
	#define STATUS_SHOWSETS "Show"
	#define STATUS_HIDESETS "Hide"

@implementation DefaultManager

// add defaults here
static NXDefaultsVector RiskDefaults = {
	// the country border
	{BORDERWIDTH, "2.0"},
	// two sets of red, green, blue first for regular color, then selected
	{BORDERCOLORS, "0.0, 0.0, 0.0, 1.0, 1.0, 1.0"},
	// six comma separated strings
	{PLAYERNAMES, "Dopey, Sneezy, Grumpy, Doc, Bashful, Sleepy"},
	// six sets of red green blue one for each of the players
	{PLAYERCOLORS, "0.9, 0.9, 0.9,"
				   "0.8, 0.8, 0.8,"
				   "0.66, 0.66, 0.66,"
				   "0.33, 0.33, 0.33,"
				   "0.15, 0.15, 0.15,"
				   "0.0, 0.0, 0.0,"},
	// the following numbers represent Initial Country Distribution,
	// Initial Army Placement, Card Set Redemption, and the Fortify Rule
	{SETUPOPTIONS, "0, 1, 0, 2"},
	{STATUSCARDSETSOPTION, STATUS_SHOWSETS},
	{NULL}
};

- init
{
	[super init];
	[NXApp loadNibSection:"DefaultManager.nib" owner:self withNames:NO];
	NXRegisterDefaults(APPNAME, RiskDefaults);
	[self revertAction:self];
	return self;
}

- showPrefPanel:sender
{
	[prefPanel makeKeyAndOrderFront:self];
	return self;
}

- revertAction:sender
{
	const char *tempStr;
	float x[18];
	NXColor tempColor;
	int trash;
	
	// the border width
	tempStr = NXGetDefaultValue(APPNAME, BORDERWIDTH);
	trash = sscanf(tempStr, "%f", &(x[0]));
	[borderWidthSlider setFloatValue:x[0]];
	[borderWidthTextField setFloatValue:x[0]];
	[borderWidthLineView setLineWidth:x[0]];
	
	tempStr = NXGetDefaultValue(APPNAME, BORDERCOLORS);
	trash = sscanf(tempStr, "%f, %f, %f, %f, %f, %f", 
					&(x[0]), &(x[1]), &(x[2]), 
					&(x[3]), &(x[4]), &(x[5]));
	tempColor = NXConvertRGBToColor(x[0], x[1], x[2]);
	[borderRegularWell setColor:tempColor];
	tempColor = NXConvertRGBToColor(x[3], x[4], x[5]);
	[borderSelectedWell setColor:tempColor];

	tempStr = NXGetDefaultValue(APPNAME, STATUSCARDSETSOPTION);
	if (strcmp(tempStr, STATUS_SHOWSETS)==0)  {
		[statusShowSetsCheckbox setState:1];
	}  else  {
		[statusShowSetsCheckbox setState:0];
	}
	[theMapView display];
	
    return self;
}

- setAction:sender
{
	float r, g, b;
	const char *tempStr;
	char str[203]="", buf[100];
	NXColor tempColor;
	
	tempStr = [borderWidthTextField stringValue];
	NXWriteDefault(APPNAME, BORDERWIDTH, tempStr);
	
	tempColor = [borderRegularWell color];
	NXConvertColorToRGB(tempColor, &r, &g, &b);
	sprintf(buf, "%f, %f, %f", r, g, b);
	strcat(str, buf);
	strcat(str, ", ");
	tempColor = [borderSelectedWell color];
	NXConvertColorToRGB(tempColor, &r, &g, &b);
	sprintf(buf, "%f, %f, %f", r, g, b);
	strcat(str, buf);
	NXWriteDefault(APPNAME, BORDERCOLORS, str);
	
	if ([statusShowSetsCheckbox state]==0)  {
		NXWriteDefault(APPNAME, STATUSCARDSETSOPTION, STATUS_HIDESETS);
	}  else  {
		NXWriteDefault(APPNAME, STATUSCARDSETSOPTION, STATUS_SHOWSETS);
	}
	
    return self;
}

- borderWidthAction:sender
{
	float newlw = [sender floatValue];
	
	if (sender == borderWidthSlider)  {
		[borderWidthTextField setFloatValue:newlw];
		[borderWidthLineView setLineWidth:newlw];
		
	}  else  {
		[borderWidthSlider setFloatValue:newlw];
		[borderWidthLineView setLineWidth:newlw];
		
	}
	[theMapView display];
    return self;
}

- borderColorAction:sender
{
	[theMapView display];
    return self;
}

- statusCardSetsAction:sender
{
	[theControlPanel display];
    return self;
}

- (float)borderWidth
{
	return [borderWidthSlider floatValue];
}

- (NXColor)borderRegularColor
{
	return [borderRegularWell color];
}

- (NXColor)borderSelectedColor
{
	return [borderSelectedWell color];
}

- (BOOL)statusShowSets
{
	return (([statusShowSetsCheckbox state]==0)?NO:YES);
}

// for Game Setup preferences
- (const char *)playerColors
{
	return NXGetDefaultValue(APPNAME, PLAYERCOLORS);
}

- (const char *)playerNames
{
	return NXGetDefaultValue(APPNAME, PLAYERNAMES);
}

- (const char *)setupOptions
{
	return NXGetDefaultValue(APPNAME, SETUPOPTIONS);
}

- setPlayerColors:(char *)colorStr
{
	NXWriteDefault(APPNAME, PLAYERCOLORS, colorStr);
	return self;
}

- setPlayerNames:(char *)nameStr
{
	NXWriteDefault(APPNAME, PLAYERNAMES, nameStr);
	return self;
}

- setSetupOptions:(char *)optionStr
{
	NXWriteDefault(APPNAME, SETUPOPTIONS, optionStr);
	return self;
}

@end
