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


#include "BountyClass.h"

// Sets default values
ABountyClass::ABountyClass()
{
 	// 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;

}

void ABountyClass::BeginPlay()
{
	Super::BeginPlay();

	SpawnCheckpoints();
	
}

void ABountyClass::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ABountyClass::SpawnCheckpoints()
{
	// Spawn all checkpoints associated with this bounty and store them in order
	FActorSpawnParameters SpawnParameters;
	SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	
	for(TSubclassOf<ACheckpointClass> CheckpointClass : CheckpointsToSpawn)
	{
		if (CheckpointClass == nullptr)
		{
			// An unassigned checkpoint class can lead to undefined behaviour, hence the fatal log
			UE_LOG(LogTemp, Fatal, TEXT("A checkpoint class wasn't set for %s"), *BountyTitle);
			return;
		}

		FVector Loc = CheckpointClass.GetDefaultObject()->GetCheckpointSpawnTransform().GetLocation();
		FRotator Rot = CheckpointClass.GetDefaultObject()->GetCheckpointSpawnTransform().GetRotation().Rotator();
		ACheckpointClass* SpawnedCheckpoint = Cast<ACheckpointClass>(GetWorld()->SpawnActor<AActor>(CheckpointClass, Loc, Rot, SpawnParameters));
		BountyCheckpoints.Add(SpawnedCheckpoint);
	}

	// Activate the first checkpoint and listen for its completion
	BountyCheckpoints[0]->Active = true;
	BountyCheckpoints[0]->SpawnWaypoint(BountyTitle);
	BountyCheckpoints[0]->CheckpointActivated();
	BountyCheckpoints[0]->CompletedCheckpoint.AddDynamic(this, &ABountyClass::IncrementBountyCheckpoint);
}

void ABountyClass::IncrementBountyCheckpoint()
{
	// Broadcast that the first Checkpoint has been completed so that side bounties can be destroyed
	if (!BountyStarted)
	{
		BountyStarted = true;
		CompletedFirstCheckpoint.Broadcast();
	}

	// Bounty Completion Condition
	if (BountyCheckpoints.Num() <= 1)
	{
		Completed = true;
		BountyCheckpoints[0]->Active = false;
		BountyCheckpoints[0]->Destroy();
		BountyCheckpoints.RemoveAt(0);
		UE_LOG(LogTemp, Warning, TEXT(" You've Completed the Bounty!! Well done"));
		return;
	}

	if (BountyCheckpoints[0] == nullptr)
	{
		UE_LOG(LogTemp, Fatal, TEXT("Missing checkpoint in bounty checkpoints, could've failed to spawn or cast"));
		return;
	}

	// Destroy Actor and Shrink Array
	BountyCheckpoints[0]->Active = false;
	BountyCheckpoints[0]->Destroy();
	BountyCheckpoints.RemoveAt(0);

	// Set the new checkpoint in pos 0 to be active and listen for it's completion
	BountyCheckpoints[0]->Active = true;
	BountyCheckpoints[0]->SpawnWaypoint(BountyTitle);
	BountyCheckpoints[0]->CheckpointActivated();
	BountyCheckpoints[0]->CompletedCheckpoint.AddDynamic(this, &ABountyClass::IncrementBountyCheckpoint);
}

void ABountyClass::CollectRewards_Implementation()
{
	UE_LOG(LogTemp, Warning, TEXT("The player has gained $%d for completing the bounty!"), RewardMoney);
}




