#import "RCTPlayerView.h"

#import <react/renderer/components/NativePrestoPlayReactSpec/ComponentDescriptors.h>
#import <react/renderer/components/NativePrestoPlayReactSpec/EventEmitters.h>
#import <react/renderer/components/NativePrestoPlayReactSpec/Props.h>
#import <react/renderer/components/NativePrestoPlayReactSpec/RCTComponentViewHelpers.h>
#import "react_native_prestoplay-Swift.h"

using namespace facebook::react;

@interface RCTPlayerView () <RCTRCTPlayerViewViewProtocol>
    @property (nonatomic, strong) PlayerView* playerView;
@end

@implementation RCTPlayerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _playerView = [[PlayerView alloc] initWithFrame:frame];
        [self addSubview:_playerView];
    }
    return self;
}

- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
  
  const auto &oldViewProps = *std::static_pointer_cast<RCTPlayerViewProps const>(oldProps);
  const auto &newViewProps = *std::static_pointer_cast<RCTPlayerViewProps const>(props);
    
  if (oldProps == nullptr || (oldViewProps.playerId != newViewProps.playerId)) {
      [_playerView setPlayerId:[NSString stringWithUTF8String:newViewProps.playerId.c_str()]];
  }

  [super updateProps:props oldProps:oldProps];
}

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

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

@end
