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


#include "PauseMenuWidget.h"

#include "Components/Button.h"
#include "FpsPlayerController.h"

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

	if (ResumeButton)
	{
		ResumeButton->OnClicked.AddDynamic(this, &UPauseMenuWidget::HandleResumeClicked);
	}

	if (QuitToMenuButton)
	{
		QuitToMenuButton->OnClicked.AddDynamic(this, &UPauseMenuWidget::HandleQuitToMenuClicked);
	}

	if (SettingsButton)
	{
		SettingsButton->OnClicked.AddDynamic(this, &UPauseMenuWidget::HandleSettingsClicked);
	}
}

void UPauseMenuWidget::NativeDestruct()
{
	if (ResumeButton)
	{
		ResumeButton->OnClicked.RemoveDynamic(this, &UPauseMenuWidget::HandleResumeClicked);
	}

	if (SettingsButton)
	{
		SettingsButton->OnClicked.RemoveDynamic(this, &UPauseMenuWidget::HandleSettingsClicked);
	}

	if (QuitToMenuButton)
	{
		QuitToMenuButton->OnClicked.RemoveDynamic(this, &UPauseMenuWidget::HandleQuitToMenuClicked);
	}

	Super::NativeDestruct();
}

void UPauseMenuWidget::HandleSettingsClicked()
{
	if (AFpsPlayerController* Controller = GetOwningPlayer<AFpsPlayerController>())
	{
		Controller->OpenSettings();
	}
}

void UPauseMenuWidget::HandleResumeClicked()
{
	if (AFpsPlayerController* Controller = GetOwningPlayer<AFpsPlayerController>())
	{
		Controller->ResumeGame();
	}
}

void UPauseMenuWidget::HandleQuitToMenuClicked()
{
	if (AFpsPlayerController* Controller = GetOwningPlayer<AFpsPlayerController>())
	{
		Controller->RequestQuitToMainMenu();
	}
}
