#import <React/RCTUIManager.h>
#import <React/RCTBridgeModule.h>

#import "ContentsquareInterfaceWrapperLegacy.h"
#import "ContentsquareInterfaceWrapper.h"


//header file import
#import "ContentsquareBridgeModule.h"
#import "CurrencyHelper.h"

@implementation ContentsquareBridgeModule {
    __strong id<ContentsquareInterfaceWrapperBase> contentsquareSDKInterfaceWrapper;
    BOOL csqEnabled;
}
#if DEBUG
@synthesize contentsquareSDKInterfaceWrapper;
#endif

- (instancetype)init {
    self = [super init];
    if (self) {
        contentsquareSDKInterfaceWrapper = [[ContentsquareInterfaceWrapper alloc] init];
    }
    return self;
}

#pragma mark - React Native requirements

@synthesize bridge = _bridge;

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

RCT_EXPORT_MODULE(ContentsquareModule);

/*
 * Used for Bridge init
 * cf https://reactnative.dev/docs/native-modules-ios#exporting-constants
 */
+ (BOOL)requiresMainQueueSetup {
    return YES;
}

- (NSDictionary *)constantsToExport {
    return CurrencyHelper.currencyConstants;
}

- (NSDictionary *) getConstants {
    return [self constantsToExport];
}

#pragma mark - Bridge API


RCT_EXPORT_METHOD(resumeTracking) {
    [contentsquareSDKInterfaceWrapper resumeTracking];
}

RCT_EXPORT_METHOD(stopTracking) {
    [contentsquareSDKInterfaceWrapper stopTracking];
}

RCT_EXPORT_METHOD(stop) {
    [contentsquareSDKInterfaceWrapper stop];
}

RCT_EXPORT_METHOD(pauseTracking) {
    [contentsquareSDKInterfaceWrapper pauseTracking];
}

RCT_EXPORT_METHOD(addDynamicStringVar:(NSString *)key
                  value:(NSString *)value) {
    [contentsquareSDKInterfaceWrapper addDynamicStringVar:key
                                                    value:value];
}

RCT_EXPORT_METHOD(addDynamicNumberVar:(NSString *)key
                  value:(double)value) {
    [contentsquareSDKInterfaceWrapper addDynamicIntVar:key
                                                 value:(NSUInteger)value];
}

RCT_EXPORT_METHOD(start: (BOOL) isCSQEnabled) {
    if (isCSQEnabled){
        contentsquareSDKInterfaceWrapper = [[ContentsquareInterfaceWrapper alloc] init];
    }
    [contentsquareSDKInterfaceWrapper start];
}

RCT_EXPORT_METHOD(optIn) {
    [contentsquareSDKInterfaceWrapper optIn];
}

RCT_EXPORT_METHOD(optOut) {
    [contentsquareSDKInterfaceWrapper optOut];
}

RCT_EXPORT_METHOD(handleUrl:(NSString *)urlString) {
    if (!urlString) {
        return;
    }
    NSURL* url = [NSURL URLWithString:urlString];
    [contentsquareSDKInterfaceWrapper handleWithUrl:url];
}

RCT_EXPORT_METHOD(send:(NSString *)name
                  cvars: (NSArray *)cvars) {
    [contentsquareSDKInterfaceWrapper sendWithScreenViewWithName:name
                                                           cvars:cvars];
}

RCT_EXPORT_METHOD(trackScreenView:(NSString *)name
                  cvars: (NSArray *)cvars
                  sourceInfo:(NSDictionary *)sourceInfo) {
    [contentsquareSDKInterfaceWrapper trackScreenViewWithName:name
                                                        cvars:cvars
                                                   sourceInfo:sourceInfo];
}

RCT_EXPORT_METHOD(trackEvent:(NSString *)event
                  properties:(NSDictionary *)properties
                  sourceInfo:(NSDictionary *)sourceInfo) {
    [contentsquareSDKInterfaceWrapper trackEventWithName:event
                                              properties:properties
                                                sourceInfo:sourceInfo];
}

RCT_EXPORT_METHOD(getUserId:(RCTResponseSenderBlock)callback) {
    [contentsquareSDKInterfaceWrapper getUserId:callback];
}

RCT_EXPORT_METHOD(sendTransaction:(NSString*)identifier
                  value:(double)value
                  currency:(double)currency) {
    [contentsquareSDKInterfaceWrapper sendTransactionWithIntCurrency:identifier
                                                               value:value
                                                            currency:(NSInteger) currency];
}

RCT_EXPORT_METHOD(sendTransactionWithStringCurrency:(NSString*)identifier
                  value:(double)value currency:(NSString*)currency) {
    [contentsquareSDKInterfaceWrapper sendTransactionWithStringCurrency:identifier
                                                                  value:value
                                                               currency:currency];
}

