//
//  MPT_FaceView.m
//  AUCoderDemo
//
//  Created by Andrew Salamon on 5/19/06.
//  Copyright (c) 2006 Machine Perception Laboratory 
//  University of California San Diego.//

#import "MPT_FaceView.h"
#import "MPT_Face.h"

@interface MPT_FaceView(PrivateMethods)
-  (void)drawFace:(MPT_Face *)face inRect:(NSRect)rect;
@end

@implementation MPT_FaceView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
	{
        [self resetFaceAndEyes];
    }
    return self;
}

- (void)dealloc
{
	[lastDropPath release];
	[super dealloc];
}

- (void)resetFaceAndEyes
{
	[self setLastFace:NSMakeRect( -1, -1, -1, -1 )];
	[self setFaces:nil];
}

- (NSRect)lastFace {
    return lastFace;
}

- (void)setLastFace:(NSRect)value {
	lastFace = value;
}

- (NSArray *)faces
{
    return faces;
}

- (void)setFaces:(NSArray *)value
{
    if( faces != value )
    {
        [faces release];
        faces = [value retain];
    }
}

- (NSString *)lastDropPath {
    return [[lastDropPath retain] autorelease];
}

- (void)setLastDropPath:(NSString *)value {
    if (lastDropPath != value) {
        [lastDropPath release];
        lastDropPath = [value copy];
    }
}

- (BOOL)isFlipped
{
	return YES;
}

- (void)drawRect:(NSRect)rect
{
//	float eyeSize = lastFace.size.width * 0.04; // 4.0;
//	float eyeOffset = eyeSize / 2.0;
	NSSize mySize = [self frame].size;
	NSImage *myImage = [self image];
	NSSize size = [myImage size];
	float xdiff = (mySize.width - size.width) / 2;
	float ydiff = (mySize.height - size.height) / 2;
	
    [super drawRect:rect];
	
	if( [faces count] > 0 )
	{
		NSRect imgRect = [self frame];
		imgRect.origin.x = xdiff;
		imgRect.origin.y = ydiff;
		imgRect.size.width = size.width;
		imgRect.size.height = size.height;
		NSEnumerator *faceEnum = [faces objectEnumerator];
		MPT_Face *mptFace;
		while( mptFace = [faceEnum nextObject] )
		{
			[self drawFace:mptFace inRect:imgRect];
		}
	}
}

-  (void)drawFace:(MPT_Face *)face inRect:(NSRect)rect
{
	static NSColor *foreColor = nil;
	
	NSRect faceRect = [face face];
	
	float eyeSize = faceRect.size.width * 0.04; // 4.0;
	float eyeOffset = eyeSize / 2.0;
	float xdiff = rect.origin.x;
	float ydiff = rect.origin.y;
	
	if( !foreColor )
		foreColor = [NSColor blueColor];
	
	[foreColor setFill];
	
//	if( faceRect.origin.x > 0 )
	{
		faceRect.origin.x += xdiff;
		faceRect.origin.y += ydiff;
		NSFrameRect( faceRect );
	}
//	NSPoint leftEye = [face leftEye];
//	if( leftEye.x > 0 )
//	{
//		NSRect aRect = NSMakeRect(leftEye.x+xdiff-eyeOffset, leftEye.y+ydiff-eyeOffset, eyeSize, eyeSize);
//		NSFrameRect( aRect );
//		// NSRectFill( aRect );
//	}
//	NSPoint rightEye = [face rightEye];
//	if( rightEye.x > 0 )
//	{
//		NSRect aRect = NSMakeRect(rightEye.x+xdiff-eyeOffset, rightEye.y+ydiff-eyeOffset, eyeSize, eyeSize);
//		NSFrameRect( aRect );
//	}
	
	NSArray *features = [face features];
	NSEnumerator *featureEnum = [features objectEnumerator];
	MPT_Feature *feature;
	while( feature = [featureEnum nextObject] )
	{
		NSPoint loc = [feature location];
		NSRect aRect = NSMakeRect(loc.x+xdiff-eyeOffset, loc.y+ydiff-eyeOffset, eyeSize, eyeSize);
		if( [feature color] )
			[[feature color] setFill];
		else
			[[NSColor redColor] setFill];
		NSFrameRect( aRect );
	}
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
	NSPasteboard *pboard = [sender draggingPasteboard];
	NSURL *url = [NSURL URLFromPasteboard:pboard];
	
	if( [url isFileURL] )
		[self setLastDropPath:[url path]];
	else
		[self setLastDropPath:nil];

	[super concludeDragOperation:sender];
}

@end
