#import "Track.h"

@implementation Track {
    Superpowered::AdvancedAudioPlayer *player;
}

- (id)initWithUri:(NSString *)uri withSamplerate:(unsigned int)samplerate {
    self = [super init];

    if (self) {
        player = new Superpowered::AdvancedAudioPlayer(samplerate, 0);
        player->open([uri cStringUsingEncoding:NSUTF8StringEncoding]);

        _leftVolume = _rightVolume = 1;
    }

    return self;
}

- (bool)processStereo:(Float32 *)buffer withMixed:(bool)mixed withNumberOfFrames:(unsigned int)numberOfFrames withVolume:(float)volume {
    return player->processStereo(buffer, mixed, numberOfFrames, volume);
}

- (bool)processLeft:(Float32 *)buffer withMixed:(bool)mixed withNumberOfFrames:(unsigned int)numberOfFrames {
    return [self processStereo:buffer withMixed:mixed withNumberOfFrames:numberOfFrames withVolume:_leftVolume];
}

- (bool)processRight:(Float32 *)buffer withMixed:(bool)mixed withNumberOfFrames:(unsigned int)numberOfFrames {
    return [self processStereo:buffer withMixed:mixed withNumberOfFrames:numberOfFrames withVolume:_rightVolume];
}

- (bool)processStereo:(Float32 *)buffer withMixed:(bool)mixed withNumberOfFrames:(unsigned int)numberOfFrames {
    return [self processLeft:buffer withMixed:mixed withNumberOfFrames:numberOfFrames];
}

- (void)setVolume:(float)left volumeRight:(float)right {
    _leftVolume = left;
    _rightVolume = right;
}

- (void)setOutputSamplerate:(unsigned int)samplerate {
    player->outputSamplerate = samplerate;
}

- (void)setSyncMode:(Superpowered::SyncMode)syncMode {
    player->syncMode = syncMode;
}

- (void)setPlaybackRate:(double)rate {
    player->playbackRate = rate;
}

- (void)play {
    player->play();
}

- (void)playSynchronized {
    player->playSynchronized();
}

- (void)pause {
    player->pause();
}

- (int)getDisplayPositionSeconds {
    return player->getDisplayPositionSeconds();
}

- (void)setPosition:(double)ms andStop:(bool)andStop {
    player->setPosition(ms, andStop, true, false, true);
}

- (Float32)getBufferedEndPercent {
    return player->getBufferedEndPercent();
}

@end
