// Fill out your copyright notice in the Description page of Project Settings.

#include "SoundManager.h"

// Engine headers.
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"

// Custom game headers.
#include "GameJamTemplate.h"

USoundManager::USoundManager():
	ObjectName(GetName())
{
	VolumeMap.Add(EGameSoundType::Effect, 1.0f);
	VolumeMap.Add(EGameSoundType::Music, 1.0f);
}

float USoundManager::GetSoundVolume(EGameSoundType SoundType)
{
	float* SoundVolume = VolumeMap.Find(SoundType);

	if (SoundVolume != nullptr)
	{
		return *SoundVolume;
	}
	else
	{
		UE_LOG(LogGameJam, Warning,
			TEXT("[%s] Sound type '%d' does not exist."),
			*ObjectName, (uint8)SoundType);
		return 0.0f;
	}
}

void USoundManager::SetSoundVolume(EGameSoundType SoundType, float NewVolume)
{
	float NormalizedVolume = FMath::Clamp(NewVolume, 0.0f, 1.0f);
	float& SoundVolume = VolumeMap.FindOrAdd(SoundType);
	SoundVolume = NormalizedVolume;
}

void USoundManager::PlaySound(EGameSoundType SoundType, USoundCue* SoundCue, FVector SoundLocation)
{
	if (SoundCue != nullptr)
	{
		float* SoundVolume = VolumeMap.Find(SoundType);

		if (SoundVolume != nullptr)
		{
			SoundCue->VolumeMultiplier = *SoundVolume;
		}
		else
		{
			UE_LOG(LogGameJam, Warning,
				TEXT("[%s] Sound type '%d' does not exist."),
				*ObjectName, (uint8)SoundType);
		}

		UGameplayStatics::PlaySoundAtLocation(GetWorld(), (USoundBase*)SoundCue, SoundLocation);
	}
}
