//
//  DiceView.m
//  Craps
//
//  Created by Rich on 2/2/11.
//  Copyright 2011 Swarthmore College. All rights reserved.
//

#import "DiceView.h"


@implementation DiceView
@synthesize delegate;
@synthesize dieFace;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
	CGRect bounds = [self bounds];
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetLineWidth(context, 10);

	[[UIColor blackColor] setStroke];
	[[UIColor blueColor] setFill];

	
	//draw box for die
	CGContextBeginPath(context);
		
	CGContextMoveToPoint(context, bounds.origin.x, bounds.origin.y);
	CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, bounds.origin.y);
	CGContextAddLineToPoint(context, bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height);
	CGContextAddLineToPoint(context, bounds.origin.x, bounds.origin.y + bounds.size.height);
	CGContextClosePath(context);
	CGContextStrokePath(context);
	

	//draw dots for die
	[[UIColor redColor] setFill];

	CGFloat dotRadius = bounds.size.width / 12.0;
	if (bounds.size.height / 12.0 < dotRadius) dotRadius = bounds.size.width;
	CGContextSetLineWidth(context, 5);


	//don't use delegate
	//int dieValue = [self.delegate diceValueForDiceView:self];
	int dieValue = self.dieFace;
	if (dieValue < 1 || dieValue > 6) { dieValue = 0; } // don't display bad data
	
	//draw top left dot
	if (dieValue == 4 || dieValue == 5 || dieValue == 6) {
		CGContextAddArc(context, bounds.origin.x + bounds.size.width/4.0, bounds.origin.y + bounds.size.height/4.0, 
						dotRadius, 0, 2*M_PI, 0);
		CGContextFillPath(context);
	}

	//draw top right dot
	if (dieValue >= 2) {
		CGContextAddArc(context, bounds.origin.x + bounds.size.width*3.0/4.0, 
						bounds.origin.y + bounds.size.height/4.0, 
						dotRadius, 0, 2*M_PI, 0);
		CGContextFillPath(context);
	}

	//draw center left dot
	if (dieValue == 6) {
		CGContextAddArc(context, bounds.origin.x + bounds.size.width/4.0, 
						bounds.origin.y + bounds.size.height*2.0/4.0, 
						dotRadius, 0, 2*M_PI, 0);
		CGContextFillPath(context);
	}

	//draw center middle dot
	if (dieValue % 2 == 1) {
		CGContextAddArc(context, bounds.origin.x + bounds.size.width*2.0/4.0, 
						bounds.origin.y + bounds.size.height*2.0/4.0, 
						dotRadius, 0, 2*M_PI, 0);
		CGContextFillPath(context);
	}
	
	//draw center right dot
	if (dieValue == 6) {
		CGContextAddArc(context, bounds.origin.x + bounds.size.width*3.0/4.0, 
						bounds.origin.y + bounds.size.height*2.0/4.0, 
						dotRadius, 0, 2*M_PI, 0);
		CGContextFillPath(context);
	}
	
	//draw bottom left dot
	if (dieValue >= 2) {
		CGContextAddArc(context, bounds.origin.x + bounds.size.width/4.0, 
						bounds.origin.y + bounds.size.height*3.0/4.0, 
						dotRadius, 0, 2*M_PI, 0);
		CGContextFillPath(context);
	}
	
	//draw bottom right dot
	if (dieValue >= 4) {
		CGContextAddArc(context, bounds.origin.x + bounds.size.width*3.0/4.0, 
						bounds.origin.y + bounds.size.height*3.0/4.0, 
						dotRadius, 0, 2*M_PI, 0);
		CGContextFillPath(context);
	}
	
	
}

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


@end
