//
//  EXPInspectorController.m
//  Expression
//
//  Created by ashley on 16/12/2008.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "EXPInspectorController.h"
#import "EXPComponent.h"
#import "EXPConnectorComponent.h"
#import "EXPDocument.h"
#import "EXPModelObject.h"

@implementation EXPInspectorController

+ (id) sharedInspectorController 
{
    static EXPInspectorController *inspectorController = nil;

    if (!inspectorController) {
        inspectorController = [[EXPInspectorController allocWithZone:NULL] init];
    }

    return inspectorController;
}

- (id) init 
{
    self = [self initWithWindowNibName:@"EXPInspector"];
    if (self) {
        [self setWindowFrameAutosaveName:@"EXPInspector"];
    }
    return self;
}

- (void) awakeFromNib
{
	[functionTypeSelector removeAllItems];
	[functionTypeSelector addItemsWithTitles:[NSArray arrayWithObjects:@"Elementary Functions", nil]];
	[functionTypeSelector selectItemAtIndex:0];
	[self setFunctionNames:0];
	[inputSelector removeAllItems];
}

- (void) setFunctionNames:(int)index
{
	NSArray *array;

	if (index==0) {
		array = [[NSArray alloc] initWithObjects:@"sin", @"cos", @"tan", @"asin", @"acos", @"atan",
			@"sinh", @"cosh", @"tanh", @"asinh", @"acosh", @"atanh",
			@"min", @"max", nil];
	} else if (index==1) {
	}

	[functionNameSelector removeAllItems];
	[functionNameSelector addItemsWithTitles:array];
	[array release];

} 

- (void) setSelection:(id)selection
{
	_selection = selection;
	[self refreshSelection];
}

- (id) selection
{
	return _selection;
}

- (void) refreshSelection
{
//	NSLog(@"EXPInspectorController: refreshSelection");
	NSMutableSet *selection = [self selection];
	NSPoint position;
	NSString *name = @"";
	NSString *xPositionString = @"";
	NSString *yPositionString = @"";
	NSString *definition = @"";
	NSString *document = @"";
	NSArray *inputs = nil;
	NSMutableArray *titles = nil;

	if ((selection==nil) || ([selection count]!=1)) {
	} else {
		EXPComponent *component = [selection anyObject];
		name = [component name];
		position = [component pos];
		xPositionString = [NSString stringWithFormat:@"%g", position.x];
		yPositionString = [NSString stringWithFormat:@"%g", position.y];
		definition = [component definitionAsString];
		document = [component document];
		
		inputs = [component inputComponents];
		titles = [[NSMutableArray alloc] init];
		int i;
		for(i=0; i<[inputs count]; i++) {
			EXPComponent *input = [inputs objectAtIndex:i];
			[titles addObject:[input name]];
		}
//		[titles addObject:[component name]];
	}
	if (name!=nil) [nameField setStringValue:name];
	if (xPositionString!=nil) [xPositionField setStringValue:xPositionString];
	if (yPositionString!=nil) [yPositionField setStringValue:yPositionString];
//	NSLog(@"definition = %@.", definition);
	if (definition!=nil) [definitionField setString:definition];
//	NSLog(@"document = %@.", document);
	if (document!=nil) [documentField setString:document];

	[inputSelector removeAllItems];
	[inputSelector addItemsWithTitles:titles];
//	[inputSelector selectItemAtIndex:0];
	
	[titles release];

}

- (IBAction) functionTypeChanged:(id)sender
{
	NSLog(@"EXPInspectorController: functionTypeChanged %@.", [sender titleOfSelectedItem]);
}

- (IBAction) functionNameChanged:(id)sender
{
//	NSLog(@"EXPInspectorController: functionNameChanged %@.", [sender titleOfSelectedItem]);
	NSString *function = [functionNameSelector titleOfSelectedItem];
//	NSLog(@"EXPInspectorController: sendFunctionSelection %@", function);
	
//	NSRange range = [definitionField selectedRange];
	NSString *string = [[NSString alloc] initWithFormat:@"%@()", function];
	[definitionField insertText:string];
	[string release];
}

- (IBAction) inputNameChanged:(id)sender
{
	NSLog(@"EXPInspectorController: inputNameChanged %@.", [sender titleOfSelectedItem]);
	NSMutableSet *selection = [self selection];
	if ((selection==nil) || ([selection count]!=1)) {
	} else {
		EXPComponent *component = [selection anyObject];
		NSString *input = [inputSelector titleOfSelectedItem];
		NSString *string = nil;
		NSArray *inputs = [component inputComponents];
		int i;
		for(i=0; i<[inputs count]; i++) {
			EXPComponent *inputComponent = [inputs objectAtIndex:i];
//			EXPComponent *start = [connector start];
			if ([[inputComponent name] isEqualToString:input]) {
				string = [inputComponent displayName];
				break;
			}
		}
		NSLog(@"EXPInspectorController: sendInputSelection %@", string);
	
		[definitionField insertText:string];
	}
}

- (IBAction) keypadButtonPressed:(id)sender
{
	NSString *string = [[sender selectedCell] title];
//	NSLog(@"EXPInspectorController: keypadButtonPressed %@", string);
	[definitionField insertText:string];
}

