#import "NTPrefThinker.h"
#import "NetTalkThinker.h"
#import "OrderMatrix.h"
#import <appkit/appkit.h>


@implementation NTPrefThinker

- setupMatrixScroller
{
	NXRect	scrollRect, matrixRect;
	NXSize	interCellSpacing = {0.0, 0.0}, cellSize;
	
	[serverScroller setBorderType:NX_BEZEL];
	[serverScroller setVertScrollerRequired:YES];
	[serverScroller setHorizScrollerRequired:NO];
	
	[serverScroller getFrame:&scrollRect];
	[ScrollView getContentSize:&(matrixRect.size)
		forFrameSize:&(scrollRect.size)
		horizScroller:YES
		vertScroller:YES
		borderType:NX_BEZEL];
	
	serverMatrix = [[OrderMatrix alloc] initFrame:&matrixRect
		mode:NX_LISTMODE cellClass:[Cell class] numRows:0 numCols:1];
	[serverMatrix setIntercell:&interCellSpacing];
	[serverMatrix setTextDelegate:self];
	[self loadServer];
	[serverMatrix getCellSize:&cellSize];
	cellSize.width = NX_WIDTH(&matrixRect);
	[serverMatrix setCellSize:&cellSize];
	[serverMatrix sizeToCells];
	[serverMatrix setAutosizeCells:YES];
	[serverMatrix setAutoscroll:YES];
	
	[[serverScroller setDocView:serverMatrix] free];
	[[serverMatrix superview] setAutoresizeSubviews:YES];
	[serverMatrix setAutosizing:NX_WIDTHSIZABLE];
	return self;
}

- awakeFromNib
{
	const char *value;
	
	value = NXGetDefaultValue(APPNAME, NTD_SOUNDNAME);
	if (strrchr(value, '/'))
		[soundText setStringValue:strrchr(value, '/')+1];
	else
		[soundText setStringValue:value];
	[audibleSwitch setState:
		(strcmp(NXGetDefaultValue(APPNAME, NTD_MUTEALARM), NTD_NO) == 0)];

	value = NXGetDefaultValue(APPNAME, NTD_RINGSOUNDNAME);
	if (strrchr(value, '/'))
		[ringingText setStringValue:strrchr(value, '/')+1];
	else
		[ringingText setStringValue:value];
	[ringingAllowedSwitch setState:
		(strcmp(NXGetDefaultValue(APPNAME, NTD_RINGINGALLOWED), NTD_YES) == 0)];

	[self setupMatrixScroller];

	NXWriteDefault(APPNAME, NTD_USESELECTED, NTD_YES);

	value = NXGetDefaultValue(APPNAME, NTD_DEFAULTTOPIC);
	[defaultTopicField setStringValue:value];

	return self;
}

- free
{
	[self saveServer];
	return [super free];
}

- loadServer
{
	const char *token;
	char        value[8192];
	int         i;
	
	token = NXGetDefaultValue(APPNAME, NTD_SERVERLIST);
	if (token)
	{
		strcpy(value, token);
		token = strtok(value, " ");
	}
	else
		token = NULL;
	i = 0;
	while (token)
	{
		[serverMatrix addRow];
		[[serverMatrix cellAt:i :0] setStringValue:token];
		token = strtok(NULL, " ");
		i++;
	}
	return self;
}

- saveServer
{
	int    i = 0;
	char   value[8192] = "";
	
	while (i < [serverMatrix cellCount])
	{
		strcat(value, [[serverMatrix cellAt:i :0] stringValue]);
		i++;
		if (i < [serverMatrix cellCount])
			strcat(value, " ");
	}
	NXWriteDefault(APPNAME, NTD_SERVERLIST, value);
	return self;
}

- addServer:sender
{
	Cell   *newCell;
	[serverMatrix addRow];
	newCell = [serverMatrix cellAt:[serverMatrix cellCount]-1 :0];
	[newCell setStringValue:[serverTextField stringValue]];
	[[[serverMatrix sizeToCells]
		selectCellAt:[serverMatrix cellCount]-1 :0] display];
	[serverTextField selectText:nil];
	[self saveServer];
	return self;
}

- replaceServer:sender
{
	Cell   *oldCell;
	
	oldCell = [serverMatrix selectedCell];
	[oldCell setStringValue:[serverTextField stringValue]];
	[serverMatrix display];
	[serverTextField selectText:nil];
	[self saveServer];
	return self;
}

- setRequestSound:sender
{
	OpenPanel  *openPanel = [OpenPanel new];
	const char *type[2] = {"snd", NULL};
	char        filePath[MAXPATHLEN];
	
	strcpy(filePath, NXGetDefaultValue(APPNAME, NTD_SOUNDNAME));
	if (strrchr(filePath, '/'))
		*strrchr(filePath, '/') = 0;
	else
		strcpy(filePath, "/NextLibrary/Sounds");
	[openPanel allowMultipleFiles:NO];
	[openPanel chooseDirectories:NO];
	if ([openPanel runModalForDirectory:filePath file:[soundText stringValue] types:type])
	{
		strcpy(filePath, [openPanel filename]);
		NXWriteDefault(APPNAME, NTD_SOUNDNAME, [openPanel filename]);
		[soundText setStringValue:(strrchr(filePath, '/')+1)];
		[[[[Sound alloc] initFromSoundfile:filePath] play:nil] free];
	}
	return self;
}

- setAudible:sender
{
	if ([audibleSwitch state])
		NXWriteDefault(APPNAME, NTD_MUTEALARM, NTD_NO);
	else
		NXWriteDefault(APPNAME, NTD_MUTEALARM, NTD_YES);
	return self;
}

- setRingSound:sender
{
	OpenPanel  *openPanel = [OpenPanel new];
	const char *type[2] = {"snd", NULL};
	char        filePath[MAXPATHLEN];
	
	strcpy(filePath, NXGetDefaultValue(APPNAME, NTD_RINGSOUNDNAME));
	if (strrchr(filePath, '/'))
		*strrchr(filePath, '/') = 0;
	else
		strcpy(filePath, "/NextLibrary/Sounds");
	[openPanel allowMultipleFiles:NO];
	[openPanel chooseDirectories:NO];
	if ([openPanel runModalForDirectory:filePath file:[ringingText stringValue] types:type])
	{
		strcpy(filePath, [openPanel filename]);
		NXWriteDefault(APPNAME, NTD_RINGSOUNDNAME, [openPanel filename]);
		[ringingText setStringValue:(strrchr(filePath, '/')+1)];
		[[[[Sound alloc] initFromSoundfile:filePath] play:nil] free];
	}
	return self;
}

- setRingingFlag:sender
{
	if ([ringingAllowedSwitch state])
		NXWriteDefault(APPNAME, NTD_RINGINGALLOWED, NTD_YES);
	else
		NXWriteDefault(APPNAME, NTD_RINGINGALLOWED, NTD_NO);
	return self;
}

- setDefaultTopic:sender
{
	NXWriteDefault(APPNAME, NTD_DEFAULTTOPIC, [defaultTopicField stringValue]);
	return self;
}

@end

@implementation NTPrefThinker(TextDelegate)

- textDidChange:sender
{
	if (sender == serverMatrix)
		[self saveServer];
	return self;
}

- (BOOL)textWillChange:sender
{
	return NO;
}

- textDidGetKeys:textObject isEmpty:(BOOL)flag
{
	[addButton setEnabled:!flag];
	[replaceButton setEnabled:(!flag && [serverMatrix selectedCell])];
	return self;
}

@end
