#import <React/RCTConvert.h>
#import "RNZoomVideoSdk.h"
#import "RNZoomVideoSdkWhiteboardHelper.h"
#import "RCTConvert+RNZoomVideoSdk.h"
#import "RNZoomWhiteboardView.h"

@implementation RNZoomVideoSdkWhiteboardHelper

- (ZoomVideoSDKWhiteboardHelper *)getWhiteboardHelper
{
    ZoomVideoSDKWhiteboardHelper* whiteboardHelper = nil;
    @try {
        whiteboardHelper = [[ZoomVideoSDK shareInstance] getWhiteboardHelper];
        if (whiteboardHelper == nil) {
            NSException *e = [NSException exceptionWithName:@"NoWhiteboardHelperFound" reason:@"No Whiteboard Helper Found" userInfo:nil];
            @throw e;
        }
    } @catch (NSException *e) {
        NSLog(@"%@ - %@", e.name, e.reason);
    }
    return whiteboardHelper;
}

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(canStartShareWhiteboard:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        ZoomVideoSDKWhiteboardHelper* whiteboardHelper = [self getWhiteboardHelper];
        if (whiteboardHelper == nil) {
            resolve(@(NO));
            return;
        }
        resolve(@([whiteboardHelper canStartShareWhiteboard]));
    });
}

RCT_EXPORT_METHOD(canStopShareWhiteboard:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        ZoomVideoSDKWhiteboardHelper* whiteboardHelper = [self getWhiteboardHelper];
        if (whiteboardHelper == nil) {
            resolve(@(NO));
            return;
        }
        resolve(@([whiteboardHelper canStopShareWhiteboard]));
    });
}

RCT_EXPORT_METHOD(startShareWhiteboard:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        ZoomVideoSDKWhiteboardHelper* whiteboardHelper = [self getWhiteboardHelper];
        if (whiteboardHelper == nil) {
            reject(@"whiteboard_helper_error", @"Whiteboard helper not available", nil);
            return;
        }
        ZoomVideoSDKError error = [whiteboardHelper startShareWhiteboard];
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @(error)]);
    });
}

RCT_EXPORT_METHOD(stopShareWhiteboard:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        ZoomVideoSDKWhiteboardHelper* whiteboardHelper = [self getWhiteboardHelper];
        if (whiteboardHelper == nil) {
            reject(@"whiteboard_helper_error", @"Whiteboard helper not available", nil);
            return;
        }
        ZoomVideoSDKError error = [whiteboardHelper stopShareWhiteboard];
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @(error)]);
    });
}

RCT_EXPORT_METHOD(isOtherSharingWhiteboard:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        ZoomVideoSDKWhiteboardHelper* whiteboardHelper = [self getWhiteboardHelper];
        if (whiteboardHelper == nil) {
            resolve(@(NO));
            return;
        }
        resolve(@([whiteboardHelper isOtherSharingWhiteboard]));
    });
}

RCT_EXPORT_METHOD(exportWhiteboard:(NSString *)format
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        ZoomVideoSDKWhiteboardHelper* whiteboardHelper = [self getWhiteboardHelper];
        if (whiteboardHelper == nil) {
            reject(@"whiteboard_helper_error", @"Whiteboard helper not available", nil);
            return;
        }
        ZoomVideoSDKWhiteboardExportFormatType exportFormat = [RCTConvert ZoomVideoSDKWhiteboardExportFormatType:format];
        ZoomVideoSDKError error = [whiteboardHelper exportWhiteboard:exportFormat];
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @(error)]);
    });
}

@end
