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


#include "MainMenuWidget.h"
#include "ConfirmModalWidget.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetSystemLibrary.h"
#include "NakedDesire/SaveGame/SaveSubsystem.h"
#include "NakedDesire/UI/TextButton.h"
#include "Widgets/CommonActivatableWidgetContainer.h"

#define LOCTEXT_NAMESPACE "MainMenu"

UMainMenuWidget::UMainMenuWidget()
{
	// Sensible default for the slice; override per-asset on WBP_MainMenu.
	GameplayMap = TSoftObjectPtr<UWorld>(FSoftObjectPath(TEXT("/Game/Test/Maps/L_TestCity.L_TestCity")));
}

void UMainMenuWidget::NativeOnActivated()
{
	Super::NativeOnActivated();

	// Continue is only available when a save exists in the active slot.
	bool bHasSave = false;
	if (const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this))
	{
		if (const USaveSubsystem* SaveSubsystem = GameInstance->GetSubsystem<USaveSubsystem>())
			bHasSave = SaveSubsystem->DoesSaveExist();
	}
	
	ContinueButton->SetIsEnabled(bHasSave);

	ContinueButton->OnClicked().AddUObject(this, &UMainMenuWidget::OnContinueClicked);
	NewGameButton->OnClicked().AddUObject(this, &UMainMenuWidget::OnNewGameClicked);
	SettingsButton->OnClicked().AddUObject(this, &UMainMenuWidget::OnSettingsClicked);
	CreditsButton->OnClicked().AddUObject(this, &UMainMenuWidget::OnCreditsClicked);
	QuitButton->OnClicked().AddUObject(this, &UMainMenuWidget::OnQuitClicked);
}

void UMainMenuWidget::OnContinueClicked()
{
	OpenGameplayLevel();
}

void UMainMenuWidget::OnNewGameClicked()
{
	if (!ConfirmModalClass || !ModalStack)
	{
		HandleNewGameConfirmed();
		return;
	}

	UConfirmModalWidget* Modal = ModalStack->AddWidget<UConfirmModalWidget>(ConfirmModalClass);
	if (!Modal)
		return;

	Modal->Setup(
		LOCTEXT("NewGameTitle", "New Game"),
		LOCTEXT("NewGameBody", "Start a new game? Any existing progress will be lost."));
	Modal->OnConfirmed.AddUObject(this, &UMainMenuWidget::HandleNewGameConfirmed);
}

void UMainMenuWidget::OnSettingsClicked()
{
	if (SettingsWidgetClass && ModalStack)
		ModalStack->AddWidget(SettingsWidgetClass);
}

void UMainMenuWidget::OnCreditsClicked()
{
	if (CreditsWidgetClass && ModalStack)
		ModalStack->AddWidget(CreditsWidgetClass);
}

void UMainMenuWidget::OnQuitClicked()
{
	if (!ConfirmModalClass || !ModalStack)
	{
		HandleQuitConfirmed();
		return;
	}

	UConfirmModalWidget* Modal = ModalStack->AddWidget<UConfirmModalWidget>(ConfirmModalClass);
	if (!Modal)
		return;

	Modal->Setup(
		LOCTEXT("QuitTitle", "Quit Game"),
		LOCTEXT("QuitBody", "Quit to desktop?"));
	Modal->OnConfirmed.AddUObject(this, &UMainMenuWidget::HandleQuitConfirmed);
}

void UMainMenuWidget::HandleNewGameConfirmed()
{
	if (UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this))
	{
		if (USaveSubsystem* SaveSubsystem = GameInstance->GetSubsystem<USaveSubsystem>())
			SaveSubsystem->StartNewGame();
	}

	OpenGameplayLevel();
}

void UMainMenuWidget::HandleQuitConfirmed()
{
	UKismetSystemLibrary::QuitGame(this, GetOwningPlayer(), EQuitPreference::Quit, false);
}

void UMainMenuWidget::OpenGameplayLevel()
{
	if (GameplayMap.IsNull())
		return;
	
	if (APlayerController* PC = UGameplayStatics::GetPlayerController(GetWorld(), 0))
	{
		PC->SetShowMouseCursor(false);

		const FInputModeGameOnly InputMode;
		PC->SetInputMode(InputMode);
	}

	UGameplayStatics::OpenLevelBySoftObjectPtr(this, GameplayMap);
}

#undef LOCTEXT_NAMESPACE