//
//  GesturesViewController.m
//  Gestures
//
//  David J. Malan
//  Harvard University
//  dmalan@harvard.edu
//
//  Demonstrates gestures.
//

#import "GesturesViewController.h"

@implementation GesturesViewController

@synthesize alertInProgress=_alertInProgress;
@synthesize imageView=_imageView;
@synthesize index=_index;
@synthesize tommies=_tommies;

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    self.alertInProgress = NO;
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
{
    if (!self.alertInProgress) {
        self.alertInProgress = YES;
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hey!" 
                                                            message:@"Stop that." 
                                                           delegate:self 
                                                  cancelButtonTitle:@"Fine" 
                                                  otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)sender
{
    // handle swipe
    UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *)sender direction];	
    switch (direction) {
            
            // ignore up, down
        case UISwipeGestureRecognizerDirectionUp:
        case UISwipeGestureRecognizerDirectionDown:
            break;
            
            // left
        case UISwipeGestureRecognizerDirectionLeft:
            self.index = (self.index + 1) % [self.tommies count];
            break;
            
            // right
        case UISwipeGestureRecognizerDirectionRight:
            self.index = (self.index + [self.tommies count] - 1) % [self.tommies count];
            break;           
    }
    
    // update Tommy
    self.imageView.image = [UIImage imageNamed:[self.tommies objectAtIndex:self.index]];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // keep track of alerts
    self.alertInProgress = NO;

    // prepare Tommies
    self.tommies = [[NSArray alloc] initWithObjects:@"tommy1.jpg", @"tommy2.jpg", @"tommy3.jpg", nil];
    self.index = 0;

    // listen for long press
    UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];	
    [self.imageView addGestureRecognizer:longpressGesture];
    [longpressGesture release];
    
	// listen for right swipe
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.imageView addGestureRecognizer:swipeGesture];
    [swipeGesture release];

    // listen for left swipe
    swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.imageView addGestureRecognizer:swipeGesture];
    [swipeGesture release];
}

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

@end
