//
//  iViewController.m
//  oscSenderExample
//
//  Created by Robert Tubb on 07/01/2013.
//
//
#include "presetManager.h"
#import "PresetAlertViewController.h"


extern PresetManager presetManager;
extern EventLogger eventLogger;

@implementation PresetAlertViewController
- (PresetAlertViewController *) init{
    [super init];
    self = [super init];
    if (self) {
        // Custom initialization
        
        self.alertShowing = NO;
        
    }
    return self;
    
}
- (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];
    self.alertShowing = YES;
    [alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    NSLog(@"Preset save %@", [[alertView textFieldAtIndex:0] text]);
    if(buttonIndex == 0){
        // cancel
        NSLog(@"preset save was cancelled");
        self.alertShowing = NO;
    }else{
        // save it
        self.alertShowing = NO;
        if([[[alertView textFieldAtIndex:0] text] isEqualToString:@"newuser"]){
            // call new user stufff
            eventLogger.newUser();
        }else{
            presetManager.addPreset([[[alertView textFieldAtIndex:0] text] cStringUsingEncoding:NSASCIIStringEncoding]);
        }
    }
}


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



@end


// global? again.
PresetAlertViewController *presetAlertViewController = [[PresetAlertViewController alloc] init];
