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

#pragma once

#include "CoreMinimal.h"
#include "WaypointActor.h"
#include "EndlessVendetta/EndlessVendettaCharacter.h"
#include "GameFramework/Actor.h"
#include <EndlessVendetta/EndlessVendettaGameMode.h>
#include "CheckpointClass.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FCompletedCheckpoint);

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

// ------------------- ATTRIBUTES ------------------------------	
	// ------ Properties set from Editor ------
	UPROPERTY(EditDefaultsOnly, Category = "Checkpoint")
	FString CheckpointTip;
	UPROPERTY(EditDefaultsOnly, Category = "Checkpoint")
	FVector WaypointLoc;
	UPROPERTY(EditDefaultsOnly, Category = "Checkpoint")
	UTexture2D* WaypointIcon;
	UPROPERTY(EditDefaultsOnly, Category = "Checkpoint")
	FTransform CheckpointSpawnTransform;
	UPROPERTY(EditDefaultsOnly, Category = "Checkpoint")
	TSubclassOf<AWaypointActor> WaypointActorClass;
	UPROPERTY(EditDefaultsOnly, Category = "Checkpoint")
	bool HideWaypoint = false;

	// Stored Title of Bounty, used by Update Checkpoint Waypoint Func
	FString BountyTitle;

	// Ref to Waypoint Actor
	AWaypointActor* WaypointActor;
protected:

public:
	// Delegate called when Checkpoint is Completed
	FCompletedCheckpoint CompletedCheckpoint;

	// Used by Checkpoint BPs to Determine weather to Run Checkpoint Logic
	UPROPERTY(BlueprintReadOnly, Category = "Checkpoint")
	bool Active = false;
// ------------------- METHODS ---------------------------------
private:

protected:
	// Used by Checkpoints BPs to broadcast that they're Completed
	UFUNCTION(BlueprintCallable, Category = "Checkpoint")
	void BroadcastCompletion()
	{
		CompletedCheckpoint.Broadcast();
		Cast<AEndlessVendettaGameMode>(GetWorld()->GetAuthGameMode())->SendEvent("Check", CheckpointTip);
	}

	// Used by Checkpoint BPs to update Checkpoint Loc without Finishing Checkpoint itself
	UFUNCTION(BlueprintCallable)
    void UpdateCheckpointWaypoint(FVector WaypointNewLoc);

	// Used to Destroy any Waypoint Actor Associated with this Checkpoint when it's Destroyed
	virtual void Destroyed() override;
public:
	// Sets default values for this actor's properties
    ACheckpointClass();

	// Used by Bounties to Spawn Waypoint for this Checkpoint when it's Activated
	void SpawnWaypoint(const FString& BountyTitle);

	// Destroys Waypoint Associated with this Checkpoint if one Exists
	void DestroyWaypoint()
	{
		if (WaypointActor) WaypointActor->Destroy();
	}

	// Used by Bounty Class to broadcast checkpoint tip for current checkpoint
	FString GetCheckpointTip()
	{
		return CheckpointTip;
	}




	
// ------------------- LEGACY ---------------------------------		

public:
	UFUNCTION(BlueprintImplementableEvent)
	void CheckpointActivated();

	// ------ Getters for CP Properties ------

	

	FVector GetWaypointLoc()
	{
		return WaypointLoc;
	}

	UTexture2D* GetWaypointIcon()
	{
		return WaypointIcon;
	}

	FTransform GetCheckpointSpawnTransform()
	{
		return CheckpointSpawnTransform;
	}
	
};
