#import "RCTApplePlayerView.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 RCTApplePlayerView () <RCTRCTApplePlayerViewViewProtocol>
    @property (nonatomic, strong) ApplePlayerView* appleplayerView;
@end

@implementation RCTApplePlayerView

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

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

  [super updateProps:props oldProps:oldProps];
}

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

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

@end
