//
//  CrapsViewController.m
//  Craps
//
//  Created by Rich on 1/19/11.
//  Copyright 2011 Swarthmore College. All rights reserved.
//

#import "CrapsViewController.h"


@interface CrapsViewController()
@property (readonly) CrapsModel* model;
@end


@implementation CrapsViewController

@synthesize balanceLabel, totalLabel, pointLabel, dieLabel1, dieLabel2, anteLabel;
@synthesize anteSlider;

- (CrapsModel *)model
{
	if (!model) {
		model = [[CrapsModel alloc] init];
	}
	return model;
}

- (void)displayBalance
{
	balanceLabel.text = [NSString stringWithFormat:@"Cash: $%d", self.model.balance];
}


- (void)displayAnte 
{
	NSLog(@"%d", self.model.ante);
	int value = self.model.ante;
	anteSlider.value = (float)value;
	anteLabel.text = [NSString stringWithFormat:@"$%3d", self.model.ante];
}


- (void)displayPoint
{
	if (self.model.point == 0) {		
		pointLabel.text = @"OFF";
		pointLabel.textColor = [UIColor blackColor];
	} else {
		pointLabel.text = [NSString stringWithFormat:@"%d", self.model.point];
		pointLabel.textColor = [UIColor redColor];
	}	
}


- (NSString *)dieGlyph:(int)value
{
	NSString *glyph = nil;
	switch (value) {
		case 1:	glyph = @"\u2680"; break;
		case 2:	glyph = @"\u2681"; break;
		case 3:	glyph = @"\u2682"; break;
		case 4:	glyph = @"\u2683"; break;
		case 5:	glyph = @"\u2684"; break;
		case 6:	glyph = @"\u2685"; break;
	}
	return glyph;
}

- (void)displayTotal
{
	NSArray *dice = self.model.dice;
	if (dice) {
		int sum = [[dice objectAtIndex:0] intValue] + [[dice objectAtIndex:1] intValue];
		totalLabel.text = [NSString stringWithFormat:@"%d", sum];
	} else {
		totalLabel.text = @"Total:";
	}
}

- (void)displayDice
{
	NSArray* dice = self.model.dice;
	
	int d1 = [(NSNumber *)[dice objectAtIndex:0] intValue];
	int d2 = [(NSNumber *)[dice objectAtIndex:1] intValue];
	
	dieLabel1.text = [self dieGlyph:d1];
	dieLabel2.text = [self dieGlyph:d2];
}

- (void)updateDisplay
{
	[self displayPoint];
	[self displayBalance];
	[self displayAnte];
	[self displayDice];
	[self displayTotal];
}

- (void)viewDidLoad {
	[super viewDidLoad];
	[self updateDisplay];
}


- (void)showAlert:(NSString *)title withMessage:(NSString *)message
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
													message:message
												   delegate:self
										  cancelButtonTitle:@"OK"
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
}





- (IBAction)rollPressed
{
	[self.model playTurn];
	
	if (self.model.won) {
		[self showAlert:@"You win!" withMessage:@""];
	} else if (self.model.lost) {
		if (self.model.bankrupt) {
			[self showAlert:@"You're broke!" withMessage:@"Resetting your balance"];
			[self.model reset];
		} else {
			[self showAlert:@"You lost!" withMessage:@""];
		}
	}
	[self updateDisplay];
}


- (IBAction)sliderMoved:(UISlider *)sender {
	self.model.ante = (int)[anteSlider value];
	[self displayAnte];
}

// handle shaking event
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
	[self rollPressed];
}

- (BOOL)canBecomeFirstResponder {
	return YES;
}

- (void)viewDidAppear:(BOOL)animated {
	[self becomeFirstResponder];	
}

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

@end
