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

#import "RCTLSSurveyCoordinator.h"
#import <LightspeedSDK/LightspeedSDK-Swift.h>
#import "LSSurvey+RCTNativeModel.h"
#import "LSGeolocationSurvey+RCTNativeModel.h"
#import "LSCompleteSurvey+RCTNativeModel.h"
#import "LSReserveSurvey+RCTNativeModel.h"
#import "LSSurveyStart+RCTNativeModel.h"
#import "LSSurveyInfo+RCTNativeModel.h"
#import "LSSurveyActivity+RCTNativeModel.h"
#import "RCTConvert+SurveyStatus.h"
#import "RCTConvert+LocationSurveyType.h"
#import "RCTConvert+LSHitOrMissState.h"
#import "RCTConvert+AwardType.h"
#import "RCTLSUtils.h"

@implementation RCTLSSurveyCoordinator
RCT_EXPORT_MODULE(LSSurveyCoordinator);

- (NSDictionary *)constantsToExport {
    return @{ @"SurveyTypeBoth" : @"SurveyTypeBoth",
              @"SurveyTypeValidated" : @"SurveyTypeValidated",
              @"SurveyTypePassive" : @"SurveyTypePassive",
              @"surveyAppTypeMobileAppNA" : @"surveyAppTypeMobileAppNA",
              @"surveyAppTypeMobileAppOnly" : @"surveyAppTypeMobileAppOnly",
              @"surveyAppTypeMobileAppEnabled" : @"surveyAppTypeMobileAppEnabled",
              @"SurveyStatusNone" : @"SurveyStatusNone",
              @"SurveyStatusCompleted" : @"SurveyStatusCompleted",
              @"SurveyStatusScreened" : @"SurveyStatusScreened",
              @"SurveyStatusQuotaFull" : @"SurveyStatusQuotaFull",
              @"SurveyStatusStudyClosed" : @"SurveyStatusStudyClosed",
              @"SurveyStatusAlreadyTaken" : @"SurveyStatusAlreadyTaken",
              @"SurveyStatusStudyTemporarilyClosed" : @"SurveyStatusStudyTemporarilyClosed",
              @"AwardTypePoints" : @"AwardTypePoints",
              @"AwardTypeSweeps" : @"AwardTypeSweeps",
              @"AwardTypeUnknown" : @"AwardTypeUnknown"
              };
}

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

+ (BOOL)requiresMainQueueSetup
{
    return YES;
}

RCT_REMAP_METHOD(getSurveys, resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] getSurveys:^(NSArray<LSSurvey *> * _Nullable surveys, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([RCTLSUtils LSArrayFromNativeModels:surveys]);
        }
    }];
}

RCT_EXPORT_METHOD(getSurveysUsingCoordinates:(double)latitude longitude:(double)longitude resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] getSurveysUsingCoordinates:latitude longitude:longitude completion:^(NSArray<LSSurvey *> * _Nullable surveys, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([RCTLSUtils LSArrayFromNativeModels:surveys]);
        }
    }];
}

RCT_EXPORT_METHOD(validatedSurveyStart:(NSInteger)surveyId latitude:(double)latitude longitude:(double)longitude geoLocationId:(NSInteger)geoLocationId resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] validatedSurveyStart:surveyId latitude:latitude longitude:longitude geoLocationId:geoLocationId completion:^(LSSurveyStart * _Nullable surveyStart, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([surveyStart reactNativeRepresentation]);
        }
    }];
}

RCT_EXPORT_METHOD(locationSurveyStart:(NSInteger)surveyId latitude:(double)latitude longitude:(double)longitude resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] locationSurveyStart:surveyId latitude:latitude longitude:longitude completion:^(LSSurveyStart * _Nullable surveyStart, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([surveyStart reactNativeRepresentation]);
        }
    }];
}

RCT_EXPORT_METHOD(surveyStart:(NSInteger)surveyId resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] surveyStart:surveyId completion:^(LSSurveyStart * _Nullable surveyStart, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([surveyStart reactNativeRepresentation]);
        }
    }];
}

RCT_EXPORT_METHOD(surveyComplete:(NSInteger)surveyStartTrackId surveyStatus:(SurveyStatus)surveyStatus resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] surveyComplete:surveyStartTrackId surveyStatus:surveyStatus completion:^(BOOL completed, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver(@YES);
        }
    }];
}


RCT_EXPORT_METHOD(reserveSurvey:(LSSurvey *)survey resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] reserveSurvey:survey completion:^(LSReserveSurvey * _Nullable reserveSurvey, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([reserveSurvey reactNativeRepresentation]);
        }
    }];
}

RCT_EXPORT_METHOD(getLocationSurveys:(double)latitude longitude:(double)longitude surveyType:(LocationSurveyType)surveyType boundingBox:(double)boundingBox maxResults:(NSInteger)maxResults resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] getLocationSurveys:latitude longitude:longitude surveyType:surveyType boundingBox:boundingBox maxResults:maxResults completion:^(NSArray<LSGeolocationSurvey *> * _Nullable surveys, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([RCTLSUtils LSArrayFromNativeModels:surveys]);
        }
    }];
}

RCT_EXPORT_METHOD(getSurveyInfo:(NSInteger)surveyId resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] getSurveyInfo:surveyId completion:^(LSSurveyInfo * _Nullable surveyInfo, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([surveyInfo reactNativeRepresentation]);
        }
    }];
}

RCT_REMAP_METHOD(getSurveyActivity, resolverSA:(RCTPromiseResolveBlock)resolver rejecterSA:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSSurveyCoordinator sharedInstance] getSurveyActivityWithCompletion:^(NSArray<LSSurveyActivity *> * _Nullable activities, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([RCTLSUtils LSArrayFromNativeModels:activities]);
        }
    }];
}
@end
