#ifdef RCT_NEW_ARCH_ENABLED
#import "DocvSdkView.h"

#import <react/renderer/components/RNDocvSdkViewSpec/ComponentDescriptors.h>
#import <react/renderer/components/RNDocvSdkViewSpec/EventEmitters.h>
#import <react/renderer/components/RNDocvSdkViewSpec/Props.h>
#import <react/renderer/components/RNDocvSdkViewSpec/RCTComponentViewHelpers.h>

#import "RCTFabricComponentsPlugins.h"
#import "Utils.h"
#import "TruliooView.h"

#import <TruliooCaptureSDK/TruliooCaptureSDK-Swift.h>

using namespace facebook::react;

@interface DocvSdkView () <RCTDocvSdkViewViewProtocol>

@end

@implementation DocvSdkView {
    TruliooView * _view;
}

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
    return concreteComponentDescriptorProvider<DocvSdkViewComponentDescriptor>();
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        static const auto defaultProps = std::make_shared<const DocvSdkViewProps>();
        _props = defaultProps;
        _view = [[TruliooView alloc] init];
        self.contentView = _view;
    }

  return self;
}

- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
    const auto &oldViewProps = *std::static_pointer_cast<DocvSdkViewProps const>(_props);
    const auto &newViewProps = *std::static_pointer_cast<DocvSdkViewProps const>(props);
    
    /// update view when new props are available
    if (oldViewProps.nativeCameraViewProps != newViewProps.nativeCameraViewProps) {
        NSString* json = [[NSString alloc] initWithUTF8String:newViewProps.nativeCameraViewProps.c_str()];
        TruliooView* truliooView = (TruliooView*)_view;
        [truliooView renderCamera:json];
    }

    [super updateProps:props oldProps:oldProps];
}

- (void)removeFromSuperview 
{
    TruliooView* truliooView = (TruliooView*)_view;
    [truliooView stopCamera];
    [super removeFromSuperview];
}

Class<RCTComponentViewProtocol> DocvSdkViewCls(void)
{
    return DocvSdkView.class;
}

@end
#endif
