//
//  APMModule.m
//  RCTAPMBanner
//
//  Created by Vincent Ducastel on 09/08/2017.
//  Copyright © 2017 Facebook. All rights reserved.
//

#import "APMModule.h"
#import "APMBridgeUtils.h"

#import <React/RCTLog.h>
#import <React/RCTConvert.h>
#import <APMBanner/APMHeaders.h>

#define EVENT_ON_BADGE_CLICK @"onBadgeClick"

@implementation APMModule

RCT_EXPORT_MODULE();

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

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

//Init
RCT_EXPORT_METHOD(initAppWithPartnerID:(NSString*)partnerID withPartnerSecret:(NSString*)partnerSecret)
{
  [[APM sharedInstance] initAppWithPartnerID:partnerID withPartnerSecret:partnerSecret];
}

RCT_REMAP_METHOD(initAppWithAppID,
                 initAppWithPartnerID:(NSString*)partnerID withPartnerSecret:(NSString*)partnerSecret withAppId:(NSString*)appId)
{
  [[APM sharedInstance] initAppWithPartnerID:partnerID withPartnerSecret:partnerSecret withAppId:appId];
}

//Cycle de vie
RCT_EXPORT_METHOD(refreshSDK)
{
  [[APM sharedInstance] viewDidAppear:YES];
}

//Services
RCT_EXPORT_METHOD(removeActionWithActionName:(NSString*)actionName)
{
  [[APM sharedInstance] removeActionWithActionName:actionName];
}

RCT_EXPORT_METHOD(selectClassID:(NSString*)classID)
{
  [[APM sharedInstance] selectClassID:classID];
}

RCT_EXPORT_METHOD(triggerAction:(NSString*)actionName)
{
  [[APM sharedInstance] triggerAction:actionName];
}

//Badge
RCT_EXPORT_METHOD(showBadge)
{
  [[APM sharedInstance] showBadge];
}

RCT_EXPORT_METHOD(hideBadge)
{
  [[APM sharedInstance] hideBadge];
}

RCT_EXPORT_METHOD(setOnBadgeClickListener:(NSInteger)badge)
{
    @try
    {
        if(badge == 0)
            [[APM sharedInstanceBadgeManager] setOnBadgeClick:nil];
        else
        {
            [[APM sharedInstanceBadgeManager] setOnBadgeClick:^BOOL(NSInteger i) {
                
                NSMutableDictionary* params = [NSMutableDictionary dictionary];
                [params setObject:@(i) forKey:@"badge"];
                [self sendEventWithName:EVENT_ON_BADGE_CLICK body:params];
                
                if(badge == i)
                    return YES;
                return NO;
            }];
        }
    }
    @catch (NSException *exception)
    {
        [APMBridgeUtils errorFromException:exception]; //Only print the error
    }
}

//Big Badge
RCT_EXPORT_METHOD(isOpenBigBadge:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
  resolve(@([[APM sharedInstance] isOpenBigBadge]));
}

RCT_EXPORT_METHOD(openBigBadge)
{
  [[APM sharedInstance] openBigBadge];
}

RCT_EXPORT_METHOD(closeBigBadge:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    @try
    {
        [[APM sharedInstance] closeBigBadge:^{
            resolve(nil);
        }];
    }
    @catch (NSException *exception)
    {
        NSError* error = [APMBridgeUtils errorFromException:exception];
        reject([NSString stringWithFormat:@"%li", (long)error.code], error.localizedDescription, error);
    }
  
}

//Getter and Setter
RCT_EXPORT_METHOD(setBaseUrl:(NSString*)baseUrl)
{
  [[APM sharedInstance] setBaseUrl:baseUrl];
}

RCT_EXPORT_METHOD(getBaseUrl:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
  resolve([[APM sharedInstance] baseUrl]);
}

RCT_EXPORT_METHOD(setDebugMode:(BOOL)debugMode)
{
  [[APM sharedInstance] setDebugMode:debugMode];
}

RCT_EXPORT_METHOD(isDebugMode:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
  resolve(@([[APM sharedInstance] debugMode]));
}

RCT_EXPORT_METHOD(setGeolocEnabled:(BOOL)geolocEnabled)
{
  [[APM sharedInstance] setGeolocEnabled:geolocEnabled];
}

RCT_EXPORT_METHOD(isGeolocEnabled:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
  resolve(@([[APM sharedInstance] geolocEnabled]));
}

@end
