///
/// HybridPreviewViewComponent.mm
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © Marc Rousavy @ Margelo
///

#import "HybridPreviewViewComponent.hpp"
#import <memory>
#import <react/renderer/componentregistry/ComponentDescriptorProvider.h>
#import <React/RCTViewComponentView.h>
#import <React/RCTComponentViewFactory.h>
#import <React/UIView+ComponentViewProtocol.h>
#import <NitroModules/NitroDefines.hpp>
#import <UIKit/UIKit.h>

#import "HybridPreviewViewSpecSwift.hpp"
#import "VisionCamera-Swift-Cxx-Umbrella.hpp"

#if __has_include(<cxxreact/ReactNativeVersion.h>)
#include <cxxreact/ReactNativeVersion.h>
#if REACT_NATIVE_VERSION_MINOR >= 82
#define ENABLE_RCT_COMPONENT_VIEW_INVALIDATE
#endif
#endif

using namespace facebook;
using namespace margelo::nitro::camera;
using namespace margelo::nitro::camera::views;

/**
 * Represents the React Native View holder for the Nitro "PreviewView" HybridView.
 */
@interface HybridPreviewViewComponent: RCTViewComponentView
+ (BOOL)shouldBeRecycled;
@end

@implementation HybridPreviewViewComponent {
  std::shared_ptr<HybridPreviewViewSpecSwift> _hybridView;
}

+ (void) load {
  [super load];
  [RCTComponentViewFactory.currentComponentViewFactory registerComponentViewClass:[HybridPreviewViewComponent class]];
}

+ (react::ComponentDescriptorProvider) componentDescriptorProvider {
  return react::concreteComponentDescriptorProvider<HybridPreviewViewComponentDescriptor>();
}

- (instancetype) init {
  if (self = [super init]) {
    std::shared_ptr<HybridPreviewViewSpec> hybridView = VisionCamera::VisionCameraAutolinking::createPreviewView();
    _hybridView = std::dynamic_pointer_cast<HybridPreviewViewSpecSwift>(hybridView);
    [self updateView];
  }
  return self;
}

- (void) updateView {
  // 1. Get Swift part
  VisionCamera::HybridPreviewViewSpec_cxx& swiftPart = _hybridView->getSwiftPart();

  // 2. Get UIView*
  void* viewUnsafe = swiftPart.getView();
  UIView* view = (__bridge_transfer UIView*) viewUnsafe;

  // 3. Update RCTViewComponentView's [contentView]
  [self setContentView:view];
}

- (void) updateProps:(const std::shared_ptr<const react::Props>&)props
            oldProps:(const std::shared_ptr<const react::Props>&)oldProps {
  // 1. Downcast props
  const auto& newViewPropsConst = *std::static_pointer_cast<HybridPreviewViewProps const>(props);
  auto& newViewProps = const_cast<HybridPreviewViewProps&>(newViewPropsConst);
  VisionCamera::HybridPreviewViewSpec_cxx& swiftPart = _hybridView->getSwiftPart();

  // 2. Update each prop individually
  swiftPart.beforeUpdate();

  // previewOutput: optional
  if (newViewProps.previewOutput.isDirty) {
    swiftPart.setPreviewOutput(newViewProps.previewOutput.value);
    newViewProps.previewOutput.isDirty = false;
  }
  // resizeMode: optional
  if (newViewProps.resizeMode.isDirty) {
    swiftPart.setResizeMode(newViewProps.resizeMode.value);
    newViewProps.resizeMode.isDirty = false;
  }
  // implementationMode: optional
  if (newViewProps.implementationMode.isDirty) {
    swiftPart.setImplementationMode(newViewProps.implementationMode.value);
    newViewProps.implementationMode.isDirty = false;
  }
  // gestureControllers: optional
  if (newViewProps.gestureControllers.isDirty) {
    swiftPart.setGestureControllers(newViewProps.gestureControllers.value);
    newViewProps.gestureControllers.isDirty = false;
  }
  // onPreviewStarted: optional
  if (newViewProps.onPreviewStarted.isDirty) {
    swiftPart.setOnPreviewStarted(newViewProps.onPreviewStarted.value);
    newViewProps.onPreviewStarted.isDirty = false;
  }
  // onPreviewStopped: optional
  if (newViewProps.onPreviewStopped.isDirty) {
    swiftPart.setOnPreviewStopped(newViewProps.onPreviewStopped.value);
    newViewProps.onPreviewStopped.isDirty = false;
  }

  swiftPart.afterUpdate();

  // 3. Update hybridRef if it changed
  if (newViewProps.hybridRef.isDirty) {
    // hybridRef changed - call it with new this
    const auto& maybeFunc = newViewProps.hybridRef.value;
    if (maybeFunc.has_value()) {
      maybeFunc.value()(_hybridView);
    }
    newViewProps.hybridRef.isDirty = false;
  }

  // 4. Continue in base class
  [super updateProps:props oldProps:oldProps];
}

+ (BOOL)shouldBeRecycled {
  return VisionCamera::VisionCameraAutolinking::isPreviewViewRecyclable();
}

- (void)prepareForRecycle {
  [super prepareForRecycle];
  VisionCamera::HybridPreviewViewSpec_cxx& swiftPart = _hybridView->getSwiftPart();
  swiftPart.maybePrepareForRecycle();
}

#ifdef ENABLE_RCT_COMPONENT_VIEW_INVALIDATE
- (void)invalidate {
  VisionCamera::HybridPreviewViewSpec_cxx& swiftPart = _hybridView->getSwiftPart();
  swiftPart.onDropView();
  [super invalidate];
}
#endif

@end
