// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "OverlayMessageBox.h"
#include "ScreenOverlay.h"

void UOverlayMessageBoxItem::NativeConstruct()
{
	m_label = Cast<UTextBlock>(WidgetTree->FindWidget("Label"));
	onPressed.AddDynamic(this, &UOverlayMessageBoxItem::m_OnPressed);
	Super::NativeConstruct();
}
void UOverlayMessageBoxItem::SetText(const FString& text)
{
	if(!m_label)
	{
		GERROR("UOverlayMessageBoxItem Label not set on " + GetName());
		return;
	}
	m_label->SetText(FText::FromString(text));
}

void UOverlayMessageBoxItem::m_OnPressed()
{
	//Cast<UOverlayMessageBox>(GetSubMenu())->returnCode = index;
	//UMenuScreenBase* screen = GetSubMenu()->GetScreen();
	//check(m_info);
	m_info->Close(index);
	//if(screen)
	//	screen->CloseActiveSubMenu();
	//else
	//	GERROR("screen == nullptr, should not happen");
}

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

	m_buttonContainer = Cast<UHorizontalBox>(WidgetTree->FindWidget("ButtonContainer"));
	m_caption = Cast<UTextBlock>(WidgetTree->FindWidget("Caption"));
	m_message = Cast<UTextBlock>(WidgetTree->FindWidget("Message"));
}

void UOverlayMessageBox::SetMessageBoxInfo(class UMsgBoxInfo* info)
{
	if(!buttonItemClass)
	{
		GERROR("Button item class not set on " + GetName());
		return;
	}

	m_buttonContainer->ClearChildren();

	m_info = info;

	UOverlayMessageBoxItem* selectItem = 0;
	for(FString option : info->options)
	{
		UOverlayMessageBoxItem* item = CreateWidget<UOverlayMessageBoxItem>(GetWorld(), buttonItemClass);
		item->m_info = info;

		UHorizontalBoxSlot* slot = Cast<UHorizontalBoxSlot>(m_buttonContainer->AddChild(item));
		slot->SetHorizontalAlignment(HAlign_Fill);
		slot->SetVerticalAlignment(VAlign_Fill);
		slot->SetSize(FSlateChildSize(ESlateSizeRule::Fill));
		item->SetButtonText(option);
		if(option == info->defaultOption)
			selectItem = item;
		else if(!selectItem)
			selectItem = item;
	}

	m_caption->SetText(FText::FromString(info->caption));
	m_message->SetText(FText::FromString(info->message));

	RescanItems();
	SelectNewItem(selectItem);
}