#import "OpenTalk.h"
#import "NetTalkThinker.h"
#import <appkit/appkit.h>

#define NX_RESCANTAG	2

@implementation OpenTalk

+ new
{
	static    OpenTalk *instance = nil;

	if (instance == nil)
	{
		id foo = [[Loader alloc] init];
		if ([NXApp loadNibSection:"OpenTalk.nib" owner:foo withNames:NO] == nil)
			return nil;
		instance = [foo thePanel];
		[foo free];
	}
	
	[instance center];
	return instance;
}

- awakeFromNib
{
	const char *value;

	modePopup = [modeButton target];
	while([modePopup count])
		[modePopup removeItemAt:0];
	[modeButton setEnabled:NO];
	[modeButton setTitle:""];

	value = NXGetDefaultValue(APPNAME, NTD_DEFAULTTOPIC);
	[form setStringValue:value at:0];

	[browser setAction:@selector(click:)];
	[browser setDoubleAction:@selector(ok:)];
	return self;
}

- setupPopupFrom:(List *)classList
{
	int    i;
	
	for (i = 0; i < [classList count]; i++)
	{
		[modePopup insertItem:
			[[[classList objectAt:i] principalClass] name] at:i];
	}
	[modeButton setTitle:[[[modePopup itemList] cellAt:0:0] title]];
	[modeButton setEnabled:(i > 0)];
	return self;
}

- setServerName:(const char *)aServerName
{
	[serverNameField setStringValue:aServerName];
	return self;
}

- cancel:sender
{
	[NXApp stopModal:NX_CANCELTAG];
	return self;
}

- ok:sender
{
	if (strcmp([form stringValueAt:0], "") == 0)
	{
		NXRunLocalizedAlertPanel(NULL
			, "Attention", "TopicRequired", "OK", NULL, NULL);
		return nil;
	}
	clientNumber = [[browser matrixInColumn:0] selectedRow];
	[okButton setEnabled:(clientNumber >= 0)];
	strcpy(clientName, [[browser selectedCell] stringValue]);
	[NXApp stopModal:NX_OKTAG];
	return self;
}

- rescan:sender
{
	BOOL    enabled = [[browser selectedCell] isEnabled];
	
	[browser reloadColumn:0];
	[okButton setEnabled:enabled];
	return self;
}

- click:sender
{
	const char *_clientname;
	clientNumber = [[browser matrixInColumn:0] selectedRow];
	[okButton setEnabled:(clientNumber >= 0)];
	_clientname = [[browser selectedCell] stringValue];
	if (_clientname)
		strcpy(clientName, _clientname);
	else
		strcpy(clientName, "");
	return self;
}

- (int)clientNumber
{
	return clientNumber;
}

- (const char*)clientName
{
	return clientName;
}

- (const char*)topic
{
	return [form stringValueAt:0];
}

- setClassName:(const char *)aClassName
{
	[modeButton setTitle:aClassName];
	return self;
}

- (const char *)className
{
	return [modeButton title];
}

- (int)runModal
{
	int value;
	
	[browser reloadColumn:0];
	clientNumber = [[browser matrixInColumn:0] selectedRow];
	[okButton setEnabled:([[browser selectedCell] isEnabled])];
	value = [NXApp runModalFor:self];
	[self orderOut:nil];
	
	return value;
}

@end

@implementation OpenTalk(BrowserDelegate)

- (int)browser:sender getNumRowsInColumn:(int)column
{
	return [delegate getClientCount];
}

- browser:sender loadCell:cell atRow:(int)row inColumn:(int)column
{
	char     title[1024];

	[cell setEnabled:[delegate getClientName:title at:row]];
	[cell setStringValue:title];
	[cell setLeaf:YES];
	[cell setLoaded:YES];
	
	return self;
}

@end

@implementation Loader

- thePanel
{
	return thePanel;
}

@end