- (IBAction) revertButtonPressed:(id)sender
{
}

 - (IBAction) applyButtonPressed:(id)sender
{
	NSLog(@"EXPInspectorController: applyButtonPressed.");
/*	NSMutableSet *selection = [self selection];
	
	if ([selection count]==1) {
		EXPComponent *component = [selection anyObject];
		printf("Component name = %s\n", [[component name] UTF8String]);

		NSTabViewItem *tabViewItem = [tabView selectedTabViewItem];
		
		if ([[tabViewItem label] isEqualToString:@"Attributes"]) {
			NSString *docString = [[NSString alloc] initWithString:[documentField string]];
			[component setDocument:docString];
			[docString release];
		} else if ([[tabViewItem label] isEqualToString:@"Definition"]) {
			NSString *defString = [[NSString alloc] initWithString:[definitionField string]];
			NSLog(@"defString = %@.", defString);
			[component setDefinitionFromString:defString];
			[defString release];
		}
		
		EXPModelObject *modelObject = [component modelObject];
		EXPDocument *document = [modelObject document];
		[[document mainview] setNeedsDisplay:YES];
	} */
	NSMutableSet *selection = [self selection];
	
	if ([selection count]==1) {
		EXPComponent *component = [selection anyObject];
		NSString *defString = [definitionField string];
		[component setDefinitionFromString:defString];
		[self refreshSelection];
	}
}

- (IBAction) sendFunctionSelection:(id)sender
{
	NSString *function = [functionNameSelector titleOfSelectedItem];
//	NSLog(@"EXPInspectorController: sendFunctionSelection %@", function);
	
//	NSRange range = [definitionField selectedRange];
	NSString *string = [[NSString alloc] initWithFormat:@"%@()", function];
	[definitionField insertText:string];
	[string release];
}

- (IBAction) sendInputSelection:(id)sender
{
	NSMutableSet *selection = [self selection];
	if ((selection==nil) || ([selection count]!=1)) {
	} else {
		EXPComponent *component = [selection anyObject];
		NSString *input = [inputSelector titleOfSelectedItem];
		NSString *string = nil;
		NSArray *inputs = [component inputComponents];
		int i;
		for(i=0; i<[inputs count]; i++) {
			EXPComponent *inputComponent = [inputs objectAtIndex:i];
//			EXPComponent *start = [connector start];
			if ([[inputComponent name] isEqualToString:input]) {
				string = [inputComponent displayName];
				break;
			}
		}
		NSLog(@"EXPInspectorController: sendInputSelection %@", string);
	
		[definitionField insertText:string];
	}
}

/*- (void)comboBoxSelectionDidChange:(NSNotification *)notification
{
	NSComboBox *combo = [notification object];
	if (combo==functionTypeSelector) {
		int index = [combo indexOfSelectedItem];
		[self setFunctionNames:index];
	}
} */

- (void)controlTextDidEndEditing:(NSNotification *)notification
{
	NSMutableSet *selection = [self selection];
	NSControl *control = [notification object];
	NSString *string = [[NSString alloc] initWithString:[control stringValue]];
	NSLog(@"EXPInspectorController: controlTextDidChange %@.", [control stringValue]);
	
	if ([selection count]==1) {
		EXPComponent *component = [selection anyObject];
		EXPModelObject *modelObject = [component modelObject];
	
		if (control==nameField) {
			[component setName:[control stringValue]];
		} else if (control==xPositionField) {
			float x = [control floatValue];
			NSPoint pt = [component pos];
			pt.x = x;
			[component setPosition:pt];
		} else if (control==yPositionField) {
			float y = [control floatValue];
			NSPoint pt = [component pos];
			pt.y = y;
			[component setPosition:pt];
		}  
		
//		EXPModelObject *modelObject = [component modelObject];
		EXPDocument *document = [modelObject document];
		[[document mainview] setNeedsDisplay:YES];
	} else {
/*		EXPDocument *document = [self document];
		EXPModelObject *modelObject = [document modelObject];
		if (control==StartTimeField) {
			[modelObject setOption:string forName:@"tstart"];
		} else if (control==StopTimeField) {
			[modelObject setOption:string forName:@"tstop"];
		} else if (control==OutstepField) {
			[modelObject setOption:string forName:@"outstep"];
		} else if (control==ToleranceField) {
			[modelObject setOption:string forName:@"epsilon"];
//		} else if (textView==StartTimeField) {
		} */
	}
}

- (void)textDidEndEditing:(NSNotification *)notification
{
	NSLog(@"EXPInspectorController: textDidEndEditing.");
}

- (void)textDidChange:(NSNotification *)notification
{
	NSMutableSet *selection = [self selection];
	
	if ([selection count]==1) {
		NSTextView *textView = [notification object];
		NSLog(@"EXPInspectorController: textDidEndEditing %@.", [textView string]);
		EXPComponent *component = [selection anyObject];
		EXPModelObject *modelObject = [component modelObject];
		EXPDocument *document = [modelObject document];
		NSString *string = [[NSString alloc] initWithString:[textView string]];

/*		if (textView==documentField) {
			[component setDocument:[textView string]];
		} */
		if (textView==documentField) {
			NSString *docString = [[NSString alloc] initWithString:string];
			[component setDocument:docString];
			[docString release];
		} else if (textView==definitionField) {
			NSString *defString = [[NSString alloc] initWithString:string];
//			NSLog(@"defString = %@.", defString);
			[component setDefinitionFromString:defString];
			[defString release];
		}
		
		[[document mainview] setNeedsDisplay:YES];
	}
} 

@end