// Oleg Petruny proprietary.

#include "InteractableActivator.h"

#include "Engine/CollisionProfile.h"

#include "Interactable/Interactable.h"
#include "PlayerBase.h"

UInteractableActivator::UInteractableActivator(const FObjectInitializer& ObjectInitializer)
    : USceneComponent(ObjectInitializer)
{
    if(GetClass() != UInteractableActivator::StaticClass())
    {
        AInteractable::AppendActivatorClass(GetClass());
    }

    if(HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))
        return;

    world = GetWorld();
    player = Cast<APlayerBase>(GetOwner());

    collisionChannel = ECC_GameTraceChannel1;
    if(auto collisions = UCollisionProfile::Get())
    {
        for(int32 i = 0; i < ECC_MAX; ++i)
        {
            if(collisions->ReturnChannelNameFromContainerIndex(i) == FName{ TEXT("Interactable") })
            {
                collisionChannel = (ECollisionChannel)i;
                break;
            }
        }
    }

    if(!world || !player || collisionChannel == ECC_MAX)
    {
        DestroyComponent();
        return;
    }

    PrimaryComponentTick.TickInterval = 0.05f;
    PrimaryComponentTick.bCanEverTick = true;
    PrimaryComponentTick.bStartWithTickEnabled = true;
}

void UInteractableActivator::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    switch(TickType)
    {
        case ELevelTick::LEVELTICK_TimeOnly:
        case ELevelTick::LEVELTICK_ViewportsOnly:
        case ELevelTick::LEVELTICK_All:
            USceneComponent::TickComponent(DeltaTime, TickType, ThisTickFunction);
            Scan();
            break;
        default:
            break;
    }
}
