// Project Lab - NHTV Igad

#pragma once

#include "GameFramework/Actor.h"
#include "Effect.generated.h"

UCLASS()
class UNREALPROJECT_API AEffect : public AActor
{
	GENERATED_BODY()

public:	
	AEffect();

	virtual void BeginPlay() override; 
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

	virtual void Tick( float DeltaSeconds ) override;

	UFUNCTION()
	void Init(float duration, AActor* followActor = nullptr, bool clientSideOnly = false);

	UFUNCTION(BlueprintNativeEvent)
	void BeginEffect();

	// Destroys this particle system + waits for deactivateParticlesBefore seconds after deactivating particle systems
	UFUNCTION(BlueprintCallable, NetMulticast, Reliable, Category="Effect")
	void End();

	// Event fired when end is called
	UFUNCTION(BlueprintImplementableEvent)
	void OnEnd();

	// Function used to update the position of this effect
	UFUNCTION(BlueprintNativeEvent)
	void CalculatePosition();

	UFUNCTION()
	void OnRootParticleSystemFinished(UParticleSystemComponent* sys);
	UFUNCTION()
	void OnRootAudioFinished();

	UFUNCTION(BlueprintCallable, Category="Effect")
	AActor* GetFollower() const;

	// Time before the end of the duration in which the particle system is deactivated
	UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category="Particles")
	float deactivateParticlesBefore;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Effect", meta = (ExposeOnSpawn))
	float m_lifeTime;
private:
	void DeactivateParticles();

	TArray<UActorComponent*> m_particleSystems;
	UParticleSystemComponent* m_rootParticleSystem;
	UAudioComponent* m_rootAudio;

	UPROPERTY(Replicated, ReplicatedUsing = CalculatePosition)
	AActor* m_followActor;
	UPROPERTY(Replicated)
	float m_duration;
	bool m_follow;
};

UCLASS()
class ATargetedEffect : public AEffect
{
	GENERATED_BODY()
public:
	UPROPERTY(Replicated, BlueprintReadOnly)
	FVector target;
};