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

#import "HybridNitroVtoViewComponent.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 "HybridNitroVtoViewSpecSwift.hpp"
#import "NitroVto-Swift-Cxx-Umbrella.hpp"

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

/**
 * Represents the React Native View holder for the Nitro "NitroVtoView" HybridView.
 */
@interface HybridNitroVtoViewComponent: RCTViewComponentView
@end

@implementation HybridNitroVtoViewComponent {
  std::shared_ptr<HybridNitroVtoViewSpecSwift> _hybridView;
}

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

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

- (instancetype) init {
  if (self = [super init]) {
    std::shared_ptr<HybridNitroVtoViewSpec> hybridView = NitroVto::NitroVtoAutolinking::createNitroVtoView();
    _hybridView = std::dynamic_pointer_cast<HybridNitroVtoViewSpecSwift>(hybridView);
    [self updateView];
  }
  return self;
}

- (void) updateView {
  // 1. Get Swift part
  NitroVto::HybridNitroVtoViewSpec_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<HybridNitroVtoViewProps const>(props);
  auto& newViewProps = const_cast<HybridNitroVtoViewProps&>(newViewPropsConst);
  NitroVto::HybridNitroVtoViewSpec_cxx& swiftPart = _hybridView->getSwiftPart();

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

  // modelUrl: string
  if (newViewProps.modelUrl.isDirty) {
    swiftPart.setModelUrl(newViewProps.modelUrl.value);
    newViewProps.modelUrl.isDirty = false;
  }
  // isActive: boolean
  if (newViewProps.isActive.isDirty) {
    swiftPart.setIsActive(newViewProps.isActive.value);
    newViewProps.isActive.isDirty = false;
  }
  // onModelLoaded: optional
  if (newViewProps.onModelLoaded.isDirty) {
    swiftPart.setOnModelLoaded(newViewProps.onModelLoaded.value);
    newViewProps.onModelLoaded.isDirty = false;
  }
  // onFaceTracked: optional
  if (newViewProps.onFaceTracked.isDirty) {
    swiftPart.setOnFaceTracked(newViewProps.onFaceTracked.value);
    newViewProps.onFaceTracked.isDirty = false;
  }
  // onGlassesDisplayed: optional
  if (newViewProps.onGlassesDisplayed.isDirty) {
    swiftPart.setOnGlassesDisplayed(newViewProps.onGlassesDisplayed.value);
    newViewProps.onGlassesDisplayed.isDirty = false;
  }
  // forwardOffset: optional
  if (newViewProps.forwardOffset.isDirty) {
    swiftPart.setForwardOffset(newViewProps.forwardOffset.value);
    newViewProps.forwardOffset.isDirty = false;
  }
  // debug: optional
  if (newViewProps.debug.isDirty) {
    swiftPart.setDebug(newViewProps.debug.value);
    newViewProps.debug.isDirty = false;
  }
  // showNativeFPS: optional
  if (newViewProps.showNativeFPS.isDirty) {
    swiftPart.setShowNativeFPS(newViewProps.showNativeFPS.value);
    newViewProps.showNativeFPS.isDirty = false;
  }
  // isClipOn: boolean
  if (newViewProps.isClipOn.isDirty) {
    swiftPart.setIsClipOn(newViewProps.isClipOn.value);
    newViewProps.isClipOn.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];
}

@end
