#import "RNZoomVideoSdkRemoteCameraControlHelper.h"
#import "RCTConvert+RNZoomVideoSdk.h"
#import "RNZoomVideoSdkUser.h"

@implementation RNZoomVideoSdkRemoteCameraControlHelper

- (ZoomVideoSDKRemoteCameraControlHelper *)getCameraControllerHelper:(NSString *)userId
{
    ZoomVideoSDKRemoteCameraControlHelper* cameraControlHelper = nil;
    @try {
        ZoomVideoSDKUser* user = [RNZoomVideoSdkUser getUser:userId];
        cameraControlHelper = [user getRemoteCameraControlHelper];

        if (cameraControlHelper == nil) {
            NSException *e = [NSException exceptionWithName:@"NoCameraControlHelper" reason:@"No Camera Control Helper Found" userInfo:nil];
            @throw e;
        }
    } @catch(NSException *e) {
        NSLog(@"%@ - %@", e.name, e.reason);
    }
    return cameraControlHelper;
}

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(requestControlRemoteCamera:(NSString *)userId
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @([[self getCameraControllerHelper: userId] requestControlRemoteCamera])]);
    });
}

RCT_EXPORT_METHOD(giveUpControlRemoteCamera:(NSString *)userId
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @([[self getCameraControllerHelper: userId] giveUpControlRemoteCamera])]);
    });
}

RCT_EXPORT_METHOD(turnLeft:(NSString *)userId
        range:(int)range
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @([[self getCameraControllerHelper: userId] turnLeft: (unsigned int)range])]);
    });
}

RCT_EXPORT_METHOD(turnRight:(NSString *)userId
        range:(int)range
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @([[self getCameraControllerHelper: userId] turnRight: (unsigned int)range])]);
    });
}

RCT_EXPORT_METHOD(turnUp:(NSString *)userId
        range:(int)range
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @([[self getCameraControllerHelper: userId] turnUp: (unsigned int)range])]);
    });
}

RCT_EXPORT_METHOD(turnDown:(NSString *)userId
        range:(int)range
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @([[self getCameraControllerHelper: userId] turnDown: (unsigned int)range])]);
    });
}

RCT_EXPORT_METHOD(zoomIn:(NSString *)userId
        range:(int)range
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @([[self getCameraControllerHelper: userId] zoomIn: (unsigned int)range])]);
    });
}

RCT_EXPORT_METHOD(zoomOut:(NSString *)userId
        range:(int)range
        withResolve:(RCTPromiseResolveBlock)resolve
        withRejecter:(RCTPromiseRejectBlock)reject)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        resolve([[RCTConvert ZoomVideoSDKErrorValuesReversed] objectForKey: @([[self getCameraControllerHelper: userId] zoomOut: (unsigned int)range])]);
    });
}
@end
