//
//  QuestionnaireViewController.m
//  oscSenderExample
//
//  Created by Robert Tubb on 16/01/2013.
//
//

#import "QuestionnaireViewController.h"

#import "testApp.h"

#pragma mark -
#pragma mark QuestionnaireViewController

@interface QuestionnaireViewController ()
    // the "model" is an array of questions 
@property (strong, nonatomic)       NSArray * questionArray;
@property (nonatomic)               NSInteger currentQuestionIndex;
@property (nonatomic, assign)       id theOFAppRef;

/*


*/

@end

@implementation QuestionnaireViewController

@synthesize picker;
@synthesize nextButton = _nextButton;
@synthesize questionArray = _questionArray;


//----------------------------------------------------------------
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        [self populateQuestionArray ];
        
    }
    return self;
}
- (void)setAppRef:(id)theOFApp{
    self.theOFAppRef = theOFApp;
    
}
//----------------------------------------------------------------
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.currentQuestionIndex = 0;
    
    // load question 1    
    [self loadQuestion:self.currentQuestionIndex];
    
    self.previousButton.hidden = NO; //  bother
    self.commentText.hidden = YES;
    self.finishButton.hidden = YES;
    self.nextButton.hidden = YES;
    
    self.lickertOptions.hidden = NO;
    self.interfacePreferenceOptions.hidden = YES;
}
//----------------------------------------------------------------
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//----------------------------------------------------------------
- (void)dealloc {

    [super dealloc];
}
//----------------------------------------------------------------
- (void)viewDidUnload {
    [self.questionArray release];
    [self setQuestionText:nil];
    [self setTitleText:nil];
    [self setFinishButton:nil];
    [self setNextButton:nil];
    [self setPreviousButton:nil];
    [self setCommentText:nil];
    [self setNumberChooser:nil];
    [self setInterfacePreferenceOptions:nil];
    [self setLickertOptions:nil];
    [self setPleaseAnswer:nil];
    [super viewDidUnload];
}
//----------------------------------------------------------------
-(IBAction)hide:(id)sender{
    // called when finish button hit
    // c++ call with NSArray argument??
    // load answers into a c++ vector;
    vector<int> answersArray;

    Question *q;
    
    for(int i=0;i<[self.questionArray count];i++){
        q = [self.questionArray objectAtIndex:i];
        answersArray.push_back(q.answer);
        
    }
    const char *userComments = [self.commentText.text cStringUsingEncoding: NSUTF8StringEncoding];
    [self.commentText resignFirstResponder];
    
	self.view.hidden = YES;
    ((testApp *)self.theOFAppRef)->questionnaireHidden(answersArray, userComments);
    
}

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

- (IBAction)nextQuestionPressed:(id)sender {
    // save answer ? no button did that hopefully
    
    // if last question show thanks
    // else go to next
    self.currentQuestionIndex++;
    if(self.currentQuestionIndex >= [self.questionArray count]){
        [self showThanks];
    }else{
        [self loadQuestion:self.currentQuestionIndex];

    }
}
//----------------------------------------------------------------
- (IBAction)previousQuestionPressed:(id)sender {
    self.currentQuestionIndex--;
    if(self.currentQuestionIndex < 0){
        // nothing
        self.currentQuestionIndex = 0;
    }else{
        [self loadQuestion:self.currentQuestionIndex];
    }
}

//----------------------------------------------------------------

- (void)showThanks{
    // hide next question button
    self.nextButton.hidden = YES;
    // hide selector
    self.picker.hidden = YES;
    self.previousButton.hidden = YES;
    self.finishButton.hidden = NO;
    
    self.lickertOptions.hidden = YES;
    self.interfacePreferenceOptions.hidden = YES;
    
    self.titleText.text = @"Thank you!";
    self.numberChooser.hidden = YES;
    self.commentText.hidden = NO;
    
    self.questionText.text = @"Thanks for helping science help you. Feel free to add further comments in the text box below, and then press 'finish' to go back and use the app.";
}

