//** Craig Laurent
#import "InfoController.h"
#import "Converse.h"		//** for prefDataMaster messages
#import "Preferences.h"
#import "User.h"

@implementation InfoController

- initWithPrefMaster:(Converse*)newMaster;
{
	if (self = [super init]) {
		infoPanel = nil;
		preferencesPanel = nil;
		[self setPrefDataMaster:newMaster];
		return self;
	}
	return nil;
}

- init
{
	return [self initWithPrefMaster:nil];
}

- (void)dealloc
{
	if (infoPanel)
		[infoPanel free];
	if (preferencesPanel)
		[preferencesPanel free];
	[super dealloc];
}

- awakeFromNib
{
	[self swapMessage:nil];
	[self loadPref:nil];
	return self;
}


/* showInfoPanel - load and display the info panel */
- showInfoPanel:sender
{
	if (!infoPanel)
		[NXApp loadNibSection:"InfoPanel.nib" owner:self withNames:YES fromZone:(NXZone*)[self zone]];
	[infoPanel makeKeyAndOrderFront:self];

    return self;
}

/* showPreferences - load and display the preferences panel */
- showPreferences:sender
{
	if (!preferencesPanel)
		[NXApp loadNibSection:"PreferencesPanel.nib" owner:self withNames:YES fromZone:(NXZone*)[self zone]];
	[preferencesPanel makeKeyAndOrderFront:self];

    return self;
}

- (Converse*)prefDataMaster
{	return prefDataMaster; }
- (void)setPrefDataMaster:(Converse*)newMaster
{
	prefDataMaster = newMaster;
}


/* swapMessage: - change preference panel to show the Display information choices. */
- swapMessage:sender
{
	[swapBox setContentView:[swapMessageBox contentView]];
	[swapBox display];
	return self;
}

/* swapUser: - change preference panel to show the User information choices. */
- swapUser:sender
{
	[swapBox setContentView:[swapUserBox contentView]];
	[swapBox display];
	return self;
}

/* changeSoundFile: - change file to load sound from */
- changeSoundFile:sender
{
	OpenPanel *oPanel = [OpenPanel new];
	const char *types[] = {"snd", NULL};
	const char *tempName;

	//** get file to open
	if ([oPanel runModalForDirectory:"~/Library/Sounds/" file:NULL types:types]) {
		tempName = [oPanel filename];
		if (tempName) {
			[alertSoundField setStringValue:tempName];
		}
	}

	return self;
}

/* changeAddressFile: - change file to load address from */
- changeAddressFile:sender
{
	NSString	*currentFile = nil;
	NSString	*currentDirectory = nil;
	NSString	*currentFilename = nil;
	OpenPanel	*oPanel = [OpenPanel new];
	int	openStatus;
	const char	*types[] = {"cvs", NULL};
	const char	*tempName;
//** change types in Converse as well!!

	//** if filename exists, it should be complete: dir/file/ext
	currentFile = [prefDataMaster filename];
	currentDirectory = [currentFile stringByDeletingLastPathComponent];
	currentFilename = [currentFile lastPathComponent];

	//** get file to open
	if (currentFile)
		openStatus = [oPanel runModalForDirectory:[currentDirectory cString] file:[currentFilename cString] types:types];
	else
		openStatus = [oPanel runModalForTypes:types];

	if (openStatus) {
		tempName = [oPanel filename];
		if (tempName) {
			[defaultAddressField setStringValue:tempName];
		}
	}

	return self;
}

- emptyAddressFile:sender
{
	[defaultAddressField setStringValue:NULL];
	return self;
}


/* updateNickname: - update nickname to reflect display prefs. */
- updateNickname:sender
{
	NSString	*nickname;
	nickname = [[prefDataMaster userInfo] userNicknameWithID:[userIDCheck state] andName:[realNameCheck state] andMachine:[machineNameCheck state]];
	[nicknameField setStringValue:[nickname cString]];

	return self;
}


/* changeTextEnable: - change the editable status for the User information.  Reverse from current.  eg if editable make not-editable. */
- changeTextEnable:sender
{
	BOOL	editStatus;

//** set editStatus to opposite of what it is currently.
//**  use this value to set status to it's opposite.
	editStatus = ! ([userIDField isEditable]);

//** change editability
	[userIDField setEditable:editStatus];
	[realNameField setEditable:editStatus];
	[machineNameField setEditable:editStatus];

//** if setting to editable, highlight UserId
//** else, in order to remove cursor from uneditable fields,
//**  must disable and then re-enable the fields.
	if (editStatus) {
		[userIDField selectText:self];
	}
	else {
		[userIDField setEnabled:NO];
		[realNameField setEnabled:NO];
		[machineNameField setEnabled:NO];
		[userIDField setEnabled:YES];
		[realNameField setEnabled:YES];
		[machineNameField setEnabled:YES];
	}

	return self;
}

/* loadPref: - display preference panel information from the Preference and User Objects. */
- loadPref:sender
{
//** load Preferences to the screen
	id	userData;
	id	prefData;

	userData = [[self prefDataMaster] userInfo];
	prefData = [[self prefDataMaster] prefs];

//** load MessageBox
	[alertAudibleCheck setState:[prefData alertAudio]];
	[alertIconCheck setState:[prefData alertIcon]];
	[alertWindowCheck setState:[prefData alertWindow]];
	[alertApplicationCheck setState:[prefData alertApplication]];

	[alertSoundField setStringValue:[[prefData soundFile] cString]];
	[defaultAddressField setStringValue:[[prefData addressFile] cString]];

//** load UserBox
	[userIDField setStringValue:[[userData userID] cString]];
	[realNameField setStringValue:[[userData userName] cString]];
	[machineNameField setStringValue:[[userData userMachine] cString]];

	[userIDCheck setState:[prefData discloseID]];
	[realNameCheck setState:[prefData discloseName]];
	[machineNameCheck setState:[prefData discloseMachine]];
	[self updateNickname:nil];

	return self;
}

/* savePref: - save preference panel information from the screen to the Preference and User Objects. */
- savePref:sender
{
//** save Prefs from the screen
	id	userData;
	id	prefData;

	userData = [prefDataMaster userInfo];
	prefData = [prefDataMaster prefs];

//** save MessageBox
	[prefData setAlertAudio:[alertAudibleCheck state]];
	[prefData setAlertIcon:[alertIconCheck state]];
	[prefData setAlertWindow:[alertWindowCheck state]];
	[prefData setAlertApplication:[alertApplicationCheck state]];

	//** set soundFile if one entered
	if ([alertSoundField stringValue])
		[prefData setSoundFile:[NSString stringWithCString:[alertSoundField stringValue]]];
	else
		[prefData setSoundFile:nil];

	//** set addressFile if one entered
	if ([defaultAddressField stringValue])
		[prefData setAddressFile:[NSString stringWithCString:[defaultAddressField stringValue]]];
	else
		[prefData setAddressFile:nil];

//** save UserBox
	[userData setUserID:[NSString stringWithCString:[userIDField stringValue]]];
	[userData setUserName:[NSString stringWithCString:[realNameField stringValue]]];
	[userData setUserMachine:[NSString stringWithCString:[machineNameField stringValue]]];

	[prefData setDiscloseID:[userIDCheck state]];
	[prefData setDiscloseName:[realNameCheck state]];
	[prefData setDiscloseMachine:[machineNameCheck state]];

	[prefData savePreferences];

	//** must set the sound in the controller
	[prefDataMaster setMsgSoundWithName:[[prefData soundFile] cString]];
	[prefDataMaster refreshAddress];	//** update address list
	return self;
}

@end
