// Oleg Petruny proprietary.

#include "InCameraInteractableActivator.h"

#include "CommonFunctions.h"
#include "Interactable/Interactable.h"
#include "InteractableScreenCapturer.h"

UInCameraInteractableActivator::UInCameraInteractableActivator(const FObjectInitializer& ObjectInitializer)
    : UInteractableActivator(ObjectInitializer)
{
    if(UCommonFunctions::IsNonGameObject(this))
        return;

    if(!world || !player)
    {
        return;
    }

    activatorType = EActivatorType::Saw;
    scanDistance = 7000;

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

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

void UInCameraInteractableActivator::OnRegister()
{
    Super::OnRegister();
    capturer->RegisterComponent();
    capturer->Activate();
}

void UInCameraInteractableActivator::NewSeenInteractable(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);
            OnNewSeenInteractable(interactable);
        }
    }
}