#import "RCTThumbnailView.h"

#import <react/renderer/components/NativePrestoPlayReactThumbnailsSpec/ComponentDescriptors.h>
#import <react/renderer/components/NativePrestoPlayReactThumbnailsSpec/EventEmitters.h>
#import <react/renderer/components/NativePrestoPlayReactThumbnailsSpec/Props.h>
#import <react/renderer/components/NativePrestoPlayReactThumbnailsSpec/RCTComponentViewHelpers.h>
#import <React/RCTFabricComponentsPlugins.h>

#import "react_native_prestoplay_thumbnails-Swift.h"

using namespace facebook::react;

@interface RCTThumbnailView () <RCTThumbnailViewViewProtocol>
    @property (nonatomic, strong) RNPrestoplayThumbnailView* thumbnailView;
@end

@implementation RCTThumbnailView

-(instancetype)init
{
  if(self = [super init]) {
    _thumbnailView = [RNPrestoplayThumbnailView new];
    [self addSubview:_thumbnailView];
  }
  return self;
}

- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
  const auto &oldViewProps = *std::static_pointer_cast<ThumbnailViewProps const>(_props);
  const auto &newViewProps = *std::static_pointer_cast<ThumbnailViewProps const>(props);

  if (oldViewProps.playerId.compare(newViewProps.playerId) != 0) {
      [_thumbnailView setPlayerId:[NSString stringWithUTF8String:newViewProps.playerId.c_str()]];
  }
  
  // NOTE: comparing doubles
  if (oldViewProps.positionMs != newViewProps.positionMs) {
      [_thumbnailView setPositionMs:newViewProps.positionMs];
  }

  [super updateProps:props oldProps:oldProps];
}

-(void)layoutSubviews
{
  [super layoutSubviews];
    _thumbnailView.frame = self.bounds;
}

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
  return concreteComponentDescriptorProvider<ThumbnailViewComponentDescriptor>();
}

@end
