#import "RookBackgroundModule.h"
#import "RookSDK/RookSDK-Swift.h"

@implementation RookBackgroundModule

RCT_EXPORT_MODULE()

// MARK: - Background
/**
 * Enable the background sync, this will automatically upload events and summaries
 * at the moment the user enables this feature.
 *
 * @param resolve A block called with a boolean value indicating whether the events were successfully sync.
 * @param reject A block called if an error occurs, such as Apple Health not being installed.
 */
RCT_EXPORT_METHOD(enableBackGroundUpdates: (RCTPromiseResolveBlock) resolve
                  reject:(RCTPromiseRejectBlock)reject) {

  [[RookBackGroundSync shared] enableBackGroundForSummaries];
  [[RookBackGroundSync shared] enableBackGroundForEvents];
  resolve(@(YES));
}

/**
 * Disable the background sync 
 *
 * @param resolve A block called with a boolean value indicating whether the events were successfully sync.
 * @param reject A block called if an error occurs, such as Apple Health not being installed.
 */
RCT_EXPORT_METHOD(disableBackGroundUpdates: (RCTPromiseResolveBlock) resolve
                  reject:(RCTPromiseRejectBlock)reject) {

    [[RookBackGroundSync shared] disableBackGroundForSummaries];
    [[RookBackGroundSync shared] disableBackGroundForEvents];
    resolve(@(YES));
}

/**
 * Enable background sync for summaries only 
 *
 * @param resolve A block called with a boolean value indicating whether the events were successfully sync.
 * @param reject A block called if an error occurs, such as Apple Health not being installed.
 */
RCT_EXPORT_METHOD(enableBackGroundEventUpdates: (RCTPromiseResolveBlock) resolve
                  reject:(RCTPromiseRejectBlock)reject) {

  [[RookBackGroundSync shared] enableBackGroundForEvents];
  resolve(@(YES));
}

/**
 * Disable the backGround sync just for events 
 *
 * @param resolve A block called with a boolean value indicating whether the events were successfully sync.
 * @param reject A block called if an error occurs, such as Apple Health not being installed.
 */
RCT_EXPORT_METHOD(disableBackGroundEventUpdates: (RCTPromiseResolveBlock) resolve
                  reject:(RCTPromiseRejectBlock)reject) {

    [[RookBackGroundSync shared] disableBackGroundForEvents];
}

/**
 * Check if the background sync for summaries are enabled 
 *
 * @param resolve A block called with a boolean value indicating whether the events were successfully sync.
 * @param reject A block called if an error occurs, such as Apple Health not being installed.
 */
RCT_EXPORT_METHOD(isBackGroundForSummariesEnable: (RCTPromiseResolveBlock) resolve
                  reject:(RCTPromiseRejectBlock)reject) {

    bool result = [[RookBackGroundSync shared] isBackGroundForSummariesEnable];
    resolve(@(result));
}

/**
 * Check if the background sync for events are enabled
 *
 * @param resolve A block called with a boolean value indicating whether the result.
 * @param reject A block called if an error occurs.
 */
RCT_EXPORT_METHOD(isBackGroundForEventsEnable: (RCTPromiseResolveBlock) resolve
                  reject:(RCTPromiseRejectBlock)reject) {

    bool result = [[RookBackGroundSync shared] isBackGroundForEventsEnable];
    resolve(@(result));
}

@end
