#import "RCTNativePlayerModule.h"
#import "react_native_prestoplay-Swift.h"

@implementation RCTNativePlayerModule
+ (NSString *)moduleName {
    return @"NativePlayerModule";
}

- (RTCNativePlayerModuleSwift*)playerModule {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[RTCNativePlayerModuleSwift shared] setEventEmitterFuncWithEmitter:^(NSString *eventName, id body) {
            [self emitOnPrestoPlayEvent:body];
        }];
    });
    return [RTCNativePlayerModuleSwift shared];
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params {
    return std::make_shared<facebook::react::NativePlayerModuleSpecJSI>(params);
}

- (void)create:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] create:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)createApplePlayer:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { 
    [[self playerModule] createApplePlayer:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)open:(nonnull NSString *)instanceId playerConfiguration:(nonnull NSDictionary *)playerConfiguration resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] open:instanceId playerConfiguration:playerConfiguration resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)destroy:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { 
    [[self playerModule] destroy:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)enableAdaptiveVideo:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { 
    [[self playerModule] enableAdaptiveVideo:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)enterPictureInPictureMode:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] enterPictureInPictureMode:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)exitPictureInPictureMode:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] exitPictureInPictureMode:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)isInPictureInPictureMode:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] isInPictureInPictureMode:instanceId resolve:^(bool isPiP){ resolve(@(isPiP)); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)pause:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { 
    [[self playerModule] pause:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)play:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { 
    [[self playerModule] play:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)replay:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { 
    [[self playerModule] replay:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)setAudioTrack:(nonnull NSString *)instanceId trackId:(nonnull NSString *)trackId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] setAudioTrack:instanceId trackId:trackId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)setMuted:(nonnull NSString *)instanceId newMuted:(BOOL)newMuted resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] setMuted:instanceId newMuted:newMuted resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)setPlaybackRate:(nonnull NSString *)instanceId newPlaybackRate:(double)newPlaybackRate resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] setPlaybackRate:instanceId newPlaybackRate:newPlaybackRate resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)setPosition:(nonnull NSString *)instanceId newPosition:(double)newPosition resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { 
    [[self playerModule] setPosition:instanceId newPositionMs:int64_t(newPosition) resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)setTextTrack:(nonnull NSString *)instanceId trackId:(NSString *)trackId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] setTextTrack:instanceId trackId:trackId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)setVideoRendition:(nonnull NSString *)instanceId renditionId:(nonnull NSString *)renditionId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] setVideoRendition:instanceId renditionId:renditionId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)setVideoTrack:(nonnull NSString *)instanceId trackId:(nonnull NSString *)trackId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] setVideoTrack:instanceId trackId:trackId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)setVolume:(nonnull NSString *)instanceId newVolume:(double)newVolume resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] setVolume:instanceId newVolume:newVolume resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)stop:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject { 
    [[self playerModule] stop:instanceId resolve:^(){ resolve(nil); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}

- (void)supportsPictureInPicture:(nonnull NSString *)instanceId resolve:(nonnull RCTPromiseResolveBlock)resolve reject:(nonnull RCTPromiseRejectBlock)reject {
    [[self playerModule] supportsPictureInPicture:instanceId resolve:^(BOOL sPiP){ resolve(@(sPiP)); } reject:^(NSString* prestoPlayErrorJson){ reject(nil, prestoPlayErrorJson, nil);}];
}



@end
