#pragma once

#include "CoreMinimal.h"
#include "SurvivorStatusInterface.h"
#include "CharacterDreamworldComponent.h"
#include "SurvivorTriggeredDreamTrapDelegate.h"
#include "Templates/SubclassOf.h"
#include "SurvivorSleepiness.h"
#include "TunableStat.h"
#include "GameplayTagContainer.h"
#include "FastTimer.h"
#include "TagStateBool.h"
#include "ECamperDamageState.h"
#include "SurvivorDreamworldComponent.generated.h"

class UStatusEffect;
class UTimerObject;
class AKiller;
class AActor;
class ADBDPlayer;

UCLASS(Blueprintable, meta=(BlueprintSpawnableComponent))
class USurvivorDreamworldComponent : public UCharacterDreamworldComponent, public ISurvivorStatusInterface
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, BlueprintAssignable)
	FSurvivorTriggeredDreamTrapDelegate OnSurvivorTriggeredDreamTrap;

private:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(AllowPrivateAccess=true))
	TSubclassOf<UStatusEffect> _survivorDreamWorldStatusEffect;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Transient, meta=(AllowPrivateAccess=true))
	FSurvivorSleepiness _survivorSleepiness;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Transient, Export, meta=(AllowPrivateAccess=true))
	UTimerObject* _sleepinessProtectionTimer;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(AllowPrivateAccess=true))
	FTunableStat _sleepGainPerSecondStat;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(AllowPrivateAccess=true))
	FTunableStat _clockSleepinessProtectionStat;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(AllowPrivateAccess=true))
	FTunableStat _sleepThresholdStat;

	UPROPERTY(EditAnywhere)
	FGameplayTagQuery _inHealActionInDreamworldQuery;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Transient, meta=(AllowPrivateAccess=true))
	bool _isImmuneFromSleepiness;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, ReplicatedUsing=OnRep_ShowKiller, Transient, meta=(AllowPrivateAccess=true))
	bool _showKiller;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	FFastTimer _killerGlimpseDurationTimer;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	FFastTimer _killerGlimpseCooldownTimer;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	float _glimpseMinDistance;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	float _glimpseMaxDistance;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	float _killerGlimpseCooldown;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	float _killerGlimpseDuration;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	int32 _wakeUpBySkillCheckCount;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	int32 _fellAsleepCount;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Transient, meta=(AllowPrivateAccess=true))
	FTagStateBool _forcedAsleep;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Transient, meta=(AllowPrivateAccess=true))
	bool _useScriptedKillerVisibility;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, meta=(AllowPrivateAccess=true))
	bool _showKillerInstantly;

public:
	UFUNCTION(BlueprintPure)
	bool ShouldLookSleepyToLocalPlayer() const;

	UFUNCTION(BlueprintCallable)
	void SetScriptedKillerVisibility(bool useScriptedKillerVisibility);

private:
	UFUNCTION(BlueprintCallable)
	void OnRep_ShowKiller();

public:
	UFUNCTION(BlueprintCallable)
	void Local_SetShowKiller(bool showKiller, bool instantly);

private:
	UFUNCTION(BlueprintCallable)
	void InitializeTunableValues(AKiller* killer);

protected:
	UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
	void HandleShowKillerChanged(bool showKiller, bool instantly);

public:
	UFUNCTION(BlueprintPure)
	bool CanWakeUpBySkillCheck() const;

	UFUNCTION(BlueprintPure)
	bool CanWakeUp() const;

	UFUNCTION(BlueprintCallable)
	void BroadcastSurvivorTriggeredDreamTrap(AActor* dreamObject);

	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly)
	void Authority_WakeUpByWakerObject();

	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly)
	void Authority_WakeUpByOther(ADBDPlayer* instigator);

	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly)
	void Authority_WakeUpByHookOrDying();

	UFUNCTION(BlueprintPure=false, BlueprintCallable, BlueprintAuthorityOnly)
	void Authority_SpawnDreamworldStatusEffect() const;

	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly)
	void Authority_PutAsleep();

private:
	UFUNCTION(BlueprintCallable)
	void Authority_OnDamageStateChanged(ECamperDamageState oldDamageState, ECamperDamageState newDamageState);

	UFUNCTION(BlueprintCallable)
	void Authority_OnAttacked();

public:
	UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly)
	void Authority_HandleSkillCheckFailure(bool wakeUpEveryoneInteracting, ADBDPlayer* instigator);

	UFUNCTION(BlueprintPure, BlueprintAuthorityOnly)
	int32 Authority_GetWakeUpBySkillCheckCount() const;

	UFUNCTION(BlueprintPure, BlueprintAuthorityOnly)
	int32 Authority_GetFellAsleepCount() const;

public:
	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;

public:
	USurvivorDreamworldComponent();
};

FORCEINLINE uint32 GetTypeHash(const USurvivorDreamworldComponent) { return 0; }
