//
//  iViewController.m
//  oscSenderExample
//
//  Created by Robert Tubb on 07/01/2013.
//
//
#include "testApp.h"
#include "eventLogger.h"

#import "usernameAlertViewController.h"


extern EventLogger eventLogger;

@implementation UsernameAlertViewController

-(void)showUserNamePrompt{

    
    self.alert = [[UIAlertView alloc] initWithTitle:@"Hello!"
                                                     message:@"Please enter your name:"
                                                    delegate:self
                                                    cancelButtonTitle:@"Continue"
                                                    otherButtonTitles:nil];
    self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * alertTextField = [self.alert textFieldAtIndex:0];
    [alertTextField setDelegate:self];
    alertTextField.keyboardType = UIKeyboardTypeDefault;
    alertTextField.placeholder = @"Username";
    [self.alert show];
    //[self.alert release];

    
}
-(void)setAppRef:(id)theAppRef{
    self.theOFAppRef = theAppRef;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]);
    NSString *userName = [[alertView textFieldAtIndex:0] text];
    if ([userName isEqualToString:@""]){
        [self showUserNamePrompt];
        return;
    }
    eventLogger.setUsername([userName cStringUsingEncoding:NSASCIIStringEncoding]);
    
    // call some start func
    ((testApp *)self.theOFAppRef)->startTheTests();
    // rememebr to change return func too!

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    NSLog(@"Entered: %@",[[self.alert textFieldAtIndex:0] text]);
    NSString *userName = [[self.alert textFieldAtIndex:0] text];
    if ([userName isEqualToString:@""]){
        [self showUserNamePrompt];
        
    }else{
        eventLogger.setUsername([userName cStringUsingEncoding:NSASCIIStringEncoding]);
        ((testApp *)self.theOFAppRef)->startTheTests();
        
    }
    [self.alert dismissWithClickedButtonIndex:self.alert.firstOtherButtonIndex animated:YES];
    return YES;
}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    UITextField *textField = [alertView textFieldAtIndex:0];
    if ([textField.text length] == 0)
    {
        return NO;
    }
    return YES;
}


@end


// global. again.

