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


#include "AI_CompanionController.h"

#include "AI_EnemyController.h"
#include "CompanionCharacter.h"
#include "EndlessVendetta/EndlessVendettaCharacter.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"


AAI_CompanionController::AAI_CompanionController(FObjectInitializer const& ObjectInitializer)
{
	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	AAIController::SetGenericTeamId(FGenericTeamId(0));
	SetupPerceptionSystem();
}

// Called when the game starts or when spawned
void AAI_CompanionController::BeginPlay()
{
	Super::BeginPlay();
}

void AAI_CompanionController::OnPossess(APawn* InPawn)
{
	Super::OnPossess(InPawn);
	if (const ACompanionCharacter* CompanionCharacter = Cast<ACompanionCharacter>(InPawn))
	{
		if (UBehaviorTree* const BehaviorTree = CompanionCharacter->GetBehaviorTree())
		{
			UBlackboardComponent* TempBlackboardPtr;
			UseBlackboard(BehaviorTree->BlackboardAsset, TempBlackboardPtr);
			Blackboard = TempBlackboardPtr;
			RunBehaviorTree(BehaviorTree);
		}
	}
}

// Called every frame
void AAI_CompanionController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void AAI_CompanionController::SetupPerceptionSystem()
{
	SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
	if (IsValid(SightConfig))
	{
		SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));
		SightConfig->SightRadius = 2000.0f;
		SightConfig->LoseSightRadius = 2100.0f;
		SightConfig->PeripheralVisionAngleDegrees = 70.0f;
		SightConfig->SetMaxAge(20.0f);
		SightConfig->AutoSuccessRangeFromLastSeenLocation = 520.0f;
		SightConfig->DetectionByAffiliation.bDetectEnemies = true;
		SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
		SightConfig->DetectionByAffiliation.bDetectNeutrals = true;
		GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation());
		GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &AAI_CompanionController::OnTargetPerceptionUpdated);
		GetPerceptionComponent()->ConfigureSense(*SightConfig);
	}
}

void AAI_CompanionController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus const Stimulus)
{
	if (AEndlessVendettaCharacter* const PlayerCharacter = Cast<AEndlessVendettaCharacter>(Actor))
	{
		// if (Stimulus.WasSuccessfullySensed())
		// {
		// 	GetBlackboardComponent()->SetValueAsObject("TargetPlayer", Actor);
		// 	GetBlackboardComponent()->SetValueAsVector("TargetLocation", Stimulus.StimulusLocation);
		// 	GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", true);
		// }
		// else
		// {
		// 	GetBlackboardComponent()->ClearValue("TargetActor");
		// 	GetBlackboardComponent()->ClearValue("TargetLocation");
		// 	GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", false);
		// }
	}
}
