// Multiplayer By Caleb

#pragma once

#include "CoreMinimal.h"
#include "AbilitySystemInterface.h"
#include "GameplayTagContainer.h"
#include "GameFramework/Character.h"
#include "CCharacter.generated.h"

class UWidgetComponent;
class UCAbilitySystemComponent;
class UCAttributeSet;

UCLASS()
class CRUNCH_API ACCharacter : public ACharacter, public IAbilitySystemInterface
{
	GENERATED_BODY()

protected:
	virtual void BeginPlay() override;

public:
	ACCharacter();
	void ServerSideInit();
	void ClientSideInit();
	bool IsLocallyControlledByPlayer() const;
	// Only called on the server
	virtual void PossessedBy(AController* NewController) override;

	/********************************************************************************************/
	/*							Gameplay Ability												*/
	/********************************************************************************************/
public:
	virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override;

private:
	void DeadTagUpdated(FGameplayTag GameplayTag, int32 NewCount);
	void BindGASChangeDelegate();
	UPROPERTY(EditDefaultsOnly, Category="Gameplay Ability")
	TObjectPtr<UCAbilitySystemComponent> CAbilitySystemComponent;
	UPROPERTY()
	TObjectPtr<UCAttributeSet> CAttributeSet;
	/********************************************************************************************/
	/*									 UI														*/
	/********************************************************************************************/
private:
	UPROPERTY(VisibleDefaultsOnly, Category="UI")
	TObjectPtr<UWidgetComponent> OverHeadWidgetComponent;
	void ConfigureOverHeadStatusWidget();

	UPROPERTY(EditDefaultsOnly, Category="UI")
	float HeadStatGaugeVisibilityCheckUpdateGap = 1.f;

	UPROPERTY(EditDefaultsOnly, Category="UI")
	float HeadStatGaugeVisibilityRangeSquared = 10000000.f;

	FTimerHandle HeadStatGaugeVisibilityUpdateTimerHandle;
	void UpdateHeadStatGaugeVisibility();
	void SetStatusGaugeEnabled(bool bIsEnabled);
	
	/********************************************************************************************/
	/*									 Death & Respawning										*/
	/********************************************************************************************/
	FTransform MeshRelativeTransform;
	UPROPERTY(EditDefaultsOnly, Category="Death")
	float DeathMontageFinishTimeShift { -0.8f };
	UPROPERTY(EditDefaultsOnly, Category="Death")
	TObjectPtr<UAnimMontage> DeathMontage;

	FTimerHandle DeathMontageTimerHandle;
	void DeathMontageFinished();
	void SetRagdollEnabled(bool bIsEnabled);

	void PlayDeathAnimation();
	
	void StartDeathSequence();
	void ReSpawn();
	virtual void OnDead();
	virtual void OnRespawn();
};
