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

#import "HybridBlurViewComponent.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 "HybridBlurViewSpecSwift.hpp"
#import "NitroQmblurview-Swift-Cxx-Umbrella.hpp"

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

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

@implementation HybridBlurViewComponent {
  std::shared_ptr<HybridBlurViewSpecSwift> _hybridView;
}

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

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

- (instancetype) init {
  if (self = [super init]) {
    std::shared_ptr<HybridBlurViewSpec> hybridView = NitroQmblurview::NitroQmblurviewAutolinking::BlurView::create();
    _hybridView = std::dynamic_pointer_cast<HybridBlurViewSpecSwift>(hybridView);
    [self updateView];
  }
  return self;
}

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

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

  // blurRadius: optional
  if (newViewProps.blurRadius.isDirty) {
    swiftPart.setBlurRadius(newViewProps.blurRadius.value);
    newViewProps.blurRadius.isDirty = false;
  }
  // cornerRadius: optional
  if (newViewProps.cornerRadius.isDirty) {
    swiftPart.setCornerRadius(newViewProps.cornerRadius.value);
    newViewProps.cornerRadius.isDirty = false;
  }
  // blurRounds: optional
  if (newViewProps.blurRounds.isDirty) {
    swiftPart.setBlurRounds(newViewProps.blurRounds.value);
    newViewProps.blurRounds.isDirty = false;
  }
  // overlayColor: optional
  if (newViewProps.overlayColor.isDirty) {
    swiftPart.setOverlayColor(newViewProps.overlayColor.value);
    newViewProps.overlayColor.isDirty = false;
  }
  // downsampleFactor: optional
  if (newViewProps.downsampleFactor.isDirty) {
    swiftPart.setDownsampleFactor(newViewProps.downsampleFactor.value);
    newViewProps.downsampleFactor.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 NitroQmblurview::NitroQmblurviewAutolinking::BlurView::isRecyclableHybridView();
}

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

@end
