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

#import "HybridLinearGradientViewComponent.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 "HybridLinearGradientViewSpecSwift.hpp"
#import "NitroGradient-Swift-Cxx-Umbrella.hpp"

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

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

@implementation HybridLinearGradientViewComponent {
  std::shared_ptr<HybridLinearGradientViewSpecSwift> _hybridView;
}

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

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

- (instancetype) init {
  if (self = [super init]) {
    std::shared_ptr<HybridLinearGradientViewSpec> hybridView = NitroGradient::NitroGradientAutolinking::createLinearGradientView();
    _hybridView = std::dynamic_pointer_cast<HybridLinearGradientViewSpecSwift>(hybridView);
    [self updateView];
  }
  return self;
}

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

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

  // colors: array
  if (newViewProps.colors.isDirty) {
    swiftPart.setColors(newViewProps.colors.value);
    newViewProps.colors.isDirty = false;
  }
  // positions: optional
  if (newViewProps.positions.isDirty) {
    swiftPart.setPositions(newViewProps.positions.value);
    newViewProps.positions.isDirty = false;
  }
  // start: optional
  if (newViewProps.start.isDirty) {
    swiftPart.setStart(newViewProps.start.value);
    newViewProps.start.isDirty = false;
  }
  // end: optional
  if (newViewProps.end.isDirty) {
    swiftPart.setEnd(newViewProps.end.value);
    newViewProps.end.isDirty = false;
  }
  // angle: optional
  if (newViewProps.angle.isDirty) {
    swiftPart.setAngle(newViewProps.angle.value);
    newViewProps.angle.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
