#import "SuperpoweredKlaas.h"

@implementation SuperpoweredKlaas {
    MultiTracksPlayer *_multiTracksPlayer;
}

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(setup:
    (NSString *) licenseKey withEnableAudioAnalysis:
    (BOOL *) enableAudioAnalysis withEnableFFTAndFrequencyDomain:
    (BOOL *) enableFFTAndFrequencyDomain withEnableAudioTimeStretching:
    (BOOL *) enableAudioTimeStretching withEnableAudioEffects:
    (BOOL *) enableAudioEffects withEnableAudioPlayerAndDecoder:
    (BOOL *) enableAudioPlayerAndDecoder withEnableCryptographics:
    (BOOL *) enableCryptographics withEnableNetworking:
    (BOOL *) enableNetworking withResolve:
    (RCTPromiseResolveBlock) resolve withReject:
    (RCTPromiseRejectBlock) reject) {
    Superpowered::Initialize(
            [licenseKey UTF8String],
            static_cast<bool>(enableAudioAnalysis),
            static_cast<bool>(enableFFTAndFrequencyDomain),
            static_cast<bool>(enableAudioTimeStretching),
            static_cast<bool>(enableAudioEffects),
            static_cast<bool>(enableAudioPlayerAndDecoder),
            static_cast<bool>(enableCryptographics),
            static_cast<bool>(enableNetworking)
    );
}

RCT_EXPORT_METHOD(initialize:
    (NSArray *) tracksURLs withResolve:
    (RCTPromiseResolveBlock) resolve withReject:
    (RCTPromiseRejectBlock) reject) {
    _multiTracksPlayer = [[MultiTracksPlayer alloc] initWithTracksURLs:tracksURLs];
}

RCT_REMAP_METHOD(destroy,
            cleanUpWithResolver:
            (RCTPromiseResolveBlock) resolve
            rejecter:
            (RCTPromiseRejectBlock) reject) {
    _multiTracksPlayer = nil;
}

RCT_EXPORT_METHOD(play) {
    [_multiTracksPlayer play];
}

RCT_EXPORT_METHOD(pause) {
    [_multiTracksPlayer pause];
}

RCT_EXPORT_METHOD(getDisplayPosition:
    (RCTPromiseResolveBlock) resolve withReject:
    (RCTPromiseRejectBlock) reject) {
    resolve(@([_multiTracksPlayer getDisplayPosition]));
}

RCT_EXPORT_METHOD(setPosition:
    (double) second withAndStop:
    (BOOL) andStop withResolve:
    (RCTPromiseResolveBlock) resolve withReject:
    (RCTPromiseRejectBlock) reject) {
    [_multiTracksPlayer setPosition:second andStop:andStop];
}

RCT_EXPORT_METHOD(getTotalBufferedEndPercent:
    (RCTPromiseResolveBlock) resolve
            withReject:
            (RCTPromiseRejectBlock) reject) {
    resolve(@([_multiTracksPlayer getBufferedPercentage]));
}

RCT_EXPORT_METHOD(getBufferedEndPercent:
    (int) index withResolver:
    (RCTPromiseResolveBlock) resolve rejecter:
    (RCTPromiseRejectBlock) reject) {
    resolve([NSString stringWithFormat:@"%f", [_multiTracksPlayer getBufferedIndexPercentage:index]]);
}

RCT_EXPORT_METHOD(setVolume:
    (int) index left:
    (float) left right:
    (float) right withResolver:
    (RCTPromiseResolveBlock) resolve rejecter:
    (RCTPromiseRejectBlock) reject) {
    [_multiTracksPlayer setVolume:index left:left right:right];
    resolve(@"Instrument toogled");
}

RCT_REMAP_METHOD(setRate,
            rate:
            (float) rate
            withResolver:
            (RCTPromiseResolveBlock) resolve
            rejecter:
            (RCTPromiseRejectBlock) reject) {
    [_multiTracksPlayer setRate:rate];
}

RCT_REMAP_METHOD(setPitchShiftCents,
            pitchShiftCents:
            (int) pitchShiftCents
            withResolver:
            (RCTPromiseResolveBlock) resolve
            rejecter:
            (RCTPromiseRejectBlock) reject) {
    [_multiTracksPlayer setPitchShiftCents:pitchShiftCents];
}

@end
