// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "ToolTipWidget.h"
#include "ToolTipText.h"
#include "WidgetLayoutLibrary.h"


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

	m_toolTip = CreateWidget<UToolTipText>(GetWorld(), textWidget);
	canvas->AddChild(m_toolTip);
	m_toolTip->SetVisibility(ESlateVisibility::Hidden);

	showTooltip = false;
}
void UToolTipWidget::NativeDestruct()
{
	Super::NativeDestruct();
}

void UToolTipWidget::NativeTick(const FGeometry& geometry, float deltaTime)
{
	Super::NativeTick(geometry, deltaTime);

	if (!showTooltip)
	{
		m_toolTip->SetVisibility(ESlateVisibility::Hidden);
	}
	else
	{
		UWorld* const world = GetWorld();
		if (!IsValid(world)) return;

		const FVector2D screenSize = FVector2D(world->GetGameViewport()->Viewport->GetSizeXY().X, world->GetGameViewport()->Viewport->GetSizeXY().Y);
		const float viewportScale = UWidgetLayoutLibrary::GetViewportScale(this);

		m_toolTip->SetVisibility(ESlateVisibility::Visible);

		FVector2D currentPos = position + size + FVector2D(5, 5);

		if ((currentPos.X + m_toolTip->GetDesiredSize().X) * viewportScale >= screenSize.X)
			currentPos.X = position.X - 5 - m_toolTip->GetDesiredSize().X;
		if ((currentPos.Y + m_toolTip->GetDesiredSize().Y) * viewportScale >= screenSize.Y)
			currentPos.Y = position.Y - 5 - m_toolTip->GetDesiredSize().Y;

		UCanvasPanelSlot* const slot = Cast<UCanvasPanelSlot>(m_toolTip->Slot);
		slot->SetPosition(currentPos);
		slot->SetSize(m_toolTip->GetDesiredSize());
	}

	//UWorld* const world = GetWorld();
	//if (!IsValid(world)) return;
	//
	//FVector2D mousePos;
	//const FVector2D screenSize = FVector2D(world->GetGameViewport()->Viewport->GetSizeXY().X, world->GetGameViewport()->Viewport->GetSizeXY().Y);
	//const float viewportScale = UWidgetLayoutLibrary::GetViewportScale(this);
	//if (UWidgetLayoutLibrary::GetMousePositionScaledByDPI(GetOwningPlayer(), mousePos.X, mousePos.Y))
	//{
	//	for (int32 i = 0; i < m_handles.Num(); i++)
	//	{
	//		FToolTipHandle& handle = *m_handles[i];
	//		UToolTipText& toolTip = *handle.m_toolTip;
	//
	//		FVector2D triggerTopLeft;
	//		FVector2D triggerSize;
	//		const FVector2D scaledMouse = mousePos * viewportScale;
	//		const FVector2D scaledSize = handle.size * viewportScale;
	//		const FVector2D scaledPosition = handle.position * viewportScale;
	//
	//		triggerTopLeft = handle.position;
	//		triggerSize = handle.size;
	//
	//		mousePos = (scaledMouse - scaledPosition) / scaledSize;
	//
	//		if (!(mousePos.X >= 0 && mousePos.X <= 1 && mousePos.Y >= 0 && mousePos.Y <= 1))
	//		{
	//			toolTip.SetVisibility(ESlateVisibility::Hidden);
	//			return;
	//		}
	//
	//		UCanvasPanelSlot* const slot = Cast<UCanvasPanelSlot>(toolTip.Slot);
	//
	//		toolTip.SetVisibility(ESlateVisibility::Visible);
	//
	//		FVector2D currentPos = triggerTopLeft + triggerSize + FVector2D(5, 5);
	//
	//		if ((currentPos.X + toolTip.GetDesiredSize().X) * viewportScale >= screenSize.X)
	//			currentPos.X = triggerTopLeft.X - 5 - toolTip.GetDesiredSize().X;
	//		if ((currentPos.Y + toolTip.GetDesiredSize().Y) * viewportScale >= screenSize.Y)
	//			currentPos.Y = triggerTopLeft.Y - 5 - toolTip.GetDesiredSize().Y;
	//
	//		slot->SetPosition(currentPos);
	//		slot->SetSize(toolTip.GetDesiredSize());
	//	}
	//}
}

void UToolTipWidget::SetTitle(const FString& title)
{
	m_toolTip->SetTitle(title);
}
void UToolTipWidget::SetText(const FString& text)
{
	m_toolTip->SetText(text);
}