// ReactNativeTmxProfiling.m

#import "ReactNativeTmxProfiling.h"
#import <TMXProfiling/TMXProfiling.h>
#import <TMXProfilingConnections/TMXProfilingConnections.h>

@implementation ReactNativeTmxProfiling

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(sampleMethod:(NSString *)stringArgument numberParameter:(nonnull NSNumber *)numberArgument callback:(RCTResponseSenderBlock)callback)
{
    // TODO: Implement some actually useful functionality
    callback(@[[NSString stringWithFormat: @"numberArgument: %@ stringArgument: %@", numberArgument, stringArgument]]);
}

RCT_EXPORT_METHOD(profileDevice:(NSString *)orgId 
    fingerprintServer:(NSString *)fingerprintServer    
    resolver:(RCTPromiseResolveBlock)resolve 
    rejecter:(RCTPromiseRejectBlock)reject)
{   
    TMXProfilingConnections *tcn = [[TMXProfilingConnections alloc] init];
    tcn.connectionTimeout = 60;
    tcn.connectionRetryCount = 3;

    [[TMXProfiling sharedInstance] configure:@{TMXOrgID:orgId, TMXFingerprintServer:fingerprintServer, TMXProfileTimeout:@180, TMXProfilingConnectionsInstance:tcn}];   
     NSArray *custAttrs = @[];
    [[TMXProfiling sharedInstance] profileDeviceUsing:@{TMXCustomAttributes:custAttrs} callbackBlock:^(NSDictionary * _Nullable result) {
        NSLog(@"Profiling finished with result of %@", result); 
        NSMutableDictionary *mutableDictionary = [result mutableCopy];     //Make the dictionary mutable to change/add
        mutableDictionary[@"orgId"] = orgId;   
        mutableDictionary[@"fingerprintServer"] = fingerprintServer;   
        resolve(mutableDictionary);
        // callback(result) 
    }];
}

@end
