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

#pragma once

#include "CoreMinimal.h"
#include "Subsystems/WorldSubsystem.h"
#include "Tickable.h"
#include "Stats/Stats.h"

#include "InteractionSubsystem.generated.h"

/**
 * 
 */
UCLASS()
class INTERACTIONSYSTEM_API UInteractionSubsystem : public UWorldSubsystem, public FTickableGameObject
{
	GENERATED_BODY()
	
public:
	
	virtual void Initialize(FSubsystemCollectionBase& Collection) override;
	virtual void Deinitialize() override;
	virtual bool ShouldCreateSubsystem(UObject* Outer) const override;
	virtual void OnWorldBeginPlay(UWorld& InWorld) override;
	virtual bool IsTickable() const override { return !IsTemplate(); }
	virtual void Tick(float DeltaTime) override;
	virtual TStatId GetStatId() const override { RETURN_QUICK_DECLARE_CYCLE_STAT(UInteractionSubsystem, STATGROUP_Tickables); }
	
	static AActor* GetClosestInteractable(const UObject* WorldContext);
	
	UFUNCTION(BlueprintCallable, Category = "Interactable")
	static void UpdateInteractableText(AActor* Interactable);
	
protected:
	
	UFUNCTION()
	void RescanWorld();
	
	void RegisterInteractableActor(AActor* InteractableActor);
	void UnregisterInteractableActor(AActor* InteractableActor);
	
	UPROPERTY()
	TArray<TWeakObjectPtr<AActor>> InteractableActorList;
	
	UPROPERTY()
	TWeakObjectPtr<AActor> CurrentlyInteractableObject;
};
