//
//  animateViewController.m
//  animate
//
//  Created by Judy Franklin on 10/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "animateViewController.h"


@implementation animateViewController
@synthesize boxView;

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
    scaleFactor = 2;
    angle = 180;
    count = 0;
    UIImage * boxImage = [UIImage imageNamed:@"iPhoneTuring.jpg"];
    
 
   
    // construct the box with statements, not with dragndrop

     CGRect frameRect = CGRectMake(20, 20, 150, 130);
      
    
    boxView = [[UIView alloc] initWithFrame:frameRect];
    boxView.contentMode = UIViewContentModeScaleAspectFit;
    
//    boxView.backgroundColor = [UIColor blueColor];
    boxView.backgroundColor = [UIColor colorWithPatternImage:boxImage];
    
    //boxImage.frame=frameRect;
    
    NSString *yahPath3 = [[NSBundle mainBundle] 
                            pathForResource:@"YahThree" ofType:@"aiff"];
    NSString *yahPath5 = [[NSBundle mainBundle] 
                             pathForResource:@"YahFive" ofType:@"aiff"];
    NSURL *yahURL3 = [NSURL fileURLWithPath:yahPath3];
    NSURL *yahURL5 = [NSURL fileURLWithPath:yahPath5];
    
    AudioServicesCreateSystemSoundID((CFURLRef)yahURL3, &yahSoundID3);
    AudioServicesCreateSystemSoundID((CFURLRef)yahURL5, &yahSoundID5);
    
    [self.view addSubview:boxView];
}


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
// ha-hah, ut we are nib-less!!
 
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

/* When the user touches the screen the blue box needs to move from its current location to the location of the touch. During this motion, the box will rotate 180 degrees and change in size. 
 */

/* For the purposes of this example we want to initiate the animation at the point that the user’s finger is lifted from the screen so need to implement the touchesEnded method 
 */
-(void)hi:(NSString *) instr
{
    NSLog(@"Is this ok? %@",instr);
    NSLog(instr);
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];  // where the touch took place in the view passed in...in this case this view (self)
    // The animation block is then started and the current class declared as the delegate.
    //[self hi];
    //NSString *teststring = [NSString string];
    NSString *teststring = @"there!";
    count++;
    [self hi:teststring];
    [teststring release];
    [UIView beginAnimations:nil context:nil];  // start of animation block
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:2];    // 2 sec. duration
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; // slow at end
    CGAffineTransform scaleTrans = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
    CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(angle * M_PI / 180);
    boxView.transform = CGAffineTransformConcat(scaleTrans, rotateTrans);
    // After this, Ternary operators are then used to switch the scale and rotation angle variables ready for the next touch. In other words, after rotating 180 degrees on the first touch the view will need to be rotated to 360 degrees on the next animation. Similarly, once the box has been scaled by a factor of 2 it needs to scale back to its original size on the next animation:
    angle = (angle == 180 ? 360 : 180);
    scaleFactor = (scaleFactor == 2 ? 1 : 2);
    // Finally, the location of the view is moved to the point on the screen where the touch occurred before the animation is committed:
    boxView.center = location;
    if(count > 4)
    {
        if(scaleFactor==1)
            AudioServicesPlaySystemSound(yahSoundID3);
        else
            AudioServicesPlaySystemSound(yahSoundID5);
    }
        // At bottom of touchesBegan
    [UIView commitAnimations];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
