// © 2025 Naked People Team. All Rights Reserved.


#include "AudioSettingsTab.h"

#include "Components/Slider.h"
#include "Kismet/GameplayStatics.h"
#include "NakedDesire/Global/AudioSettingsSubsystem.h"
#include "NakedDesire/Global/NakedDesireUserSettings.h"

void UAudioSettingsTab::NativeConstruct()
{
	Super::NativeConstruct();

	MasterSlider->OnValueChanged.AddUniqueDynamic(this, &UAudioSettingsTab::OnMasterChanged);
	MusicSlider->OnValueChanged.AddUniqueDynamic(this, &UAudioSettingsTab::OnMusicChanged);
	SfxSlider->OnValueChanged.AddUniqueDynamic(this, &UAudioSettingsTab::OnSfxChanged);

	RefreshFromSettings();
}

void UAudioSettingsTab::RefreshFromSettings()
{
	const UNakedDesireUserSettings* Settings = UNakedDesireUserSettings::GetNakedDesireUserSettings();
	if (!Settings)
		return;

	MasterSlider->SetValue(Settings->GetGlobalVolume());
	MusicSlider->SetValue(Settings->GetMusicVolume());
	SfxSlider->SetValue(Settings->GetSfxVolume());
}

void UAudioSettingsTab::OnMasterChanged(float Value)
{
	if (UNakedDesireUserSettings* Settings = UNakedDesireUserSettings::GetNakedDesireUserSettings())
		Settings->SetGlobalVolume(Value);
	ApplyLive();
}

void UAudioSettingsTab::OnMusicChanged(float Value)
{
	if (UNakedDesireUserSettings* Settings = UNakedDesireUserSettings::GetNakedDesireUserSettings())
		Settings->SetMusicVolume(Value);
	ApplyLive();
}

void UAudioSettingsTab::OnSfxChanged(float Value)
{
	if (UNakedDesireUserSettings* Settings = UNakedDesireUserSettings::GetNakedDesireUserSettings())
		Settings->SetSfxVolume(Value);
	ApplyLive();
}

void UAudioSettingsTab::ApplyLive()
{
	if (const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this))
	{
		if (UAudioSettingsSubsystem* Audio = GameInstance->GetSubsystem<UAudioSettingsSubsystem>())
			Audio->ApplyVolumes();
	}
}