extends Node3D
class_name WholePlayer

var INITIAL_HEALTH = 100
var REGENERATION_RATE = 10
var REGENERATION_DELAY = 2

var max_health = INITIAL_HEALTH
var current_health = INITIAL_HEALTH
var startHealthRegen = false

@export var prevStage:String
@export var thisStage:String
@export var nextStage:String
@onready var deathSFX = $DeathSFX
static var getPrevStage
static var getThisStage
static var getNextStage

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	$"../Hud/HealthBar".max_value = max_health
	getPrevStage = prevStage
	getThisStage = thisStage
	getNextStage = nextStage

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if Input.is_action_just_pressed("Swap"):
		var temp_pos = $Player.global_position
		$Player.global_position = $OtherBody.global_position
		$OtherBody.global_position = temp_pos
		$Player.swap_inventory()

	#if Input.is_action_just_pressed("Take Damage"):
		#_takeDamage(20)
	if startHealthRegen:
		current_health = min(INITIAL_HEALTH, current_health + REGENERATION_RATE * delta)
		$"../Hud/HealthBar".value = current_health

func _takeDamage(amount: float) -> void:
	startHealthRegen = false
	current_health -= amount
	$"../Hud/HealthBar".value = current_health
	$Player/Head/Camera3D/AnimationPlayer.play("take_damage")
	if current_health < 0:
		die()
	$Timer.start(REGENERATION_DELAY)

func die() -> void:
	deathSFX.play()
	get_tree().reload_current_scene()

func _on_timer_timeout() -> void:
	startHealthRegen = true
