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

#import "HybridRiveViewComponent.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 "HybridRiveViewSpecSwift.hpp"
#import "RNRive-Swift-Cxx-Umbrella.hpp"

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

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

@implementation HybridRiveViewComponent {
  std::shared_ptr<HybridRiveViewSpecSwift> _hybridView;
}

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

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

- (instancetype) init {
  if (self = [super init]) {
    std::shared_ptr<HybridRiveViewSpec> hybridView = RNRive::RNRiveAutolinking::createRiveView();
    _hybridView = std::dynamic_pointer_cast<HybridRiveViewSpecSwift>(hybridView);
    [self updateView];
  }
  return self;
}

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

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

  // artboardName: optional
  if (newViewProps.artboardName.isDirty) {
    swiftPart.setArtboardName(newViewProps.artboardName.value);
    newViewProps.artboardName.isDirty = false;
  }
  // stateMachineName: optional
  if (newViewProps.stateMachineName.isDirty) {
    swiftPart.setStateMachineName(newViewProps.stateMachineName.value);
    newViewProps.stateMachineName.isDirty = false;
  }
  // autoPlay: optional
  if (newViewProps.autoPlay.isDirty) {
    swiftPart.setAutoPlay(newViewProps.autoPlay.value);
    newViewProps.autoPlay.isDirty = false;
  }
  // file: hybrid-object
  if (newViewProps.file.isDirty) {
    swiftPart.setFile(newViewProps.file.value);
    newViewProps.file.isDirty = false;
  }
  // alignment: optional
  if (newViewProps.alignment.isDirty) {
    swiftPart.setAlignment(newViewProps.alignment.value);
    newViewProps.alignment.isDirty = false;
  }
  // fit: optional
  if (newViewProps.fit.isDirty) {
    swiftPart.setFit(newViewProps.fit.value);
    newViewProps.fit.isDirty = false;
  }
  // layoutScaleFactor: optional
  if (newViewProps.layoutScaleFactor.isDirty) {
    swiftPart.setLayoutScaleFactor(newViewProps.layoutScaleFactor.value);
    newViewProps.layoutScaleFactor.isDirty = false;
  }
  // dataBind: optional
  if (newViewProps.dataBind.isDirty) {
    swiftPart.setDataBind(newViewProps.dataBind.value);
    newViewProps.dataBind.isDirty = false;
  }
  // onError: function
  if (newViewProps.onError.isDirty) {
    swiftPart.setOnError(newViewProps.onError.value);
    newViewProps.onError.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 RNRive::RNRiveAutolinking::isRiveViewRecyclable();
}

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

@end
