// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "SoundEffect.h"
#include "DefaultGameInstance.h"
#include "Prefs.h"
#include "SoundFunctionLibrary.h"

ASoundEffect* USoundFunctionLibrary::CreateSoundEffect(USoundBase* sndClass, AActor* spawner)
{
	if (!sndClass)
	{
		JERROR("Sound class was not assigned when spawning Sound Effect");
		return nullptr;
	}
	if (!spawner)
	{
		JERROR("Spawner was not assigned when spawning Sound Effect");
		return nullptr;
	}

	UWorld* const world = spawner->GetWorld();

	FTransform transform;
	transform.SetIdentity();
	transform.TransformPosition(spawner->GetActorLocation());
	ASoundEffect* sound = world->SpawnActorDeferred<ASoundEffect>(ASoundEffect::StaticClass(), transform);
	if (sound)
	{
		sound->Init(sndClass);
		UGameplayStatics::FinishSpawningActor(sound, transform);
	}
	else
	{
		GWERROR(L"Failed to CreateSoundEffect()");
	}
	return sound;
}


float USoundFunctionLibrary::GetSFXVolume(UDefaultGameInstance* gameInstance)
{
	if (!gameInstance || !gameInstance->IsA(UDefaultGameInstance::StaticClass())) { JERROR("GameInstance was not assigned getting Sound Volume"); return 0; }
	return Cast<UDefaultGameInstance>(gameInstance)->GetPrefs()->sfxVolume;
}
float USoundFunctionLibrary::GetMusicVolume(UDefaultGameInstance* gameInstance)
{
	if (!gameInstance || !gameInstance->IsA(UDefaultGameInstance::StaticClass())) { JERROR("GameInstance was not assigned getting Music Volume"); return 0; }
	return Cast<UDefaultGameInstance>(gameInstance)->GetPrefs()->musicVolume;
}