//
//  Site24x7Rn.m
//  Zoho_React_Native_Project
//
//  Created by Murugappan V R on 03/06/21.
//

#import <Foundation/Foundation.h>
#import "Site24x7Rn.h"
#import <APM/APM.h>

@implementation Site24x7Rn
RCT_EXPORT_MODULE();
NSMutableDictionary *s247TransactionsDictionary=nil;
NSMutableDictionary *s247ComponentsDictionary=nil;
RCT_EXPORT_METHOD(startMonitoring:(NSString *)apiKey interval:(NSString *)interval){
    @try{
        [S24APM startWithAppKey:apiKey interval:[interval integerValue]];
    }
    @catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception starting monitoring %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(startMonitoringWithEndPoint:(NSString *)apiKey endPoint:(NSString *)endPoint interval:(NSString *)interval){
    @try{
        [S24APM startWithAppKey:apiKey interval:[interval integerValue] server:endPoint ];
    }
    @catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception starting monitoring %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(addScreen:(NSString *)screen orientation:(NSString *)orientation startTime:(NSString *)startTime loadTime:(NSString *)loadTime){
    @try{
        [S24APM addScreen:screen withTime:[loadTime integerValue] startTime:startTime];
    }
    @catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception adding screen %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(addHttpCall:(NSString *)httpCall method:(NSString *)method startTime:(NSString *)startTime loadTime:(NSString *)loadTime statusCode:(NSString *)statusCode screenName:(NSString *)screenName){
    @try{
        [S24APM addHttpCall:httpCall withTime:[loadTime integerValue] startTime:startTime respCode:[statusCode integerValue] httpMethod:method];
    }
    @catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception adding httpcall %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(addJsError:(NSString *)message errorType:(NSString *)errorType file:(NSString *)file methodName:(NSString *)methodName stackTrace:(NSString *)stackTrace userTrace:(NSString *)userTrace screen:(NSString *)screen startTime:(NSString *)startTime handledException:(BOOL)handledException){
    @try{
      [S24APM addJsError:message errorType:errorType file:file methodName:methodName stackTrace:stackTrace userTrace:userTrace screen:screen startTime:startTime handledException:handledException];
      [S24APM flush];
    }
    @catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception adding js error %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(setUserId:(NSString *)userId){
    @try{
      [S24APM setUserId:userId];
    }@catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception setting user id %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(startTransaction:(NSString *)txnName){
    @try{
      S24Transaction *transaction = [S24APM startTransactionWithName:txnName];
      if(s247TransactionsDictionary == nil){
        s247TransactionsDictionary = [[NSMutableDictionary<NSString *, S24Transaction *> alloc] init];
      }
      [s247TransactionsDictionary setValue:transaction forKey:txnName];
    }@catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception adding txn %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(startComponent:(NSString *)txnName component:(NSString *)component){
    @try{
      S24Transaction *transaction = s247TransactionsDictionary[txnName];
      S24Component *apmComponent = [transaction startComponentWithType:component];
      
      [s247TransactionsDictionary setValue:transaction forKey:txnName];
      
      if(s247ComponentsDictionary == nil){
        s247ComponentsDictionary = [[NSMutableDictionary<NSString *, S24Component *> alloc] init];
      }
      [s247ComponentsDictionary setObject:apmComponent forKey:[NSString stringWithFormat:@"%@%@", txnName, component]];
    }@catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception stoping txn %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(stopTransaction:(NSString *)txnName){
    S24Transaction *transaction = s247TransactionsDictionary[txnName];
    if(transaction!=nil){
        @try{
            [S24APM stopTransaction:transaction];
            [s247TransactionsDictionary removeObjectForKey:txnName];
        }
        @catch(NSException *exception){
          NSLog(@"site24x7mobileRUM exception adding component %@", exception.reason);
        }
    }
}

RCT_EXPORT_METHOD(stopComponent:(NSString *)txnName component:(NSString *)component){
    
    S24Transaction *transaction = s247TransactionsDictionary[txnName];
    NSString *componentKey = [NSString stringWithFormat:@"%@%@", txnName, component];
    S24Component *apmComponent = s247ComponentsDictionary[componentKey];
    if(transaction!=nil && apmComponent!=nil){
        @try{
            [transaction stopComponent:apmComponent];
            [s247TransactionsDictionary removeObjectForKey:componentKey];
        }
        @catch(NSException *exception){
          NSLog(@"site24x7mobileRUM exception stoping component %@", exception.reason);
        }
    }
}

RCT_EXPORT_METHOD(flush){
    @try{
        [S24APM flush];
    }
    @catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception flushing data %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(trackNativeExceptions){
    @try{
        [S24APM enableErrorReporting];
    }
    @catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception initializing native exception reporting %@", exception.reason);
    }
}

RCT_EXPORT_METHOD(excludeNativeScreens:(NSArray *)screens){
    @try {
        [S24APM ignoreScreens:screens];
    } @catch (NSException *exception) {
        NSLog(@"site24x7mobileRUM exception adding exclude native screens");
    }
}

RCT_EXPORT_METHOD(setEnvironment:(NSString *)environment){
    @try{
        [S24APM setEnvironment:environment];
    }@catch(NSException *exception){
      NSLog(@"site24x7mobileRUM exception setting environment %@", exception.reason);
    }
}

@end