RCT_EXPORT_METHOD(trackTransaction:(NSString*)identifier
                  value:(double)value
                  currency:(double)currency) {
    [contentsquareSDKInterfaceWrapper trackTransactionWithIntCurrency:identifier
                                                                value:value
                                                             currency:(NSInteger) currency];
}

RCT_EXPORT_METHOD(trackTransactionWithStringCurrency:(NSString*)identifier
                  value:(double)value currency:(NSString*)currency) {
    [contentsquareSDKInterfaceWrapper trackTransactionWithStringCurrency:identifier
                                                                   value:value
                                                                currency:currency];
}

RCT_EXPORT_METHOD(sendDynamicStringVar:(NSString *)key
                  value:(NSString *)value) {
    [contentsquareSDKInterfaceWrapper sendDynamicStringVar:key
                                                     value:value];
}

RCT_EXPORT_METHOD(sendDynamicIntVar:(NSString *)key
                  value:(double)value) {
    [contentsquareSDKInterfaceWrapper sendDynamicIntVar:key
                                                  value:(NSUInteger)value];
}

RCT_EXPORT_METHOD(injectWebView:(double)webViewTag) {
    NSNumber *reactTag = @(webViewTag);
    [contentsquareSDKInterfaceWrapper injectWebView:reactTag
                                                 in:_bridge.uiManager];
}

RCT_EXPORT_METHOD(removeWebViewInjection:(double)webViewTag) {
    NSNumber *reactTag = @(webViewTag);
    [contentsquareSDKInterfaceWrapper removeInjectionInWebView:reactTag
                                                            in:_bridge.uiManager];
}

RCT_EXPORT_METHOD(configureProductAnalytics:(NSString *)envId
                  productAnalyticsOptions:(NSDictionary *)productAnalyticsOptions) {
    [contentsquareSDKInterfaceWrapper configureProductAnalyticsWithEnvironmentID:envId additionalOptions:productAnalyticsOptions];
}

RCT_EXPORT_METHOD(initComponents:(NSDictionary*)params) {
    [contentsquareSDKInterfaceWrapper initComponentsWithParams:params];
}

RCT_EXPORT_METHOD(setDefaultMasking:(BOOL)isMasking) {
    [contentsquareSDKInterfaceWrapper setDefaultMaskingWithIsMasking:isMasking];
}

RCT_EXPORT_METHOD(sendUserIdentifier:(NSString *)userIdentifier){
    [contentsquareSDKInterfaceWrapper sendUserIdentifier:userIdentifier];
}

RCT_EXPORT_METHOD(identify:(NSString *)identity){
    [contentsquareSDKInterfaceWrapper identify:identity];
}

RCT_EXPORT_METHOD(resetIdentity){
    [contentsquareSDKInterfaceWrapper resetIdentity];
}

RCT_EXPORT_METHOD(setOnSessionReplayLinkChange){
    [contentsquareSDKInterfaceWrapper setOnSessionReplayLinkChange];
}

RCT_EXPORT_METHOD(setOnMetadataChange){
    [contentsquareSDKInterfaceWrapper setOnMetadataChange];
}

RCT_EXPORT_METHOD(monitorWarn:(NSDictionary*)params){
    [contentsquareSDKInterfaceWrapper monitorWarn:params];
}

RCT_EXPORT_METHOD(monitorError:(NSDictionary*)params){
    [contentsquareSDKInterfaceWrapper monitorError:params];
}

RCT_EXPORT_METHOD(registerWebView:(double)webViewTag) {
    NSNumber *reactTag = @(webViewTag);
    [contentsquareSDKInterfaceWrapper registerWebView:reactTag in:_bridge.uiManager];
}

RCT_EXPORT_METHOD(unregisterWebView:(double)webViewTag) {
    NSNumber *reactTag = @(webViewTag);
    [contentsquareSDKInterfaceWrapper unregisterWebView:reactTag from:_bridge.uiManager];
}

RCT_EXPORT_METHOD(setUrlMaskingPatterns:(NSArray<NSString *> *)patterns) {
    [contentsquareSDKInterfaceWrapper setURLMaskingPatterns:patterns];
}

RCT_EXPORT_METHOD(triggerNativeCrash) {
    [contentsquareSDKInterfaceWrapper triggerNativeCrash];
}

RCT_EXPORT_METHOD(addEventProperties:(NSDictionary*)properties) {
    [contentsquareSDKInterfaceWrapper addEventProperties:properties];
}

RCT_EXPORT_METHOD(removeEventProperty:(NSString*)key) {
    [contentsquareSDKInterfaceWrapper removeEventProperty:key];
}

RCT_EXPORT_METHOD(clearEventProperties) {
    [contentsquareSDKInterfaceWrapper clearEventProperties];
}

RCT_EXPORT_METHOD(addUserProperties:(NSDictionary*)properties) {
    [contentsquareSDKInterfaceWrapper addUserProperties:properties];
}

#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
    return std::make_shared<facebook::react::NativeContentsquareModuleSpecJSI>(params);
}
#endif

@end

