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


#include "SettingsAudioWidget.h"

#include "SettingRowWidget.h"
#include "SurvivalUserSettings.h"

#define LOCTEXT_NAMESPACE "Reglages"

namespace SettingsAudioTab
{
	/** La seule liste de choix de cet onglet. */
	TArray<FText> MakeMuteAudibleLabels()
	{
		return { LOCTEXT("Muet", "Muet"), LOCTEXT("Audible", "Audible") };
	}
}

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

	using namespace SurvivalSettingsRange;
	using namespace SettingsAudioTab;

	const FText PercentSuffix = LOCTEXT("Pourcent", " %");

	if (MasterVolumeRow)
	{
		MasterVolumeRow->SetupAsNumber(LOCTEXT("VolumeGeneral", "Volume général"),
			VolumeMin, VolumeMax, VolumeStep, VolumeArrowStep, VolumeMin, PercentSuffix);
		MasterVolumeRow->OnIndexChanged.AddDynamic(this, &USettingsAudioWidget::HandleMasterVolumeChanged);
	}

	if (MusicVolumeRow)
	{
		MusicVolumeRow->SetupAsNumber(LOCTEXT("Musique", "Musique"),
			VolumeMin, VolumeMax, VolumeStep, VolumeArrowStep, VolumeMin, PercentSuffix);
		MusicVolumeRow->OnIndexChanged.AddDynamic(this, &USettingsAudioWidget::HandleMusicVolumeChanged);
	}

	if (EffectsVolumeRow)
	{
		// Cle "EffetsSonores" et pas "Effets" : l'onglet Graphismes utilise deja
		// cette derniere pour la qualite des effets visuels, dans le meme namespace.
		EffectsVolumeRow->SetupAsNumber(LOCTEXT("EffetsSonores", "Effets sonores"),
			VolumeMin, VolumeMax, VolumeStep, VolumeArrowStep, VolumeMin, PercentSuffix);
		EffectsVolumeRow->OnIndexChanged.AddDynamic(this, &USettingsAudioWidget::HandleEffectsVolumeChanged);
	}

	if (UiVolumeRow)
	{
		UiVolumeRow->SetupAsNumber(LOCTEXT("Interface", "Interface"),
			VolumeMin, VolumeMax, VolumeStep, VolumeArrowStep, VolumeMin, PercentSuffix);
		UiVolumeRow->OnIndexChanged.AddDynamic(this, &USettingsAudioWidget::HandleUiVolumeChanged);
	}

	if (BackgroundSoundRow)
	{
		BackgroundSoundRow->SetupAsChoice(LOCTEXT("SonEnArrierePlan", "Jeu en arrière-plan"),
			MakeMuteAudibleLabels(), 0);
		BackgroundSoundRow->OnIndexChanged.AddDynamic(this, &USettingsAudioWidget::HandleBackgroundSoundChanged);
	}

	RefreshFromSettings();
}

void USettingsAudioWidget::RefreshFromSettings()
{
	USurvivalUserSettings* Settings = USurvivalUserSettings::Get();
	if (!Settings)
	{
		UE_LOG(LogTemp, Error, TEXT("USettingsAudioWidget : GameUserSettingsClassName n'est pas configure dans DefaultEngine.ini, l'onglet restera inerte."));
		return;
	}

	// SetNumericValueSilently : diffuser ici ferait boucler l'onglet sur lui-meme.
	if (MasterVolumeRow)
	{
		MasterVolumeRow->SetNumericValueSilently(Settings->GetMasterVolume());
	}

	if (MusicVolumeRow)
	{
		MusicVolumeRow->SetNumericValueSilently(Settings->GetMusicVolume());
	}

	if (EffectsVolumeRow)
	{
		EffectsVolumeRow->SetNumericValueSilently(Settings->GetEffectsVolume());
	}

	if (UiVolumeRow)
	{
		UiVolumeRow->SetNumericValueSilently(Settings->GetUiVolume());
	}

	if (BackgroundSoundRow)
	{
		BackgroundSoundRow->SetIndexSilently(Settings->GetPlayInBackground() ? 1 : 0);
	}
}

void USettingsAudioWidget::ResetToDefaults()
{
	USurvivalUserSettings* Settings = USurvivalUserSettings::Get();
	if (!Settings)
	{
		return;
	}

	Settings->SetSurvivalAudioToDefaults();
	Settings->ApplyAudioSettings();
	RefreshFromSettings();
}

void USettingsAudioWidget::HandleMasterVolumeChanged(int32 NewIndex)
{
	USurvivalUserSettings* Settings = USurvivalUserSettings::Get();
	if (!Settings || !MasterVolumeRow)
	{
		return;
	}

	Settings->SetMasterVolume(MasterVolumeRow->GetNumericValue());

	// Pas de SaveSettings ici : c'est l'ecran de reglages qui ecrit le .ini en se
	// fermant. Meme regle que les autres onglets.
	Settings->ApplyAudioSettings();
}

void USettingsAudioWidget::HandleMusicVolumeChanged(int32 NewIndex)
{
	USurvivalUserSettings* Settings = USurvivalUserSettings::Get();
	if (!Settings || !MusicVolumeRow)
	{
		return;
	}

	Settings->SetMusicVolume(MusicVolumeRow->GetNumericValue());
	Settings->ApplyAudioSettings();
}

void USettingsAudioWidget::HandleEffectsVolumeChanged(int32 NewIndex)
{
	USurvivalUserSettings* Settings = USurvivalUserSettings::Get();
	if (!Settings || !EffectsVolumeRow)
	{
		return;
	}

	Settings->SetEffectsVolume(EffectsVolumeRow->GetNumericValue());
	Settings->ApplyAudioSettings();
}

void USettingsAudioWidget::HandleUiVolumeChanged(int32 NewIndex)
{
	USurvivalUserSettings* Settings = USurvivalUserSettings::Get();
	if (!Settings || !UiVolumeRow)
	{
		return;
	}

	// Aucun son de test a jouer : les fleches de la ligne portent deja le son de
	// clic de l'interface, donc le joueur entend le reglage qu'il est en train de
	// faire, sur le son exact qu'il regle.
	Settings->SetUiVolume(UiVolumeRow->GetNumericValue());
	Settings->ApplyAudioSettings();
}

void USettingsAudioWidget::HandleBackgroundSoundChanged(int32 NewIndex)
{
	if (USurvivalUserSettings* Settings = USurvivalUserSettings::Get())
	{
		Settings->SetPlayInBackground(NewIndex == 1);
		Settings->ApplyAudioSettings();
	}
}

#undef LOCTEXT_NAMESPACE
