/*	You may freely copy, distribute and reuse the code in this example.
 *	NeXT disclaims any warranty of any kind, expressed or implied, as to
 *	its fitness for any particular use.
 */

#import "CallClient.h"

@implementation CallClient:Object

/* 
 * Purpose: Initializes the CallClient
 *
 * Inherited from Object, overridden by CallClient.
 * Connects the client, through the callProvider proxy, 
 * to the CallServer in the YourCallServer application.  
 */

- init
{
    [super init];
    callProvider = [NXConnection connectToName:"CallDataServer"];
    return self;
}


- awakeFromNib
{
  [customerForm selectText:self];
  [[customerForm window] makeKeyAndOrderFront:self];
  return self;
}


/*
 * Purpose: Retrieves a call from the database.  
 *
 * Takes the customer name currently displayed in customerFrom. 
 * Requests that the server -- through the proxy -- find a CallData 
 * conformant object for the name.  
 * Displays data from the CallData conformant object in the call form
 * with the name field selected.  
 * If no such record is found, displays an attention panel.  
 * If no name is entered, also displays an attention panel.  
 */

- retrieveCall:sender
{
    const char *fetchName;
    id <CallData> fetchRecord = nil;

    fetchName = [customerForm stringValueAt:0]; 
    if (fetchName && strlen(fetchName)) {
	fetchRecord = [callProvider lookupCall:fetchName];
	if (fetchRecord) {
	    [customerForm setStringValue:[fetchRecord street] at:1]; 
	    [customerForm setStringValue:[fetchRecord city] at:2]; 
	    [customerForm setStringValue:[fetchRecord state] at:3]; 
	    [customerForm setStringValue:[fetchRecord phone] at:4]; 
	    [questionText setStringValue:[fetchRecord question]];
	    [answerText setStringValue:[fetchRecord answer]];
	    [customerForm selectText:self];
	}
	else {
	    NXRunAlertPanel("Search Failed", "Customer %s not found", 
		NULL, NULL, NULL, fetchName);
	}
    }
    else {
	NXRunAlertPanel("Search Failed", "Please enter a customer name", 
	    NULL, NULL, NULL);
    }
    return self;
}


/*
 * Purpose: Saves the information in the form to the database
 *
 * Reads the name from the form and tests whether it contains a valid string. 
 * If the name string exists, requests a new CallData conformant object 
 * from the server through the proxy.  
 * Reads data from the Call Information form, puts it in the CallData 
 * conformant object, and requests the server store that object in the 
 * database.
 */

- saveCall:sender
{
    const char *formName = NULL;
    id <CallData> newRecord;

    formName = [customerForm stringValueAt:0];
    if (formName && strlen(formName)){
	newRecord = [callProvider newRecord];
	[newRecord setName:[customerForm stringValueAt:0]];
	[newRecord setStreet:[customerForm stringValueAt:1]];
	[newRecord setCity:[customerForm stringValueAt:2]];
	[newRecord setState:[customerForm stringValueAt:3]];
	[newRecord setPhone:[customerForm stringValueAt:4]];
	[newRecord setQuestion:[questionText stringValue]];
 	[newRecord setAnswer:[answerText stringValue]];
	[callProvider storeCall:newRecord]; 
    }
    return self;
}


- clearForm:sender;
{
    [questionText setStringValue:NULL];
    [answerText setStringValue:NULL];
    [customerForm setStringValue:NULL at:0];
    [customerForm setStringValue:NULL at:1];
    [customerForm setStringValue:NULL at:2];
    [customerForm setStringValue:NULL at:3];
    [customerForm setStringValue:NULL at:4];
    [customerForm selectText:self];
    return self;
}


- showInfoPanel:sender
{
    if (!infoPanel)
        [NXApp loadNibSection:"InfoPanel.nib" owner:self];
    [infoPanel makeKeyAndOrderFront:self];
    return self;
}


- free
{
    return [super free];
}

@end
