//
//  EXPConnectorComponent.m
//  Expression
//
//  Created by ashley on 20/11/2008.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "EXPAuxiliaryComponent.h"
#import "EXPModelObject.h"

@implementation EXPAuxiliaryComponent

- (id) componentType
{
	return @"Auxiliary";
}

- (BOOL) acceptsInputs
{
	return YES;
}

- (BOOL) acceptsOutputs
{
	return YES;
}

- (void) computePath
{
//	EXPModelObject *modelObject = [self modelObject];
//	NSDictionary *defaults = [modelObject defaults];

	NSSize size = [self size];
	NSPoint pos = [self pos];
	float x = pos.x;
	float y = pos.y;
	float w = size.width;
	float h = size.height;
	NSRect rect = NSMakeRect(x, y, w, h);
	NSBezierPath *path = [[NSBezierPath alloc] init];
	[path appendBezierPathWithOvalInRect:rect];
/*	[[defaults objectForKey:@"AuxiliaryFillColour"] set];
	[path fill];
	[[defaults objectForKey:@"AuxiliaryStrokeColour"] set];
	[path stroke]; */
	[self setPath:path];
	[path release];

}

- (BOOL) containsPoint:(NSPoint)point
{
	if ([self path]==nil) {
		[self computePath];
	}

	NSBezierPath *path = [self path];
	
	return [path containsPoint:point];
}

- (NSPoint) normalVectorForPoint:(NSPoint)point length:(float)length
{
//	NSPoint surfacePoint = [self closestSurfacePointToPoint:point];
	float xp = point.x;
	float yp = point.y;

	NSSize size = [self size];
	float w = size.width;
	float radius = w/2.0;

	NSPoint pos = [self pos];
	float x = pos.x;
	float y = pos.y;
	float xc = x + radius;
	float yc = y + radius;

	float nx = xp - xc;
	float ny = yp - yc;
	float nlength = hypot(nx, ny);
	
	return NSMakePoint(length*nx/nlength, length*ny/nlength);
}

- (NSPoint) closestSurfacePointToPoint:(NSPoint)point
{
	float xp = point.x;
	float yp = point.y;
	NSPoint pos = [self pos];
	float x = pos.x;
	float y = pos.y;
	NSSize size = [self size];
	float w = size.width;
//	float h = size.height;
	
	float radius = w/2.0;
	float xc = x + radius;
	float yc = y + radius;
	
	float distance = hypot(xp - xc, yp - yc);
	float xs = xc + (xp - xc)*radius/distance;
	float ys = yc + (yp - yc)*radius/distance;

	return NSMakePoint(xs, ys);
}

- (NSRect) drawingBounds
{
	NSPoint pos = [self pos];
	NSSize size = [self size];
	NSRect rect = NSMakeRect(pos.x, pos.y, size.width, size.height);
	
	EXPModelObject *modelObject = [self modelObject];
	NSDictionary *attribs = [modelObject textAttributes];
	NSString *name = [self name];
	NSSize nameSize = [name sizeWithAttributes:attribs];
	float xp = pos.x + (size.width - nameSize.width)/2.0;
	float yp = pos.y - nameSize.height - 2.0;
	NSRect nameBounds = NSMakeRect(xp, yp, nameSize.width, nameSize.height);
	
	return NSUnionRect(rect, nameBounds);
}

- (void) drawInView:(id)view isSelected:(BOOL)flag
{
	if ([self path]==nil) {
		[self computePath];
	}

	EXPModelObject *modelObject = [self modelObject];
	NSDictionary *defaults = [modelObject defaults];
	
/*	NSSize size = [self size];
	NSPoint pos = [self pos];
	float x = pos.x;
	float y = pos.y;
	float w = size.width; */
/*	float h = size.height;
	NSRect rect = NSMakeRect(x, y, w, h);
	NSBezierPath *path = [[NSBezierPath alloc] init];
	[path appendBezierPathWithOvalInRect:rect];
	[[defaults objectForKey:@"AuxiliaryFillColour"] set];
	[path fill];
	[[defaults objectForKey:@"AuxiliaryStrokeColour"] set];
	[path stroke];
	[path release]; */
	NSBezierPath *path = [self path];
	[[defaults objectForKey:@"AuxiliaryFillColour"] set];
	[path fill];
	[[defaults objectForKey:@"AuxiliaryStrokeColour"] set];
	[path stroke];
	
	if (flag) {
		NSSize size = [self size];
		NSPoint pos = [self pos];
		float controlSquareSize = [[defaults objectForKey:@"ControlSquareSize"] floatValue];
		[[NSColor magentaColor] set];

		NSFrameRect(NSMakeRect(pos.x - controlSquareSize/2.0, pos.y - controlSquareSize/2.0, controlSquareSize, controlSquareSize));
		NSFrameRect(NSMakeRect(pos.x + size.width - controlSquareSize/2.0, pos.y - controlSquareSize/2.0, controlSquareSize, controlSquareSize));
		NSFrameRect(NSMakeRect(pos.x - controlSquareSize/2.0, pos.y + size.height - controlSquareSize/2.0, controlSquareSize, controlSquareSize));
		NSFrameRect(NSMakeRect(pos.x + size.width - controlSquareSize/2.0, pos.y + size.height - controlSquareSize/2.0, controlSquareSize, controlSquareSize));
	}

/*	NSString *name = [self name];
	float xp = x + w/2.0;
	float yp = y;
	NSDictionary *attribs = [[NSDictionary alloc] initWithObjectsAndKeys:
							[NSFont fontWithName:[defaults objectForKey:@"FontName"] size:[[defaults objectForKey:@"FontSize"] floatValue]], NSFontAttributeName,
							[defaults objectForKey:@"TextColour"], NSForegroundColorAttributeName,
							nil];
	NSSize nameSize = [name sizeWithAttributes:attribs];
	[name drawAtPoint:NSMakePoint(xp - nameSize.width/2.0, yp - nameSize.height  - 2.0) withAttributes:attribs];
	[attribs release]; */
	[self drawName];

}

@end