// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "MenuItemBase.h"
#include "SubMenu.h"
#include "PlayerControllerBase.h"
#include "ScreenOverlay.h"

void UMenuItemBase::NativeConstruct()
{
	m_selected = false;
	blockInput = false;
	m_subMenu = nullptr;
	priority = 0;
	Super::NativeConstruct();
}
void UMenuItemBase::NativeDestruct()
{
	Super::NativeDestruct();
}

void UMenuItemBase::FocusTick(float DeltaTime)
{

}

UMsgBoxInfo* UMenuItemBase::ShowMessageBox(FOverlayItemClosed cb, FString caption, FString message, TArray<FString> options, FString defaultOption)
{
	UScreenOverlay* overlay = Cast<APlayerControllerBase>(GetOwningPlayer())->overlay;
	if(!overlay)
		return nullptr;
	return overlay->ShowMessageBoxCallback(cb, caption, message, options, defaultOption);
}

void UMenuItemBase::NativeOnSelectionChanged(bool selected, bool controller)
{
	if(selected != m_selected)
	{
		m_selected = selected;
		onSelectionChanged.Broadcast(selected);
	}
}

bool UMenuItemBase::IsSelected() const
{
	return m_selected;
}

class USubMenu* UMenuItemBase::GetSubMenu() const
{
	return m_subMenu;
}

bool UMenuItemBase::HasFocus() const
{
	return GetSubMenu() && GetSubMenu()->HasFocus();
}

void UMenuItemBase::AddActionBinding(EMenuActionBinding binding, FMenuAction action)
{
	m_actionBindings.Add(binding, action);
}

void UMenuItemBase::ClearActionBinding(EMenuActionBinding binding)
{
	m_actionBindings.Remove(binding);
}

void UMenuItemBase::OnMenuAction_Implementation(EMenuActionBinding binding)
{
}
void UMenuItemBase::NativeOnMenuAction(EMenuActionBinding binding)
{
	OnMenuAction(binding);

	TArray<FMenuAction> actions;
	m_actionBindings.MultiFind(binding, actions);
	for(int32 i = 0; i < actions.Num(); i++)
	{
		actions[i].Execute();
	}
}

FText UMenuItemBase::GetDescription_Implementation()
{
	return FText();
}

FText UMenuItemBase::GetTitle_Implementation()
{
	return FText();
}
