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


#include "Components/InteractionWidgetComponent.h"
#include "InteractionSettings.h"
#include "Interface/InteractableActorInterface.h"
#include "Widgets/BaseInteractionWidget.h"


UInteractionWidgetComponent::UInteractionWidgetComponent()
{
	PrimaryComponentTick.bCanEverTick = true;
	
	Space = EWidgetSpace::Screen;
	bManuallyRedraw = true;
	RedrawTime = 1.f;
	bDrawAtDesiredSize = true;
	Pivot = FVector2D(0.5f, 0.5f);
	TickMode = ETickMode::Disabled;
	if (const UInteractionSettings* InteractionSettings = GetDefault<UInteractionSettings>())
	{
		WidgetClass = InteractionSettings->DefaultInteractionWidgetClass;
	}
}

void UInteractionWidgetComponent::ToggleWidget(bool bActive)
{
	if (bActive)
	{
		if (UBaseInteractionWidget* InteractionWidget = Cast<UBaseInteractionWidget>(GetWidget()))
		{
			InteractionWidget->UpdateInfo(IInteractableActorInterface::Execute_GetInteractionText(GetOwner()));
		}
	}
	
	SetVisibility(bActive);
	RequestRenderUpdate();
}

void UInteractionWidgetComponent::BeginPlay()
{
	Super::BeginPlay();

	if (UBaseInteractionWidget* InteractionWidget = Cast<UBaseInteractionWidget>(GetWidget()))
	{
		InteractionWidget->UpdateInfo(IInteractableActorInterface::Execute_GetInteractionText(GetOwner()));
	}
	
	SetPivot(GetPivot() + WidgetOffset);
	ToggleWidget(false);
}


