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

#import "RCTLSUserCoordinator.h"
#import <LightspeedSDK/LightspeedSDK-Swift.h>
#import "LSBooster+RCTNativeModel.h"
#import "LSPanelistAchievementHistory+RCTNativeModel.h"
#import "LSPanelistAchievementSummary+RCTNativeModel.h"
#import "LSOpportunities+RCTNativeModel.h"
#import "LSUserProfile+RCTNativeModel.h"
#import "LSPerksLogin+RCTNativeModel.h"
#import "LSHitOrMissInfo+RCTNativeModel.h"
#import "RCTConvert+LSHitOrMissState.h"

NSString *const UserCoordinatorProfileUpdateEvent = @"UserProfileUpdatedEvent";

@implementation RCTLSUserCoordinator
RCT_EXPORT_MODULE(LSUserCoordinator);

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

- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}

- (NSArray<NSString *> *)supportedEvents {
    return @[UserCoordinatorProfileUpdateEvent];
}

- (NSDictionary *)constantsToExport {
    return @{ @"HitOrMissStateNotAvailable" : @(HitOrMissStateNotAvailable),
              @"HitOrMissStateAvailable" : @(HitOrMissStateAvailable),
              @"HitOrMissStateContentNotAvailable" : @(HitOrMissStateContentNotAvailable),
              @"UserUpdatedNotification":UserCoordinatorProfileUpdateEvent
              };
}

- (void)startObserving {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleProfileUpdatedNotification:)
                                                 name:[LSUserCoordinator UserProfileUpdatedNotificationName]
                                               object:nil];
}

- (void)stopObserving
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)handleProfileUpdatedNotification:(NSNotification *)notification {
    [self sendEventWithName:UserCoordinatorProfileUpdateEvent body:notification.userInfo];
}

+ (BOOL)requiresMainQueueSetup
{
    return YES;
}

RCT_REMAP_METHOD(getBoosterInfo, resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSUserCoordinator sharedInstance] getBoosterInfo:^(LSBooster * _Nullable booster, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([booster reactNativeRepresentation]);
        }
    }];
}

RCT_REMAP_METHOD(getAchievementHistory, resolverH:(RCTPromiseResolveBlock)resolver rejecterH:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSUserCoordinator sharedInstance] getAchievementHistory:^(LSPanelistAchievementHistory * _Nullable history, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([history reactNativeRepresentation]);
        }
    }];
}


RCT_REMAP_METHOD(getAchievementSummary, resolverS:(RCTPromiseResolveBlock)resolver rejecterS:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSUserCoordinator sharedInstance] getAchievementSummary:^(LSPanelistAchievementSummary * _Nullable summary, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([summary reactNativeRepresentation]);
        }
    }];
}

RCT_EXPORT_METHOD(getOpportunities:(BOOL)locationEnabled resolver:(RCTPromiseResolveBlock)resolver rejected:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSUserCoordinator sharedInstance] getOpportunities:locationEnabled completion:^(LSOpportunities * _Nullable opportunities, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([opportunities reactNativeRepresentation]);
        }
    }];
}

RCT_REMAP_METHOD(getPanelistProfile, resolverP:(RCTPromiseResolveBlock)resolver rejecterP:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSUserCoordinator sharedInstance] getPanelistProfile:^(LSUserProfile * _Nullable profile, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([profile reactNativeRepresentation]);
        }
    }];
}

RCT_EXPORT_METHOD(updatePanelistProfile:(LSUpdateProfileRequest * _Nonnull)request resolver:(RCTPromiseResolveBlock)resolver rejected:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSUserCoordinator sharedInstance] updatePanelistProfile:request completion:^(LSUpdatePanelistProfile * _Nullable profile, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver(@(YES));
        }
    }];
}

RCT_EXPORT_METHOD(perksLogin:(NSInteger)totalPoints resolver:(RCTPromiseResolveBlock)resolver rejecter:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSUserCoordinator sharedInstance] perksLogin:totalPoints completion:^(LSPerksLogin * _Nullable perks, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([perks reactNativeRepresentation]);
        }
    }];
}

RCT_REMAP_METHOD(getHitOrMissInfo, resolverHM:(RCTPromiseResolveBlock)resolver rejecterHM:(RCTPromiseRejectBlock)rejecter) {
    [[RCTLSUserCoordinator sharedInstance] getHitOrMissInfoWithCompletion:^(LSHitOrMissInfo * _Nullable info, NSError * _Nullable error) {
        if (error) {
            rejecter([@(error.code) stringValue], error.localizedFailureReason, error);
        }else {
            resolver([info reactNativeRepresentation]);
        }
    }];
}@synthesize description;

@synthesize hash;

@synthesize superclass;


@end

