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

#import "HybridReadiumViewComponent.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 "HybridReadiumViewSpecSwift.hpp"
#import "NitroReadium-Swift-Cxx-Umbrella.hpp"

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

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

@implementation HybridReadiumViewComponent {
  std::shared_ptr<HybridReadiumViewSpecSwift> _hybridView;
}

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

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

- (instancetype) init {
  if (self = [super init]) {
    std::shared_ptr<HybridReadiumViewSpec> hybridView = NitroReadium::NitroReadiumAutolinking::createReadiumView();
    _hybridView = std::dynamic_pointer_cast<HybridReadiumViewSpecSwift>(hybridView);
    [self updateView];
  }
  return self;
}

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

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

  // file: optional
  if (newViewProps.file.isDirty) {
    swiftPart.setFile(newViewProps.file.value);
    newViewProps.file.isDirty = false;
  }
  // preferences: optional
  if (newViewProps.preferences.isDirty) {
    swiftPart.setPreferences(newViewProps.preferences.value);
    newViewProps.preferences.isDirty = false;
  }
  // decorations: optional
  if (newViewProps.decorations.isDirty) {
    swiftPart.setDecorations(newViewProps.decorations.value);
    newViewProps.decorations.isDirty = false;
  }
  // selectionActions: optional
  if (newViewProps.selectionActions.isDirty) {
    swiftPart.setSelectionActions(newViewProps.selectionActions.value);
    newViewProps.selectionActions.isDirty = false;
  }
  // onLocationChange: optional
  if (newViewProps.onLocationChange.isDirty) {
    swiftPart.setOnLocationChange(newViewProps.onLocationChange.value);
    newViewProps.onLocationChange.isDirty = false;
  }
  // onPublicationReady: optional
  if (newViewProps.onPublicationReady.isDirty) {
    swiftPart.setOnPublicationReady(newViewProps.onPublicationReady.value);
    newViewProps.onPublicationReady.isDirty = false;
  }
  // onDecorationActivated: optional
  if (newViewProps.onDecorationActivated.isDirty) {
    swiftPart.setOnDecorationActivated(newViewProps.onDecorationActivated.value);
    newViewProps.onDecorationActivated.isDirty = false;
  }
  // onSelectionChange: optional
  if (newViewProps.onSelectionChange.isDirty) {
    swiftPart.setOnSelectionChange(newViewProps.onSelectionChange.value);
    newViewProps.onSelectionChange.isDirty = false;
  }
  // onSelectionAction: optional
  if (newViewProps.onSelectionAction.isDirty) {
    swiftPart.setOnSelectionAction(newViewProps.onSelectionAction.value);
    newViewProps.onSelectionAction.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 NitroReadium::NitroReadiumAutolinking::isReadiumViewRecyclable();
}

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

@end
