//
//  ViewController.m
//  Gestures
//
//  David J. Malan
//  Harvard University
//  malan@harvard.edu 
//
//  Demonstrates gestures.
//

#import "ViewController.h"


@interface ViewController ()

// private properties
@property (assign, nonatomic, readwrite) BOOL alertInProgress;
@property (nonatomic, readwrite, weak) IBOutlet UIImageView *imageView;
@property (assign, nonatomic, readwrite) int index;
@property (nonatomic, readwrite, strong) NSArray *tommies;

// private methods
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
- (void)handleLongPress:(UILongPressGestureRecognizer *)sender;
- (void)handleSwipe:(UISwipeGestureRecognizer *)sender;

@end


@implementation ViewController

// synthesized properties
@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];
    }
}

- (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]];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        // keep track of alerts
        self.alertInProgress = NO;
    
        // prepare Tommies
        self.tommies = [[NSArray alloc] initWithObjects:@"tommy1.jpg", @"tommy2.jpg", @"tommy3.jpg", nil];
        self.index = 0;
    }
    return self;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // load image
    self.imageView.image = [UIImage imageNamed:[self.tommies objectAtIndex:self.index]];
    
    // listen for long press
    UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];	
    [self.imageView addGestureRecognizer:longpressGesture];
    
	// listen for right swipe
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.imageView addGestureRecognizer:swipeGesture];
    
    // listen for left swipe
    swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.imageView addGestureRecognizer:swipeGesture];
}

@end
