#import "ClockView.h"

float calculateAngle(float x, float y, enum roundType roundDirection) 
{
	float angle = (180.0/3.14159)*atan2f(y, x) - 90.0;
	if ((angle>=0.0) && (angle<=90.0)) {
		angle -= 360.0;
	}
	if (roundDirection==roundToNearest) {
		angle = 30.0*roundf(angle/30.0);
	} else if (roundDirection==roundToLast) {
		angle = 30.0*ceilf(angle/30.0);
	}
	return angle;
}

@implementation ClockView

- (id) initWithFrame:(NSRect)frameRect
{
	if ((self = [super initWithFrame:frameRect]) != nil) {
		_hours = 0;
		_minutes = 0;	
		_exactAngles = NO;
	}
	return self;
}

- (void) setTimeToHours:(int)hours andMinutes:(int)minutes
{	
	NSLog(@"ClockView setTimeToHours:%d andMinutes:%d", hours, minutes);
	_hours = hours;
	_minutes = minutes;
	float denominator = _exactAngles?360.0:60.0;
	float angle = minutes*360.0/denominator;
	[_bigHand setAngle:-angle];
	denominator = _exactAngles?360.0:12.0;
	angle = hours*360.0/denominator + 30.0*minutes/60.0;
	[_littleHand setAngle:-angle];
	[self setNeedsDisplay:YES];
}

- (int) hours
{
	int hours = (_hours==0)?12:_hours;
	return hours;
}

- (void) setHours:(int)hours
{
	_hours = hours;
}

- (int) minutes
{
	return _minutes;
}

- (void) setMinutes:(int)minutes
{
	_minutes = minutes;
}

- (void) setupFace
{
	NSRect bounds = [self bounds];
	[[NSColor whiteColor] set];
	NSRectFill(bounds);
	
	_hw = bounds.size.width/2.0;
	_hh =  bounds.size.height/2.0;
	_centre = NSMakePoint(_hw, _hh);
	
	float border = 5.0;
	if (bounds.size.width>bounds.size.height) {
		_l = bounds.size.height - border;
		_hl = _l/2.0;
		_ox = _hw - _hl;
		_oy = 0.0;
	} else {
		_l = bounds.size.width - border;
		_hl = _l/2.0;
		_ox = 0.0;
		_oy = _hh - _hl;
	}
	
	_numeralRadius = 0.9*_hl;
	_numeralSize = 0.1*_hl;
	
	_bigHandLength = 0.8*_hl;
	_bigHandWidth = 0.1*_hl;
	float _littleHandLength = 0.5*_hl;
	float _littleHandWidth = _bigHandWidth;

	ClockHand *bigHand = [[ClockHand alloc] initWithCentre:_centre andRadius:_bigHandLength andWidth:_bigHandWidth];
	[bigHand setAngle:0.0];
	[bigHand setColour:[NSColor blueColor]];
	[self setBigHand:bigHand];
	[bigHand release];
	
	ClockHand *littleHand = [[ClockHand alloc] initWithCentre:_centre andRadius:_littleHandLength andWidth:_littleHandWidth];
	[littleHand setAngle:0.0];
	[littleHand setColour:[NSColor redColor]];
	[self setLittleHand:littleHand];
	[littleHand release];
	
	_selectedHand = nil;

}

