//
//  YCLayerHostingView.m
//  HR-WorkLoad
//
//  Created by Julian on 2/6/10.
//  Copyright 2010 Electronic Visualization Laboratory, University of Illinois at Chicago. All rights reserved.
//

#import "YCLayerHostingView.h"

float decDistSq(NSDecimal x0, NSDecimal x1, NSDecimal y0, NSDecimal y1)
{
	double retVal = 0.0f;
	
	NSDecimalNumber *_x0 = [NSDecimalNumber decimalNumberWithDecimal:x0];
	NSDecimalNumber *_x1 = [NSDecimalNumber decimalNumberWithDecimal:x1];
	NSDecimalNumber *_y0 = [NSDecimalNumber decimalNumberWithDecimal:y0];
	NSDecimalNumber	*_y1 = [NSDecimalNumber decimalNumberWithDecimal:y1];
	
	retVal = sqrt(([_x1 doubleValue] - [_x0 doubleValue]) * ([_x1 doubleValue] - [_x0 doubleValue]) + 
				  ([_y1 doubleValue] - [_y0 doubleValue]) * ([_y1 doubleValue] - [_y0 doubleValue]));
	
	return (float) retVal;
}

@implementation YCLayerHostingView

@synthesize brushDelegate;

-(id) initWithFrame:(NSRect)frameRect {
	if (! (self = [super initWithFrame:frameRect])) return nil;
	
	trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds 
												options:(NSTrackingMouseMoved|NSTrackingActiveInKeyWindow) 
												  owner:self 
											   userInfo:nil];
	[self addTrackingArea:trackingArea];
	
	return self;
}

-(void) updateTrackingAreas {
	[self removeTrackingArea:trackingArea];
	[trackingArea release];
	trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds 
												options:(NSTrackingMouseMoved|NSTrackingActiveInKeyWindow) 
												  owner:self 
											   userInfo:nil];
	[self addTrackingArea:trackingArea];
}

-(BOOL) inRangeInput:(NSDecimal) a usingCoord:(CPCoordinate) coord left:(NSDecimal *)left right:(NSDecimal *)right
{
	BOOL b = (BOOL)(CPDecimalGreaterThan(a, left[coord]) && CPDecimalLessThan(a, right[coord]));
	return b;
}

-(void) brushDetection:(NSEvent *)theEvent
{
	CPGraph *graph = (CPGraph *)self.hostedLayer;
	CPXYPlotSpace *plotspace = (CPXYPlotSpace *)[graph defaultPlotSpace];
	
	CGPoint mp = NSPointToCGPoint([self convertPoint:[theEvent locationInWindow] fromView:nil]);
	CGPoint gp = [self.layer convertPoint:mp toLayer:graph.plotArea];
	
	float s = 5.0f;
	NSDecimal mouse[2];
	NSDecimal left[2];
	NSDecimal right[2];
	
	[plotspace plotPoint:mouse forPlotAreaViewPoint:gp];
	[plotspace plotPoint:left  forPlotAreaViewPoint:CGPointMake(gp.x - s, gp.y - s)];
	[plotspace plotPoint:right forPlotAreaViewPoint:CGPointMake(gp.x + s, gp.y + s)];
	
		// Block removed. Use private method `-(BOOL) inRangeInput:'
	
	float mindist = MAXFLOAT;
	
	int plotnum = -1;
	int pointnum = -1;
	NSDecimal xval, yval;
	
	int i = -1;
	for (CPPlot * plot in [graph allPlots]) {
		i++;
		NSArray * xs = [plot cachedNumbersForField:CPCoordinateX];
		NSArray * ys = [plot cachedNumbersForField:CPCoordinateY];
		int j = -1;
		for (NSNumber *x in xs) {
			j++;
			NSDecimal xd = [x decimalValue];
				//			if (inRange(xd, CPCoordinateX)) {
			if([self inRangeInput:xd usingCoord:CPCoordinateX left:left right:right]) {
				NSDecimal yd = [[ys objectAtIndex:j] decimalValue];
					//				if (inRange(yd, CPCoordinateY)) {
				if([self inRangeInput:yd usingCoord:CPCoordinateY left:left right:right]) {
					float d = decDistSq(xd, mouse[CPCoordinateX], yd, mouse[CPCoordinateY]);
					
					if (d < mindist) {
						mindist = d;
						xval = xd;
						yval = yd;
						plotnum = i;
						pointnum = j;
					}}}}}
	
	if (plotnum > -1 && pointnum > -1) {
			// Call a registered YCBrushDelegate for updates(UI and/or data)
		if(brushDelegate) {
			[brushDelegate brushFoundDataInPlotNumber:plotnum 
									  withPointNumber:pointnum 
										   withXValue:CPDecimalFloatValue(xval) 
											 withYVal:CPDecimalFloatValue(yval)];
		}
	}
}

#pragma mark Mouse Events
-(void) mouseMoved:(NSEvent *)theEvent 
{
	[self brushDetection:theEvent];
}

-(void) mouseDown:(NSEvent *)theEvent 
{
	[self brushDetection:theEvent];
}

@end
