//
//  TopButtonViewController.m
//  sonicZoom
//
//  Created by Robert Tubb on 31/01/2013.
//
//

#import "TopButtonViewController.h"
#import "testApp.h"
#import "presetManager.h"

extern PresetManager presetManager;


@interface TopButtonViewController ()

@end

@implementation TopButtonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.playButton.enabled = true;
    self.pauseButton.enabled = false;
    self.qButton.enabled = true;
    self.newUserButton.enabled = true;
    //------------------------
    // Get the reference to the current toolbar buttons
    NSMutableArray *toolbarButtons = [self.toolbarItems mutableCopy];
    
    // This is how you remove the button from the toolbar and animate it
    [toolbarButtons removeObject:self.qButton];
    [self setToolbarItems:toolbarButtons animated:YES];
    
    // This is how you add the button to the toolbar and animate it
    /*
    if (![toolbarButtons containsObject:self.qButton]) {
        [toolbarButtons addObject:self.qButton];
        [self setToolbarItems:toolbarButtons animated:YES];
    }
    */
    
}

- (void)enableQuestionButton{
    self.qButton.enabled = true;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

- (IBAction)playPressed:(id)sender {
    ((testApp *)self.theOFAppRef)->seqStartStop(true);
    self.playButton.enabled = false;
    self.pauseButton.enabled = true;
}

- (IBAction)pausePressed:(id)sender {
    ((testApp *)self.theOFAppRef)->seqStartStop(false);
    self.pauseButton.enabled = false;
    self.playButton.enabled = true;
}

- (IBAction)savePressed:(id)sender {
    cout << "SAVE PRESET\n";
    presetManager.showNameDialog();
}

- (IBAction)lockSequencePressed:(id)sender {
    UIBarButtonItem *button = (UIBarButtonItem *)sender;
    if([button.title isEqualToString:@"Lock Sequence (X)"]){
        ((testApp *)self.theOFAppRef)->lockSequencerPressed(true);
        button.title = @"Unlock Sequence (X)";
    }else if([button.title isEqualToString:@"Unlock Sequence (X)"]){
        ((testApp *)self.theOFAppRef)->lockSequencerPressed(false);
        button.title = @"Lock Sequence (X)";
    }else{
        NSLog(@"button title error");
    }
}

- (IBAction)lockSynthPressed:(id)sender {
    UIBarButtonItem *button = (UIBarButtonItem *)sender;
    if([button.title isEqualToString:@"Lock Synth (Y)"]){
        ((testApp *)self.theOFAppRef)->lockSynthPressed(true);
        button.title = @"Unlock Synth (Y)";
    }else if([button.title isEqualToString:@"Unlock Synth (Y)"]){
        ((testApp *)self.theOFAppRef)->lockSynthPressed(false);
        button.title = @"Lock Synth (Y)";
    }else{
        NSLog(@"button title error");
    }
}

- (IBAction)qPressed:(id)sender {
    ((testApp *)self.theOFAppRef)->showQuestionnaire();
}

- (IBAction)randomise:(id)sender {
    ((testApp *)self.theOFAppRef)->randomise();
}

- (IBAction)helpPressed:(id)sender {
    ((testApp *)self.theOFAppRef)->showHelp();

}

- (IBAction)newUser:(id)sender {
    ((testApp *)self.theOFAppRef)->setupNewUser();
}

- (IBAction)show:(id)sender
{
    self.theOFAppRef = sender;
    self.view.hidden = NO;
}

/*
 helpViewController	= [[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle:nil];
 [ofxiPhoneGetGLParentView() addSubview:helpViewController.view];
 [helpViewController hide:(id)this];
 */
- (void)dealloc {
    [_playButton release];
    [_pauseButton release];
    [_qButton release];

    [_newUserButton release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setPlayButton:nil];
    [self setPauseButton:nil];
    [self setQButton:nil];
    [self setNewUserButton:nil];
    [super viewDidUnload];
}
@end
