//
//  RCTLSQuestionCoordinator.m
//  ReactLightspeedSDK
//
//  Created by marc matta on 10/13/16.
//  Copyright © 2016 com.lightspeed. All rights reserved.
//

#import "RCTLSQuestionsCoordinator.h"
#import <LightspeedSDK/LightspeedSDK-Swift.h>
#import "RCTLSUtils.h"
#import "LSQuestionList+RCTNativeModel.h"
#import "LSSingleAchievement+RCTNativeModel.h"
#import "LSQuestionList+RCTNativeModel.h"

@implementation RCTLSQuestionsCoordinator
RCT_EXPORT_MODULE(LSQuestionsCoordinator);

- (NSDictionary *)constantsToExport {
    return @{ @"QuestionTypeSingleAnswer": LSQuestionType.single,
              @"QuestionTypeMultipleAnswer": LSQuestionType.multi,
              @"InquestTypeProfiler": LSInquestType.profiler,
              @"InquestTypeTt": LSInquestType.tt,
              @"AnswersSequenceStatic":LSAnswersSequence.staticType,
              @"AnswersSequenceShuffle": LSAnswersSequence.shuffleType,
              @"DailyQuizStatusNotCompleted": DailyQuizStatusStringFromValue(@(DailyQuizStatusNotCompleted)),
              @"DailyQuizStatusCompleted": DailyQuizStatusStringFromValue(@(DailyQuizStatusCompleted)),
              @"DailyQuizStatusLost": DailyQuizStatusStringFromValue(@(DailyQuizStatusLost)),
              @"QuestionsStatusAvailable": QuestionsStatusFromValue(QuestionsStatusAvailable),
              @"QuestionsStatusQuestionsCompleted": QuestionsStatusFromValue(QuestionsStatusQuestionsCompleted),
              @"QuestionsStatusNoQuestionsAvailable": QuestionsStatusFromValue(QuestionsStatusQuestionsCompleted),
              @"RequestQuestionTypeIRCheck":LSQuestionCoordinator.IRCheckQuestionType
              };
}

static LSQuestionCoordinator *sharedInstance;
+ (LSQuestionCoordinator *)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [LSQuestionCoordinator new];
    });
    
    return sharedInstance;
}

+ (BOOL)requiresMainQueueSetup
{
    return YES;
}

RCT_EXPORT_METHOD(getQuestions:(BOOL)restartChallenge questionType:(NSString * _Nullable)questionType resolverq:(RCTPromiseResolveBlock)resolver rejecterq:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSQuestionsCoordinator sharedInstance] getQuestions:restartChallenge questionType:questionType completion:^(LSQuestionList * _Nullable questions, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([questions reactNativeRepresentation]);
        }
    }];
}

RCT_EXPORT_METHOD(submitAnswers:(NSArray<QuestionAnswer *> *)answers answerType:(NSString * _Nonnull)answerType lastQuestion:(id _Nullable)lastQuestion redeemCredit:(id _Nullable)redeemCredit resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    
    [[RCTLSQuestionsCoordinator sharedInstance] submitAnswers:answers answerType:answerType lastQuestion:lastQuestion redeemCredit:redeemCredit completion:^(LSSingleAchievement * _Nullable achievement, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([achievement reactNativeRepresentation]);
        }
    }];
    
}
@end
