//
//  Created by Trulioo Inc. on 2024-05-02.
//

#import "TruliooView.h"
#import <TruliooCaptureSDK/TruliooCaptureSDK-Swift.h>

@implementation TruliooView

- (void)renderCamera:(NSString*)json {
    NSData* jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSError* error;
    NSDictionary* jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    
    if (error) {
        NSLog(@"Error parsing JSON: %@", error.localizedDescription);
    } else {
        NSString* cameraId = jsonDict[@"cameraId"];
        NSString* backgroundColor = jsonDict[@"backgroundColor"];
        NSNumber* showAnimationNum = jsonDict[@"shouldShowAnimation"];
        BOOL bShowAnimation = [showAnimationNum boolValue];
        
        TruliooCapture *trulioo = [[TruliooCapture alloc] init];
        @try {
            [trulioo 
             renderCapture:self
             :cameraId
             :backgroundColor
             :bShowAnimation
             error:nil
             :_onCaptureRegionChanged
            ];
        } @catch (NSException *exception) {
            NSLog(@"Error occurred: %@", exception.reason);
        }
    }
}

- (void)stopCamera {
    TruliooCapture *trulioo = [[TruliooCapture alloc] init];
    [trulioo onViewDisconnected];
}

- (void)removeFromSuperview {
    [self stopCamera];
    [super removeFromSuperview];
}

@end