- (void) drawRect:(NSRect)rect
{
	NSRect dialrect = NSMakeRect(_ox, _oy, _l, _l);
	NSBezierPath *dialCircle = [NSBezierPath bezierPathWithOvalInRect:dialrect];
	[[NSColor greenColor] set];
	[dialCircle fill];
	[[NSColor blackColor] set];
	[dialCircle stroke];

	NSMutableDictionary *attribs  = [[NSMutableDictionary alloc] init];
	[attribs setObject:[NSFont fontWithName:@"Arial" size:24] forKey:NSFontAttributeName];
	[attribs setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];

	[[NSColor blackColor] set];
	int i;
	for(i=1; i<=12; i++) {
		NSString *numeral = [[NSString alloc] initWithFormat:@"%d", i];
		
		float theta = -(i*2.0*3.14159/12.0 - 3.14159/2.0);
		NSPoint pt = NSMakePoint(_hw + _numeralRadius*cos(theta), _hh + _numeralRadius*sin(theta));
		[numeral drawAtPoint:pt withAttributes:attribs];
		[numeral release];

		float dotRadius = 0.9*_numeralRadius;
		float x = _hw + dotRadius*cos(theta);
		float y =  _hh + dotRadius*sin(theta);
		NSRect dot = NSMakeRect(x, y, 10.0, 10.0);
		NSBezierPath *oval = [NSBezierPath bezierPathWithOvalInRect:dot];
		[[NSColor blackColor] set];
		[oval fill];
		
	}
	
	[attribs release];
	
	[_bigHand draw];
	[_littleHand draw];

}

- (void) setBigHand:(ClockHand *)bigHand
{
	[bigHand retain];
	[_bigHand release];
	_bigHand = bigHand;
}

- (ClockHand *)bigHand
{
	return _bigHand;
}

- (void) setLittleHand:(ClockHand *)littleHand
{
	[littleHand retain];
	[_littleHand release];
	_littleHand = littleHand;
}

- (ClockHand *)littleHand
{
	return _littleHand;
}

- (void) selectHand:(ClockHand *)hand
{
	_selectedHand = hand;
}

- (ClockHand *)selectedHand
{
	return _selectedHand;
}

- (void)mouseDown:(NSEvent *)event
{
	NSPoint location = [event locationInWindow];
	NSPoint point = [self convertPoint:location fromView:nil];
//	NSLog(@"ClockView --- mouseDown:(NSEvent *)event %@", NSStringFromPoint(point));
	
	if ([[[self littleHand] path] containsPoint:point]) {
		_selectedHand = [self littleHand];
	} else if ([[[self bigHand] path] containsPoint:point]) {
		_selectedHand = [self bigHand];
	} else {
		_selectedHand = nil;
	}
}

- (void)mouseUp:(NSEvent *)event
{
	NSPoint location = [event locationInWindow];
	NSPoint point = [self convertPoint:location fromView:nil];
//	NSLog(@"ClockView --- mouseUp:(NSEvent *)event");
	float angle = calculateAngle(point.x - _centre.x, point.y - _centre.y, roundToLast);
//	printf("angle = %f\n", angle);
	
	if (_selectedHand==_bigHand) {
		printf("angle = %f\n", angle);
		[self setMinutes:(int)roundf(-angle*60.0/360.0)];
		[[self bigHand] setAngle:angle];
		angle = [self hours]*360.0/12.0 + [self minutes]*30.0/60.0;
		[[self littleHand] setAngle:-angle];
		[self setNeedsDisplay:YES];
	} else if (_selectedHand==_littleHand) {
		angle -= [self minutes]*(30.0/60.0);
		printf("angle = %f\n", angle);
		[[self littleHand] setAngle:angle];
		[self setHours:(int)floorf(-angle*12.0/360.0)];
		[self setNeedsDisplay:YES];
	}
	printf("%d %d\n", [self hours], [self minutes]);
}

- (void)mouseDragged:(NSEvent *)event
{
	NSPoint location = [event locationInWindow];
	NSPoint point = [self convertPoint:location fromView:nil];
//	NSLog(@"ClockView --- mouseDragged:(NSEvent *)event");
	float angle = calculateAngle(point.x - _centre.x, point.y - _centre.y, noRounding);
	
	if (_selectedHand==_bigHand) {
		[_bigHand setAngle:angle];
		[self setNeedsDisplay:YES];
	} else if (_selectedHand==_littleHand) {
		[_littleHand setAngle:angle];
		[self setNeedsDisplay:YES];
	}
}

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

@end
