//
//  iViewController.m
//  oscSenderExample
//
//  Created by Robert Tubb on 07/01/2013.
//
//

#import "iViewController.h"

extern EventLogger eventLogger;
extern PresetManager presetManager;
@implementation IViewController

-(void)showUserNamePrompt{

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

    
}


- (void)showPresetNamePrompt{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Save"
                                                     message:@"Enter preset name:"
                                                    delegate:self
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"Enter",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * alertTextField = [alert textFieldAtIndex:0];
    alertTextField.keyboardType = UIKeyboardTypeDefault;
    alertTextField.placeholder = @"Preset Name";
    [alert show];
    [alert release];
}
/*
- (void)showQuestionPrompt:(NSString *)question{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Save"
                                                     message:@"Are you mad?"
                                                    delegate:self
                                           cancelButtonTitle:@"Next"
                                           otherButtonTitles:@"Hell No",nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
    [alert release];
}
 */

// shheet - only one delegate fnc
// we're actually overriding alertViews function here?

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"Button: %d",buttonIndex);
    
    NSLog(@"Dialog title: %@",[alertView title]); // check right title!
    if([[alertView title] isEqualToString:@"Hello!"]){
        NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]);
        NSString *userName = [[alertView textFieldAtIndex:0] text];
        if ([userName isEqualToString:@""]){
            [self showUserNamePrompt:43];
            return;
        }
        eventLogger.setUsername([userName cStringUsingEncoding:NSASCIIStringEncoding]);
    } else if ([[alertView title] isEqualToString:@"Save"]) {
        NSLog(@"Preset save ");
        if(buttonIndex == 0){
            // cancel
            NSLog(@"preset save was cancelled");
        }else{
            // save it
            presetManager.addPreset([[[alertView textFieldAtIndex:0] text] cStringUsingEncoding:NSASCIIStringEncoding]);
        }
    } else {
        NSLog(@"Dialog title didn't match anything");
    }

}


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


@end


// global. again.
IViewController *iViewController = [[IViewController alloc] init];
