// Oleg Petruny staff

#include "AudioPlayerAsync.h"
#include "UObject/ConstructorHelpers.h"

void UAudioPlayerAsync::workAsync_Implementation() {
	UGameplayStatics::PlaySound2D(this, this->Sound, this->volume, this->pitch, this->startTime);//NEED NEW PLAY FUNCTION
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, this->Sound->GetFName().ToString());
}

void UAudioPlayerAsync::CreateAudioPlayerAsync(USoundBase *Sound, FCallbackDelegate EndCallback, UAudioPlayer *&playerReference, float volume, float pitch, float startTime, int loops) {
	UAudioPlayer *player = NewObject<UAudioPlayer>();
	player->Sound = Sound;
	player->volume = volume;
	player->pitch = pitch;
	player->startTime = startTime;
	UAsyncLibraryAsync::CreateAsyncTaskWithFunctionName(player, FName("work"), EndCallback, player->asyncer, loops, 0, false);
	playerReference = player;
}

void UAudioPlayerAsync::StartAudioPlayerAsync() {
	this->startStamp = FDateTime::Now();
	this->asyncer->StartAsyncTask();
}

void UAudioPlayerAsync::StopAudioPlayerAsync() {}

float UAudioPlayerAsync::GetActualPlayTimeAsync() {
	return ((this->startStamp - FDateTime::Now() > 0) ? (this->startStamp - FDateTime::Now()).GetTotalSeconds() : 0.0f);
}