//
//  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 setLastLeftEye:NSMakePoint( -1, -1 )];
	[self setLastRightEye:NSMakePoint( -1, -1 )];
	[self setFaces:nil];
}

- (NSRect)lastFace {
    return lastFace;
}

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

- (NSPoint)lastLeftEye {
    return lastLeftEye;
}

- (void)setLastLeftEye:(NSPoint)value {
	lastLeftEye = value;
}

- (NSPoint)lastRightEye {
    return lastRightEye;
}

- (void)setLastRightEye:(NSPoint)value {
	lastRightEye = 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( lastFace.origin.x > 0 )
//	{
//		NSRect tmp = lastFace;
//		tmp.origin.x += xdiff;
//		tmp.origin.y += ydiff;
//		NSFrameRect( tmp );
//	}
//	if( lastLeftEye.x > 0 )
//	{
//		NSRect aRect = NSMakeRect(lastLeftEye.x+xdiff-eyeOffset, lastLeftEye.y+ydiff-eyeOffset, eyeSize, eyeSize);
//		NSFrameRect( aRect );
//		// NSRectFill( aRect );
//	}
//	if( lastRightEye.x > 0 )
//	{
//		NSRect aRect = NSMakeRect(lastRightEye.x+xdiff-eyeOffset, lastRightEye.y+ydiff-eyeOffset, eyeSize, eyeSize);
//		NSFrameRect( aRect );
//	}
	
	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;
	static NSDictionary *attrs = nil;
	
	NSRect faceRect = [face face];
	NSPoint leftEye = [face leftEye];
	NSPoint rightEye = [face rightEye];
	
	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];
	if( !attrs )
		attrs = [[NSDictionary alloc] initWithObjectsAndKeys:foreColor, NSForegroundColorAttributeName, [NSFont userFontOfSize:14], NSFontAttributeName, nil];
	
	[foreColor setFill];
	
//	if( faceRect.origin.x > 0 )
	{
		faceRect.origin.x += xdiff;
		faceRect.origin.y += ydiff;
		NSFrameRect( faceRect );
	}
	if( leftEye.x > 0 )
	{
		NSRect aRect = NSMakeRect(leftEye.x+xdiff-eyeOffset, leftEye.y+ydiff-eyeOffset, eyeSize, eyeSize);
		NSFrameRect( aRect );
		// NSRectFill( aRect );
	}
	if( rightEye.x > 0 )
	{
		NSRect aRect = NSMakeRect(rightEye.x+xdiff-eyeOffset, rightEye.y+ydiff-eyeOffset, eyeSize, eyeSize);
		NSFrameRect( aRect );
	}
	
	/*
	NSString *angle = [NSString stringWithFormat:@"%.4f", [face headTilt]];
	NSRect rect1 = faceRect;
	
	rect1.origin.y = faceRect.origin.y + faceRect.size.height - 25;
	rect1.origin.x += 5.0;
	[angle drawInRect:rect1 withAttributes:attrs];
	 */
}

- (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