//----------------------------------------------------------------
- (void)loadQuestion:(NSInteger)questionIndex {
    // populate text fields with question
    NSString *qtitle;
    qtitle = [@"Question " stringByAppendingFormat:@"%d / %d",questionIndex+1, [self.questionArray count]];
    self.titleText.text = qtitle;
    
    Question *curQ = [self.questionArray objectAtIndex:self.currentQuestionIndex];
    
    self.questionText.text = curQ.questionText;
    
    
    // refresh picker view content
    //[picker reloadComponent:0];

    // show correct option number labels
    if(curQ.questionType == AGREE_DISAGREE){
        self.lickertOptions.hidden = NO;
        self.interfacePreferenceOptions.hidden = YES;
    }else if(curQ.questionType == SLIDERS_ZOOMER){
        self.lickertOptions.hidden = YES;
        self.interfacePreferenceOptions.hidden = NO;
    }

    //NSLog(@"Prev answer answerInt %d", curQ.answer);
    //[picker selectRow:2 inComponent:0 animated:YES];
    
    // what about unselecting segment?

}
// 1/3/13 removed q 6 and 15. now only 15 qs
//----------------------------------------------------------------
- (void)populateQuestionArray{
// potential leak
    self.questionArray = [NSArray arrayWithObjects:
                          [[Question alloc] initWithTextAndType:@"I am familiar with music software and sound synthesis.":AGREE_DISAGREE],
                          [[Question alloc] initWithTextAndType:@"The ability to retrace my steps using the history path was useful...":AGREE_DISAGREE],
                          [[Question alloc] initWithTextAndType:@"The best interface for discovering interesting sounds quickly was...":SLIDERS_ZOOMER],
                          [[Question alloc] initWithTextAndType:@"The best interface for fine tuning a sound was...":SLIDERS_ZOOMER],
                          [[Question alloc] initWithTextAndType:@"The correspondence between the sliders and the grid was understandable.":AGREE_DISAGREE],
                          [[Question alloc] initWithTextAndType:@"Scrolling a greater distance on the grid seemed to correspond to larger difference in the sound.":AGREE_DISAGREE],
                          [[Question alloc] initWithTextAndType:@"The interface that I felt more in control using was...":SLIDERS_ZOOMER],
                          [[Question alloc] initWithTextAndType:@"Being able to see previously saved presets laid on the grid is useful.":AGREE_DISAGREE],  //
                          [[Question alloc] initWithTextAndType:@"The interface that felt more creative was...":SLIDERS_ZOOMER],
                          [[Question alloc] initWithTextAndType:@"The range of sounds was too limited to be able to judge the eventual usefulness of the interface.":AGREE_DISAGREE],
                          [[Question alloc] initWithTextAndType:@"The interface better for generating new ideas was...":SLIDERS_ZOOMER], 
                          [[Question alloc] initWithTextAndType:@"The interface better for performing live would be...":SLIDERS_ZOOMER],
                          [[Question alloc] initWithTextAndType:@"The Zoomer was an improvement on just using a randomiser.":AGREE_DISAGREE],
                          [[Question alloc] initWithTextAndType:@"The combination of Zoomer and Sliders was better than either individually.":AGREE_DISAGREE],
                          [[Question alloc] initWithTextAndType:@"I enjoy 'happy accidents' in the creative process.":AGREE_DISAGREE],
                          [[Question alloc] initWithTextAndType:@"Overall, the interface I preferred using was...":SLIDERS_ZOOMER], // ??????
                          nil];
 // The zoomer seemed more appropriate to explore the synth sound controls (red) than the note sequence controls (blue)
// I enjoyed the sounds produced

}

//----------------------------------------------------------------
#pragma mark -
#pragma mark PickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}
//----------------------------------------------------------------
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{

    return NUM_CHOICES; // always 6
}
//----------------------------------------------------------------
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    Question *curQ = [self.questionArray objectAtIndex:self.currentQuestionIndex];

    // get array of answers from Question class
    NSArray * answers = [Question answersWithType:curQ.questionType];
    return [answers objectAtIndex:row];

}
//----------------------------------------------------------------
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    Question *curQ = [self.questionArray objectAtIndex:self.currentQuestionIndex];
    // set question answerArray
    curQ.answer = row;
    
    // chek wot we just rote
    
    NSLog(@"Answer: %d",curQ.answer);

}
//----------------------------------------------------------------
#pragma mark UITextViewDelegate functions
/*
 This answer was useful to me, but I was also looking for the callback function from the "Go" button, which I found is:
 - (BOOL) textFieldShouldReturn:(UITextField *)textField { // Customer code return YES; }
 
 You will need to send the UITextField delegate to your view controller for that to work.
 
 */
 - (BOOL) textFieldShouldReturn:(UITextField *)textField {
     // Customer code
     NSLog(@"RETURN DELEGATE");
     [self hide:self ];
     return NO;
 }
- (IBAction)answerChosen:(id)sender {
    self.pleaseAnswer.hidden = YES;
    UISegmentedControl *seg = (UISegmentedControl *)sender;
    Question *curQ = [self.questionArray objectAtIndex:self.currentQuestionIndex];
    // set question answerArray
    curQ.answer = seg.selectedSegmentIndex;
    
    // chek wot we just rote
    
    NSLog(@"Answer: %d",curQ.answer);
    
    // automatically go next q
    self.currentQuestionIndex++;
    if(self.currentQuestionIndex >= [self.questionArray count]){
        [self showThanks];
    }else{
        [self loadQuestion:self.currentQuestionIndex];
        
    }
    
}
@end // end implementation
//----------------------------------------------------------------
//----------------------------------------------------------------
