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

#pragma once

#include "CoreMinimal.h"
#include "CheckpointClass.h"
#include "GameFramework/Actor.h"
#include "BountyClass.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FCompletedFirstCheckpoint);

UCLASS()
class ENDLESSVENDETTA_API ABountyClass : public AActor
{
	GENERATED_BODY()

	// Used to temp store value of incoming replacement checkpoint class
	TSubclassOf<ACheckpointClass> ReplacementCheckpointClass;
	
	bool BountyStarted = false;

protected:
	// ------- Properties Set in Editor --------------
	UPROPERTY(EditDefaultsOnly, Category = "Bounty")
	TArray<TSubclassOf<ACheckpointClass>> CheckpointsToSpawn;
          
	UPROPERTY(EditDefaultsOnly, Category = "Bounty")
	int RewardMoney = 0;

	UPROPERTY(EditDefaultsOnly, Category = "Bounty")
	FString BountyTitle;

	UPROPERTY(EditDefaultsOnly, Category = "Bounty")
	FString BountyDesc;
	// -----------------------------------------------
	
	bool Completed = false;
	
	// Spawned in Checkpoints for this Bounty
	TArray<ACheckpointClass*> BountyCheckpoints;

	// Spawns and stores this Bounties Checkpoints in order
	void SpawnCheckpoints();
	
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	
public:
	// Broadcast when first checkpoint from this bounty is completed
	FCompletedFirstCheckpoint CompletedFirstCheckpoint;

	// ------ Getters for Bounty and Checkpoint Properties ------
	bool IsCompleted()
	{
		return Completed;
	}
	
	FString GetBountyTitle()
	{
		return BountyTitle;
	}

	FString GetBountyDesc()
	{
		return BountyDesc;
	}

	int GetBountyrewardMoney()
	{
		return RewardMoney;
	}

	FVector GetCheckpointLocation()
	{
		if (BountyCheckpoints.IsEmpty() || BountyCheckpoints[0] == nullptr)
		{
			return FVector(0, 0, 0);
		}

		return BountyCheckpoints[0]->GetCheckpointSpawnTransform().GetLocation();
	}

	FVector GetActiveWaypointLocation()
	{
		if (BountyCheckpoints.IsEmpty() || BountyCheckpoints[0] == nullptr)
		{
			return FVector(0, 0, 0);
		}

		return BountyCheckpoints[0]->GetWaypointLoc();
	}

	UTexture2D* GetActiveWaypointIcon()
	{
		if (BountyCheckpoints.IsEmpty() || BountyCheckpoints[0] == nullptr)
		{
			return nullptr;
		}

		return BountyCheckpoints[0]->GetWaypointIcon();
	}
	
	// ----------------------------------------------------------

	// Sets default values for this actor's properties
	ABountyClass();

	// Called every frame
    virtual void Tick(float DeltaTime) override;
	
	// Called when a checkpoint is completed, handles moving onto next checkpoint and discarding the old one
	UFUNCTION()
	virtual void IncrementBountyCheckpoint();

	// Collect Money in C++, any other special reward will be implemented in BP if neccessary
	UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
	void CollectRewards();
};
