// Oleg Petruny proprietary.

#include "ActivateInteractableModificator.h"

#include "EnhancedInputComponent.h"
#include "InputMappingContext.h"

#include "CommonFunctions.h"
#include "Interactable/Interactable.h"
#include "MainGameModeBase.h"
#include "Widgets/InteractableHintWidget.h"
#include "Widgets/WidgetsManager.h"

UActivateInteractableModificator::UActivateInteractableModificator(const FObjectInitializer& ObjectInitializer)
    : UInteractableModificator(ObjectInitializer)
{
    activatorTypes |= static_cast<uint8>(EActivatorType::Use);

    static ConstructorHelpers::FObjectFinder<UInputMappingContext> asset{ TEXT("/Script/EnhancedInput.InputMappingContext'/Game/Input/Interactables/IMC_InteractableActivate.IMC_InteractableActivate'") };
    inputMapping = asset.Object;
}

void UActivateInteractableModificator::Bind_Implementation(UEnhancedInputComponent* input)
{
    if(!input)
        return;

    inputMapping.LoadSynchronous();
    if(!inputMapping)
        return;

    for(auto& mapping : inputMapping->GetMappings())
    {
        auto handle = input->BindAction(mapping.Action, ETriggerEvent::Triggered, this, &UActivateInteractableModificator::ActivateInteractable).GetHandle();
        bindindingHandlers.Add(handle);
    }
    lastInput = input;
}

void UActivateInteractableModificator::Unbind_Implementation()
{
    if(!lastInput)
        return;

    for(auto handle : bindindingHandlers)
    {
        lastInput->RemoveBindingByHandle(handle);
    }
    bindindingHandlers.Empty();
}

void UActivateInteractableModificator::ActivateInteractable()
{
    AMainGameModeBase::GetWidgetsManager()->AnimateInteractionHint(this, 0, EInputAnimatedWidgetAnimation::Click);
    OnActivated.Broadcast();
}
