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

#import "CrapsViewController.h"



@implementation CrapsViewController

@synthesize diceView1, diceView2;
@synthesize anteSlider;
@synthesize rollButton;
@synthesize totalLabel, pointLabel, anteLabel, balanceLabel;
@synthesize highScoreLabel;

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

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


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


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

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

- (void)displayHighScore
{
	int highScore = self.model.highScore;
	self.highScoreLabel.text = [NSString stringWithFormat:@"High Score: $%d", highScore];
}

- (void)updateDisplay
{
	[self displayPoint];
	[self displayBalance];
	[self displayAnte];
	[self displayTotal];
	[self displayHighScore];
	
	// don't use delegate
	self.diceView1.dieFace = [[self.model.dice objectAtIndex:0] intValue];
	self.diceView2.dieFace = [[self.model.dice objectAtIndex:1] intValue];
	
	[self.diceView1 setNeedsDisplay];
	[self.diceView2 setNeedsDisplay];	
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (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];
}


#pragma mark -
#pragma mark Shake Event Handling
- (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];
}


#pragma mark -
#pragma mark Rotation

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	//Return YES for supported orientations
	return YES;
}


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	if (UIInterfaceOrientationIsPortrait(fromInterfaceOrientation)) {
		CGPoint moveTo = CGPointMake(self.pointLabel.center.x, 
									 self.pointLabel.center.y - self.pointLabel.bounds.size.height);
		totalLabel.center = moveTo;
	} else {
		CGPoint moveTo = CGPointMake(20 + self.totalLabel.bounds.size.width/2.0, self.pointLabel.center.y);
		totalLabel.center = moveTo;
	}
}


#pragma mark -
#pragma mark DiceViewDelegate
- (int)diceValueForDiceView:(DiceView*)diceView 
{
	if (diceView == diceView1) {
		return [[model.dice objectAtIndex:0] intValue];
	} else {
		return [[model.dice objectAtIndex:1] intValue];
	}

}


@end
