//
//  TimedSessionController.m
//  sonicZoom
//
//  Created by Robert Tubb on 18/02/2013.
//
//
#import "testApp.h"
#import "TimedSessionController.h"
@interface TimedSessionController()
    // private stuff
@end

@implementation TimedSessionController

    int orders[6][3] = {{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0}};
    int theOrder;
    int scount;

    NSArray *alertMessages = [NSArray arrayWithObjects:
                      @"Thanks, now try the sliders only",
                      @"Thanks, now try the sliders and the Zoomer together",
                      @"Thanks, now try the zoomer only",
                      @"Thanks, now please take the questionnaire",nil];

// nicer if it was initWithAppRef ?
- (void)setAppRef:(id)theOFApp{
    self.theOFAppRef = theOFApp;
    theOrder = arc4random() % 6;
    NSLog(@"the order: %d", theOrder);
    
    scount = 0;
}
-(void)startTimer{
    // SHOW FIRST PROMPT
    NSString *themessage;
    themessage = [alertMessages objectAtIndex:orders[theOrder][0]];
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Thanks!"
                                                     message:themessage
                                                    delegate:self
                                           cancelButtonTitle:@"Continue"
                                           otherButtonTitles:nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
    [alert release];

}
-(void)resetTime:(int)newTime{
    
}

// ALERT


-(void)showNextPrompt{
    NSString *themessage;
    scount++;
    ((testApp *)self.theOFAppRef)->moveVel = TwoVector(); // stops movement
    if(scount < 3){
        themessage = [alertMessages objectAtIndex:orders[theOrder][scount] ];
    }else{
        themessage = [alertMessages objectAtIndex:3 ];
    }
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Thanks!"
                                                     message:themessage
                                                    delegate:self
                                           cancelButtonTitle:@"Continue"
                                           otherButtonTitles:nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
    [alert release];
    
}

-(void)cancelTimers{
    scount = 0;
    if(self.theCurrentTimer){
        [self.theCurrentTimer invalidate];
        self.theCurrentTimer = nil;
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    NSLog(@"OK TO NEXT VIEW");
    
    if(scount < 3){
        NSInteger next = orders[theOrder][scount];
        
        ((testApp *)self.theOFAppRef)->interfaceSelected(next);
        
        self.theCurrentTimer = [NSTimer scheduledTimerWithTimeInterval:SECONDS_PER_INTERFACE target:self selector:@selector(showNextPrompt) userInfo:nil repeats:NO];
        
    }else{
        ((testApp *)self.theOFAppRef)->showQuestionnaire();
        scount = 0; // incase we try again
        self.theCurrentTimer = nil;
    }
    
}


@end
