//
//	Xanthus
//	Copyright (c) 1992-95 Bjorn Backlund.  
//	All rights reserved. 
//

#import <appkit/appkit.h>

#import "XQ_InterchangeCell.h"
#import "XQ_UserFunction.h"
#import "XQ_InterfaceObject.h"

#import "Questor_API_Demo4.h"

@implementation Questor_API_Demo4

- appDidInit:sender; 
{
	[self enableButtons];
	return self;
}
 
- enableButtons;
{
	BOOL isAlive = [theInterface isSheetAlive];
	
    	[createSheetButton setEnabled: !isAlive];
    	[connectSheetButton setEnabled: !isAlive];
	
    	[hideSheetButton setEnabled: isAlive];
    	[frontSheetButton setEnabled: isAlive];
    	[closeSheetButton setEnabled: isAlive];
    	[saveSheetButton setEnabled: isAlive];
    	[getDataButton setEnabled: isAlive];
    	[sendDataButton setEnabled: isAlive];
	
	return self;
}

- createSheet:sender;
{
	theInterface = [[XQ_Interface alloc] initForNewSheet];
	if(theInterface == nil) {
		NXRunAlertPanel("Communication Problem",
		    "Could not create sheet",
		    "OK", NULL, NULL);
	}
	[theInterface setDelegate:self];
	[self enableButtons];
	
	return self;
}

- connectSheet:sender;
{
	theInterface = [[XQ_Interface alloc] initForCurrentSheet];
	
	if(theInterface == nil) {
	
		NXRunAlertPanel("Communication Problem",
		
		    "Could not connect to current sheet.",
		    
		    "OK", NULL, NULL);
		    
		[self enableButtons];
		
		return self;
	}
	
	[theInterface setDelegate:self];
	[self enableButtons];
	
	return self;
}

- hideSheet:sender;
{
	[theInterface XQ_hide];
	return self;
}

- saveSheet:sender;
{
	[theInterface XQ_save];
	return self;
}

- closeSheet:sender;
{
	[theInterface XQ_close];
	return self;
}

- bringSheetToFront:sender;
{
	[theInterface XQ_makeKeyAndOrderFront];
	return self;
}

- getDataFromSheet:sender;
{
	int fr = 0;
	int fc = 0;
	int tr = fr + 4;
	int tc = fc + 2;
	id valueList = nil;
	int i, j, k = 0;
	id cell, val;
	int delivered = 0;
	int ok = YES;
	
    // create a list to hold the value cells
    
	valueList = [List new];
	
	delivered = [theInterface XQ_getValues: valueList
			fromRow: fr
			fromCol: fc
			toRow: tr
			toCol: tc
			ok: &ok];
	
	if(delivered != 0) {
		NXRunAlertPanel("Communication Problem",
		    "Could not communicate with Questor. Error code was %d",
		    "OK", NULL, NULL,
		    delivered);
		goto exit;
	}
	if(!ok) {
		NXRunAlertPanel("Communication Problem",
		    "Xanthus Questor could not provide data!",
		    "OK", NULL, NULL);
		goto exit;
	}
	
	for(i=0; i<5; i++) {
		for(j=0; j<3; j++) {
		
			cell = [fieldMatrix2 cellAt: i : j];
			val = [valueList objectAt: k++];
			
			//NXLogError("val type is %d", [val type]);
			
			if([val type] == XQ_INTERCHANGE_STRING)
				[cell setStringValue: [val stringValue]];
			else if([val type] == XQ_INTERCHANGE_DOUBLE)
				[cell setDoubleValue: [val doubleValue]];
			else [cell setIntValue: [val intValue]];
		}
	}
	
    exit:
	[[valueList freeObjects] free];
	
	return self;
}

- sendDataToSheet:sender;
{
	int fr = 0;
	int fc = 0;
	int tr = fr + 4;
	int tc = fc + 2;
	id valueList = nil;
	int i,j;
	id cell, val;
	int delivered = 0;
	int ok = YES;
	
    // create a list of value cells
    
	valueList = [List new];
	for(i=0; i<5; i++) {
		for(j=0; j<3; j++) {
		
			cell = [fieldMatrix2 cellAt: i : j];
			
			val = [[XQ_InterchangeCell alloc] init];
			[val setCellType: XQ_DOUBLE_ARG];
			[val setDoubleValue: [cell doubleValue]];
		
			[valueList addObject: val];
		}
	}
	
	delivered = [theInterface XQ_sendValues: valueList
			fromRow: fr
			fromCol: fc
			toRow: tr
			toCol: tc
			ok: &ok];
	if(delivered != 0) {
		NXRunAlertPanel("Communication Problem",
		    "Could not communicate with Questor. Error code was %d",
		    "OK", NULL, NULL,
		    delivered);
	}
	if(!ok) {
		NXRunAlertPanel("Communication Problem",
		    "Xanthus Questor could not handle the sent data!",
		    "OK", NULL, NULL);
	}
	
	delivered = [theInterface XQ_recalculate];
	if(delivered != 0) {
		NXRunAlertPanel("Communication Problem",
		    "Could not tell sheet to recalculate itself. Error code was %d",
		    "OK", NULL, NULL,
		    delivered);
	}
	
	delivered = [theInterface XQ_redisplay];
	if(delivered != 0) {
		NXRunAlertPanel("Communication Problem",
		    "Could not tell sheet to redisplay itself. Error code was %d",
		    "OK", NULL, NULL,
		    delivered);
	}
	
	[[valueList freeObjects] free];
	
	return self;
}

- XQ_sheetWillClose:sender;
{
	[self addMessage: "Sheet will close...\n"];
	[self enableButtons];
	return self;
}

- XQ_sheetDataDidChange:sender;
{
	[self addMessage: "Sheet data did change...\n"];
	return self;
}

- XQ_sheetWasSaved: (int)type sender:sender;
{
	[self addMessage: "Sheet was saved...\n"];
	return self;
}

- XQ_sheetDidBecomeEdited:sender;
{
	[self addMessage: "Sheet did become edited...\n"];
	return self;
}

- addMessage: (const char *)message;
{
	id text = [messageView docView];
	
	[text setSel: [text textLength] : 0];
	[text replaceSel:message];
	return self;
}


@end
