/*
 * SplatDefaults
 * description: a subclass of EKDefaultsManager for managing Splat prefs
 * history:
 *	5/20/93 [Erik Kay] - created
 *	6/3/93 [Erik Kay] - added SquareSize
 */

#import "EKApplication.h"
#import "EKSoundManager.h"
#import "Board.h"
#import "SplatView.h"
#import "SplatDefaults.h"
#import <pwd.h>

// all of the defaults for Splat
#define MOVE_ANIMATION	"DoMoveAnimation"
#define PLAY_SOUNDS	"PlaySounds"
#define SQUARE_SIZE	"SquareSize"
#define BOARD_FILE	"BoardFile"
#define PLAYER_NAME	"PlayerName"

@implementation SplatDefaults

- init
{
    struct passwd *pwd;
    char *login, path[MAXPATHLEN];
    const char *tmp;
    
    [super init];
    
    tmp = [[NXBundle mainBundle] directory];
    sprintf(path,"%s/boards",tmp);
    boardBundle = [[NXBundle alloc] initForDirectory:path];
    
    // add all of the defaults to the hash table
    [self addDefault:MOVE_ANIMATION];
    [self addDefault:PLAY_SOUNDS];
    [self addDefault:SQUARE_SIZE];
    [self addDefault:BOARD_FILE];
    [self addDefault:PLAYER_NAME];
    
    // give the defaults initial values (takes the place of a defaults vector)
    doMoveAnimation = YES;
    playSounds = YES;
    squareSize = 52;

    boardFile = NXCopyStringBuffer("Starter");
    [self computeBoardPath];
    
    login = getlogin();
    pwd = getpwnam(login);
    playerName = NXCopyStringBuffer(pwd->pw_gecos);
    
    return self;
}

// load the defaults, then do a few actions based on those results
- loadDefaults
{
    [super loadDefaults];
    [[NXApp soundManager] setPlaySounds:playSounds];
    return self;
}

// load and display the preferences panel... override so that we can set
// some of the things in the panel up right
- loadPreferencesPanel
{
    if (!prefsPanel) {
	[NXApp loadNibSection:"Preferences.nib" owner:self withNames:NO];
	[moveAnimationSwitch setIntValue:(int)doMoveAnimation];
	[playSoundsSwitch setIntValue:(int)playSounds];
	[playerNameField setStringValue:playerName];
	[boardView setScalable:YES];
	[boardView setBoard:[Board newFromFile:boardPath]];
    }
    return self;
}

// set a default value and take action based on what default was set
//! this is kind of a pain to extend
- setDefault:(const char *)key value:(const char *)val
{
    int tmpSize;
    
    [super setDefault:key value:val];
    if (!val)
	return self;
    if (!strcmp(key,MOVE_ANIMATION)) {
	if ((val[0] == 'Y') || (val[0] == 'y')) {
	    doMoveAnimation = YES;
	} else doMoveAnimation = NO;
	[moveAnimationSwitch setIntValue:(int)doMoveAnimation];
    } else if (!strcmp(key,PLAY_SOUNDS)) {
	if ((val[0] == 'Y') || (val[0] == 'y')) {
	    playSounds = YES;
	} else playSounds = NO;
	[playSoundsSwitch setIntValue:(int)playSounds];
    } else if (!strcmp(key,SQUARE_SIZE)) {
	tmpSize = atoi(val);
	if (tmpSize > 0)
	    squareSize = tmpSize;
    } else if (!strcmp(key,PLAYER_NAME)) {
	free(playerName);
	playerName = NXCopyStringBuffer(val);
	[playerNameField setStringValue:val];
    } else if (!strcmp(key,BOARD_FILE)) {
	id oldBoard, newBoard;
	char *tmp;
	
	tmp = boardFile;
	boardFile = NXCopyStringBuffer(val);
	[self computeBoardPath];
	oldBoard = [boardView board];
	newBoard = [Board newFromFile:boardPath];
	if (!newBoard) {
	    free(boardFile);
	    boardFile = tmp;
	    [self computeBoardPath];
	} else {
	    [boardView setScalable:YES];
	    [boardView setBoard:newBoard];
	    [oldBoard free];
	}
	[super setDefault:key value:boardFile];
    }
    return self;
}

// turns move animation on or off
- setMoveAnimation:sender
{
    if ([sender intValue]) {
	doMoveAnimation = YES;
	[self setDefault:MOVE_ANIMATION value:"Yes"];
    } else {
    	doMoveAnimation = NO;
	[self setDefault:MOVE_ANIMATION value:"No"];
    }
    return self;
}

- (BOOL)doMoveAnimation
{
    return doMoveAnimation;
}

// enables or disables sound playing
- setSoundEnabled:sender
{
    if ([sender intValue]) {
	playSounds = YES;
	[self setDefault:PLAY_SOUNDS value:"Yes"];
    } else {
    	playSounds = NO;
	[self setDefault:PLAY_SOUNDS value:"No"];
    }
    [[NXApp soundManager] setPlaySounds:playSounds];
    return self;
}

- (BOOL)playSounds
{
    return playSounds;
}

// the default square size
- setSquareSize:(int)val
{
    static char buf[10];
    sprintf(buf,"%d",val);
    [self setDefault:SQUARE_SIZE value:buf];
    squareSize = val;
    return self;
}

- (int)squareSize
{
    return squareSize;
}

- setDefaultPlayerName:sender
{
    [self setDefault:PLAYER_NAME value:[sender stringValue]];
    return self;
}

- (char *)defaultPlayerName
{
    return playerName;
}

- computeBoardPath
{
    const char *path;
    char *tmp;
    
    path = [boardBundle directory];
    if (strlen(boardFile) >= strlen(path)) {
	if (!strncmp(path,boardFile,strlen(path))) {
	    strcpy(boardPath,boardFile);
	    tmp = boardFile;
	    *(rindex(tmp,'.')) = '\0';
	    boardFile = rindex(tmp,'/') + 1;
	    boardFile = NXCopyStringBuffer(boardFile);
	    free(tmp);
	} else strcpy(boardPath,boardFile);
    } else if (!rindex(boardFile,'/')) {
	[boardBundle getPath:boardPath forResource:boardFile 
					    ofType:BOARD_EXTENSION];
    } else strcpy(boardPath,boardFile);
    return self;
}

- setBoardFile:sender
{
    static id openPanel = nil;
    const char *theType = BOARD_EXTENSION, *filename;
    char const *fileTypes[2] = {0,0};
    static char boardDir[MAXPATHLEN] = {'\0'};
    int opr;
    char *ptr;
    
    if (!boardDir[0])
	strcpy(boardDir,[boardBundle directory]);
    if (!openPanel)
        openPanel = [OpenPanel new];
    fileTypes[0] = theType;
    [NXApp setAutoupdate:NO];
    opr = [openPanel runModalForDirectory:boardDir 
			file:NULL types:fileTypes];
    if (opr) {
	filename = [openPanel filename];
	[self setDefault:BOARD_FILE value:filename];
	strcpy(boardDir,filename);
	ptr = rindex(boardDir,'/');
	if (ptr)
	    *ptr = '\0';
    } else {
	[NXApp setAutoupdate:YES];
	return self;
    }
    
    return self;
}

- (char *)boardPath
{
    return boardPath;
}

@end
