// Oleg Petruny proprietary.


#include "InCameraInteractableActivator.h"

#include "InteractableScreenCapturer.h"

UInCameraInteractableActivator::UInCameraInteractableActivator(const FObjectInitializer& ObjectInitializer)
    : UInteractableActivator(ObjectInitializer)
{
    if(!world || !player)
    {
        return;
    }

    activatorType = EActivatorType::Saw;
    scanDistance = 7000;

    _capturer = CreateDefaultSubobject<UInteractableScreenCapturer>(TEXT("UInCameraInteractableActivator_UInteractableScreenCapturer"));
    _capturer->interactableInScreenDelegate.BindUObject(this, &UInCameraInteractableActivator::NewSeenInteractable_Implementation);
    _capturer->SetupAttachment(this);
    _capturer->scanDistance = scanDistance;

    PrimaryComponentTick.bCanEverTick = true;
    PrimaryComponentTick.bStartWithTickEnabled = false;
}

void UInCameraInteractableActivator::OnRegister()
{
    UInteractableActivator::OnRegister();
    _capturer->RegisterComponent();
    _capturer->Activate();
}

void UInCameraInteractableActivator::NewSeenInteractable_Implementation(AInteractable* interactable)
{
    _interactablesToActivate.Enqueue(interactable);
    SetComponentTickEnabled(true);
}

void UInCameraInteractableActivator::Scan_Implementation()
{
    SetComponentTickEnabled(false);
    while(!_interactablesToActivate.IsEmpty())
    {
        AInteractable* interactable;
        _interactablesToActivate.Dequeue(interactable);
        if(interactableActivatedDelegate.IsBound())
        {
            interactableActivatedDelegate.Execute(interactable, activatorType);
        }
    }
}