#import "ReReactNativeSDK.h"

#import <rereactnative-Swift.h>

@implementation ReReactNativeSDK
RCT_EXPORT_MODULE()

+ (id)allocWithZone:(struct _NSZone *)zone {
  static ReReactNativeSDK *shared = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    shared = [super allocWithZone:zone];
  });
  
  
  return shared;
}


RCT_REMAP_METHOD(multiply,
                 multiplyWithA:(double)a withB:(double)b
                 withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    NSNumber *result = @(10 * 10);

    resolve(result);
}

RCT_REMAP_METHOD(updatePushToken,updatePushToken:(NSString *)token  withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    
    [ReReactNativeSDKSwift updatePushTokenWithToken:token];
}

RCT_REMAP_METHOD(customEvent,customEvent:(NSString *)event withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift customEventWithEventData:event];
}

RCT_REMAP_METHOD(customEventWithData,eventName:(NSString *)event data:(NSDictionary *) dict  withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift customEventWithDataWithEventName:event eventData:dict];
}

RCT_REMAP_METHOD(onDeviceUserRegister,userRegister:(NSString *)userRegister withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift onDeviceUserRegisterWithUserRegister:userRegister];
}

RCT_REMAP_METHOD(formDataCapture,formDataCapture:(NSString *)formData withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift formDataCaptureWithFormData:formData];
}
RCT_REMAP_METHOD(getNotifications,getNotifications:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    NSArray *array = [ReReactNativeSDKSwift getNotifications];
    resolve(array);
}
RCT_REMAP_METHOD(deleteNotificationById,deleteNotificationById:(NSString *)campaignId withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift deleteNotificationByIdWithCampaignId:campaignId];
}

RCT_REMAP_METHOD(getReadNotificationCount,getReadNotificationCount:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift getReadNotificationCountOnSuccess:^(NSInteger count) {
        NSString *inStr = [NSString stringWithFormat: @"%ld", (long)count];

        resolve(inStr);
    }];
}
RCT_REMAP_METHOD(getUnreadNotificationCount,getUnreadNotificationCount:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift getUnReadNotificationCountOnSuccess:^(NSInteger count) {
        NSString *inStr = [NSString stringWithFormat: @"%ld", (long)count];
        resolve(inStr);
    }];
}
RCT_REMAP_METHOD(appConversionTracking,appConversionTracking:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift appConversionTracking];
}

RCT_REMAP_METHOD(appConversionTrackingWithData,appConversionTrackingWithData:(NSDictionary *) data withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift appConversionTrackingWithDataWithData:data];
}
RCT_REMAP_METHOD(screenNavigation,screenNavigation:(NSString *) screenName withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift screenNavigationWithScreenName:screenName];
}
RCT_REMAP_METHOD(readNotification,readNotification:(NSString *) campaignId withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift readNotificationWithCampaignId:campaignId];
}
RCT_REMAP_METHOD(unReadNotification,unReadNotification:(NSString *) campaignId withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift unReadNotificationWithCampaignId:campaignId];
}
RCT_REMAP_METHOD(locationUpdate,locationUpdate:(NSString *) lat longidute:(NSString *) longidute  withResolver:(RCTPromiseResolveBlock)resolve
                 withRejecter:(RCTPromiseRejectBlock)reject)
{
    [ReReactNativeSDKSwift locationUpdateWithLat:lat long:longidute];
}

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

- (void)initSdkWithAppId:(NSString *)appId{
    NSLog(@"************** %@",appId);
    
    ReReactNativeSDKSwift *reHandler = [[ReReactNativeSDKSwift alloc]init];
    [reHandler sdkInitWithAppId:appId];

    
}
+ (void)willPresentNotification:(UNNotification *)notification
          withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    [ReReactNativeSDKSwift willPresent:notification withCompletionHandler:^(UNNotificationPresentationOptions opt) {
        completionHandler(opt);
    }];
}
+ (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
    [ReReactNativeSDKSwift didReceiveRemoteNotificationWithData:userInfo];
}
- (void) didReceiveNotificationResponse:(UNNotificationResponse *)response {
    [ReReactNativeSDKSwift didReceiveNotificationResponseWithResponse:response];
    
    [self sendEventWithName:@"smartLinkNotificationData" body:response.notification.request.content.userInfo];
    
    
}
- (void) continueUserActivity:(nonnull NSUserActivity *)userActivity{
    [ReReactNativeSDKSwift handleDynamicLinkWithUserActivity:userActivity successHandler:^(NSString * successData) {
        [self sendEventWithName:@"smartLinkNotificationData" body:successData];
    } failureHandler:^(NSString * err) {
        
    }];
}
- (void) openURL:(NSURL *)url{
    [ReReactNativeSDKSwift handleOpenlinkWithUrl:url successHandler:^(NSString * successData) {
        [self sendEventWithName:@"smartLinkNotificationData" body:successData];
    } failureHandler:^(NSString * _Nonnull) {
        
    }];
}

// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
    (const facebook::react::ObjCTurboModule::InitParams &)params
{
    return std::make_shared<facebook::react::NativeLibraryRnSpecJSI>(params);
}
#endif

@end
