// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "MenuButton.h"
#include "SubMenu.h"


FLinearColor uiIdleColor = FLinearColor(1.0f, 1.0f, 1.0f, 0.3f);
FLinearColor uiSelectedColor = FLinearColor(0.8f, 1.0f, 0.8f, 0.4f);
FLinearColor uiSelectedFocusedColor = FLinearColor(0.2f, 1.0f, 0.2f, 0.8f);

TSubclassOf<class UMenuButton> defaultButtonClass;

UMenuButton::UMenuButton(const FObjectInitializer& init)
	: Super(init)
{
	defaultButtonClass = ConstructorHelpers::FClassFinder<UMenuButton>(L"/Game/Assets/GUI/Components/WEEGEE_Button").Class;
}
void UMenuButton::NativeConstruct()
{
	m_button = WidgetTree->FindWidget<UButton>("Button");
	m_label = WidgetTree->FindWidget<UTextBlock>("Label");
	m_textOverlay = WidgetTree->FindWidget<UOverlay>("TextOverlay");
	if(!m_button)
	{
		GWERROR(L"Widget " + GetName() + L" does not contain a Button componenet");
	}
	else
	{
		m_button->OnClicked.AddDynamic(this, &UMenuButton::m_OnPressed);
	}


	NativeOnSelectionChanged(false, true);
	Super::NativeConstruct();

	if(m_textOverlay && m_label)
	{
		m_blurOverlay = NewObject<UOverlay>(GetWorld());
		UOverlaySlot* slot = m_textOverlay->AddChildToOverlay(m_blurOverlay);
		slot->SetHorizontalAlignment(EHorizontalAlignment::HAlign_Fill);
		slot->SetVerticalAlignment(EVerticalAlignment::VAlign_Fill);

		UOverlaySlot* labelSlot = Cast<UOverlaySlot>(m_label->Slot);
		EHorizontalAlignment textAlignH = labelSlot ? labelSlot->HorizontalAlignment.GetValue() : HAlign_Fill;
		EVerticalAlignment textAlignV = labelSlot ? labelSlot->VerticalAlignment.GetValue() : VAlign_Fill;

		m_label->RemoveFromParent();

		static int32 quality = 16;
		static float distance = 2.0f;
		for(int32 i = 0; i < quality; i++)
		{
			float r = (float)i / (float)(quality - 1);
			FVector2D offset = FVector2D(cos(r*PI), sin(r*PI)) * distance;
			// Create Glow effect
			UTextBlock* blurText = NewObject<UTextBlock>(GetWorld());
			UOverlaySlot* textSlot = m_blurOverlay->AddChildToOverlay(blurText);
			textSlot->SetHorizontalAlignment(textAlignH);
			textSlot->SetVerticalAlignment(textAlignV);

			FSlateFontInfo font = m_label->Font;
			blurText->SetFont(font);
			blurText->SetColorAndOpacity(FSlateColor(FLinearColor(0.6f, 0.6f, 1.0f, 0.5f)));
			blurText->SetRenderTranslation(offset);
			blurText->SetText(m_label->GetText());
		}
		m_blurOverlay->SetVisibility(ESlateVisibility::Hidden);

		labelSlot = m_textOverlay->AddChildToOverlay(m_label);
		labelSlot->SetHorizontalAlignment(textAlignH);
		labelSlot->SetVerticalAlignment(textAlignV);
	}
}

void UMenuButton::FocusTick(float DeltaTime)
{
	Super::FocusTick(DeltaTime);
	if(m_button && m_button->IsHovered() && GetSubMenu()->GetSelectedItem() != index)
	{
		GetSubMenu()->SelectNewItemByIndex(index);
	}
}

void UMenuButton::m_OnPressed()
{
	if(HasFocus())
	{
		NativeOnPressed(false);
	}
}
void UMenuButton::NativeOnMenuAction(EMenuActionBinding binding)
{
	Super::NativeOnMenuAction(binding);
	if(binding == EMenuActionBinding::Confirm)
		NativeOnPressed(true);
}

void UMenuButton::NativeOnSelectionChanged(bool selected, bool controller)
{
	Super::NativeOnSelectionChanged(selected, controller);
	if(!m_button)
		return;
	
	UScrollBox* sb = Cast<UScrollBox>(Slot->Parent);
	if(sb && selected)
	{
		sb->ScrollWidgetIntoView(this, true);
	}

	if(m_blurOverlay)
	{
		if(selected)
		{
			m_blurOverlay->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
		}
		else
		{
			m_blurOverlay->SetVisibility(ESlateVisibility::Hidden);
		}
	}
}

void UMenuButton::NativeOnPressed(bool controllerInput)
{
	// Simulate OnSelectionConfirmed event if this button is pressed by a mouse
	GetSubMenu()->NativeOnSelectionConfirmed(this);
	onPressed.Broadcast();
}

void UMenuButton::SetButtonText(FString textStr)
{
	FText text = FText::FromString(textStr);
	if(m_label)
		m_label->SetText(text);
	if(m_blurOverlay)
	{
		for(int32 i = 0; i < m_blurOverlay->GetChildrenCount(); i++)
		{
			UTextBlock* tb = Cast<UTextBlock>(m_blurOverlay->GetChildAt(i));
			if(tb)
			{
				tb->SetText(text);
			}
		}
	}
}
