// RN1Flow.m

#import "RN1Flow.h"

@import _1Flow;

@interface RN1Flow()

@end

@implementation RN1Flow

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

+ (BOOL)requiresMainQueueSetup {
    return YES;
}

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

RCT_EXPORT_MODULE(RN1Flow)

#pragma mark - Segmentation

RCT_EXPORT_METHOD(configure:(NSString *)projectKey enableSurveys:(BOOL)enableSurveys resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        [OneFlow configure:projectKey];
        [OneFlow setEnableSurveys:enableSurveys];
        [[NSNotificationCenter defaultCenter] addObserverForName:@"survey_finished" object:nil queue:nil usingBlock:^(NSNotification *note) {
            NSDictionary *notification = note.userInfo;
            
            // Check if notification is valid and contains necessary information
            if (notification && [notification isKindOfClass:[NSDictionary class]]) {
                // Ensure we're on the main thread
                dispatch_async(dispatch_get_main_queue(), ^{
                    // Safe event dispatch
                    @try {
                        [self sendEventWithName:@"oneFlowSurveyFinishNotification" body:notification];
                    } @catch (NSException *exception) {
                        // Log the error if sending event fails
                        NSLog(@"Error sending event: %@", exception.reason);
                    }
                });
            } else {
                NSLog(@"Invalid notification received: %@", notification);
            }
        }];
        
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"survey_finished" object:nil];
}

RCT_EXPORT_METHOD(recordEventName:(NSString *)eventName parameters:(NSDictionary *)parameters resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        
        [OneFlow recordEventName:eventName parameters:parameters];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}

RCT_EXPORT_METHOD(recordEventNameWithoutParam:(NSString *)eventName resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        
        [OneFlow recordEventName:eventName parameters:nil];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}


RCT_EXPORT_METHOD(startFlow:(NSString *)flowId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        
        [OneFlow startFlow:flowId];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}
RCT_EXPORT_METHOD(logUser:(NSString *)uniqueId userDetails:(NSDictionary *)userDetails resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        [OneFlow logUser:uniqueId userDetails:userDetails];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}

RCT_EXPORT_METHOD(logUserWithoutParam:(NSString *)uniqueId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        [OneFlow logUser:uniqueId userDetails:nil];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}


RCT_EXPORT_METHOD(enableSurveys:(BOOL)enableSurveys resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        [OneFlow setEnableSurveys:enableSurveys];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}

RCT_EXPORT_METHOD(shouldPrintLog:(BOOL)shouldPrintLog resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        [OneFlow shouldPrintLog:shouldPrintLog];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}

RCT_EXPORT_METHOD(useFont:(NSString *)fontFamily path:(NSString *)path resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        [OneFlow useFontWithFontFamily:fontFamily];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}

RCT_EXPORT_METHOD(showInbox:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    @try {
        [OneFlow showInbox];
        resolve(nil);
    } @catch (NSError *e) {
        reject(nil, nil, e);
    }
}

@end
