//
//  RCTLSServicesCoordinator.m
//  ReactLightspeedSDK
//
//  Created by marc matta on 11/25/16.
//  Copyright © 2016 com.lightspeed. All rights reserved.
//

#import "RCTLSServicesCoordinator.h"
#import <LightspeedSDK/LightspeedSDK-Swift.h>
#import "LSMultipleAchievements+RCTNativeModel.h"
#import "LSDeviceService+RCTNativeModel.h"
#import "RCTLSUtils.h"

@implementation RCTLSServicesCoordinator
RCT_EXPORT_MODULE(LSServicesCoordinator);

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


- (NSDictionary<NSString *,id> *)constantsToExport {
    return @{
             @"service_location" : [LSServicesCoordinator LocationServices],
             @"service_notification": [LSServicesCoordinator NotificationServices]
             };
}

+ (BOOL)requiresMainQueueSetup
{
    return YES;
}

RCT_EXPORT_METHOD(postStatuses:(NSArray <LSDeviceService *>*)statuses resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSServicesCoordinator sharedInstance] postStatuses:statuses completion:^(LSMultipleAchievements * _Nullable achievements, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([achievements reactNativeRepresentation]);
        }
    }];
}

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

@end
