#import "MyConditionalTask.h"

@implementation MyConditionalTask

- (instancetype)init
{
    // I would not use hard coded strings for a production app, of course.
    ORKInstructionStep *introductoryStep = [[ORKInstructionStep alloc] initWithIdentifier:@"kIntroStep"];
    introductoryStep.title = @"A Survey with a Conditional Question";
    
    ORKStep *feverStep = [ORKQuestionStep questionStepWithIdentifier:@"kFeverIdentifier" title:@"Do you have a fever now?" answer:[ORKBooleanAnswerFormat booleanAnswerFormat]];
    
    // Was testing this out to see what would happen.
    ORKAnswerFormat *tempFormat = [ORKHealthKitQuantityTypeAnswerFormat answerFormatWithQuantityType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyTemperature] unit:[HKUnit degreeFahrenheitUnit] style:ORKNumericAnswerStyleDecimal];
    ORKQuestionStep *currentTempStep = [ORKQuestionStep questionStepWithIdentifier:@"kTemperature" title:@"What is your current body temperature?" answer:tempFormat];
    currentTempStep.placeholder = @"Fahrenheit";
    
    ORKStep *lastFeverDateStep = [ORKQuestionStep questionStepWithIdentifier:@"kLastFever" title:@"What was the date of your last fever?" answer:[ORKAnswerFormat dateAnswerFormat]];
    
    self = [super initWithIdentifier:@"kConditionalTask" steps:@[introductoryStep, feverStep, currentTempStep, lastFeverDateStep]];
    
    return self;
}

#pragma mark - ORKTask

- (nullable ORKStep *)stepAfterStep:(nullable ORKStep *)step withResult:(ORKTaskResult * __nonnull)result
{
    NSString *currentStepIdentifier = step.identifier;
    
    // I would not use hard coded strings for a production app, of course.
    if ([currentStepIdentifier isEqualToString:@"kFeverIdentifier"])
    {
        ORKStepResult *currentStepResult = [result stepResultForStepIdentifier:currentStepIdentifier];
        ORKQuestionResult *currentQuestionResult = (ORKQuestionResult *)currentStepResult.firstResult;
        
        if ([currentQuestionResult isKindOfClass:[ORKBooleanQuestionResult class]])
        {
            ORKBooleanQuestionResult *booleanResult = (ORKBooleanQuestionResult *)currentQuestionResult;
            NSNumber *booleanAnswer = booleanResult.booleanAnswer;
            
            if (booleanAnswer)
            {
                // Too simplistic for production, but good enough for this early testing.
                return booleanAnswer.boolValue ? self.steps[2] : self.steps[3];
            }
        }
    }
    
    // Fall-through
    return [super stepAfterStep:step withResult:result];
}

- (nullable ORKStep *)stepBeforeStep:(nullable ORKStep *)step withResult:(ORKTaskResult * __nonnull)result
{
    NSString *currentStepIdentifier = step.identifier;
    
    // I would not use hard coded strings for a production app, of course.
    if ([currentStepIdentifier isEqualToString:@"kLastFever"])
    {
        ORKStepResult *feverStepResult = [result stepResultForStepIdentifier:@"kFeverIdentifier"];
        ORKQuestionResult *feverQuestionResult = (ORKQuestionResult *)feverStepResult.firstResult;
        
        if ([feverQuestionResult isKindOfClass:[ORKBooleanQuestionResult class]])
        {
            ORKBooleanQuestionResult *booleanResult = (ORKBooleanQuestionResult *)feverQuestionResult;
            NSNumber *booleanAnswer = booleanResult.booleanAnswer;
            
            if (booleanAnswer)
            {
                // Too simplistic for production, but good enough for this early testing.
                return booleanAnswer.boolValue ? self.steps[2] : self.steps[1];
            }
        }
    }
    
    // Fall-through
    return [super stepBeforeStep:step withResult:result];
}

@end