extends CharacterBody2D

const GAME_OVER_TIMER = 2
const MAX_HEALTH = 100
const STEP_HEALTH = 20
const JUMP_VELOCITY = -450.0
var current_health = MAX_HEALTH

var SPEED = 250.0
@export var RUN_SPEED: float = 250.0
@export var AIM_SPEED: float = 50.0
@onready var label: Label = $"../Label"

@onready var head_sprite: Sprite2D = $Head_Sprite
@onready var head_collision: Area2D = $Head_Collision
@onready var slinger: Node2D = $Slinger

var INIT_HEAD_SCALE
var INIT_HEAD_HITBOX_SCALE

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

@onready var animated_sprite = $AnimatedSprite2D
func _ready() -> void:
	INIT_HEAD_SCALE = head_sprite.scale
	INIT_HEAD_HITBOX_SCALE = head_collision.scale
	
func take_damage():
	# Take Damage logic
	
	if current_health <= MAX_HEALTH and current_health >= STEP_HEALTH:
		current_health = current_health - STEP_HEALTH
	if current_health == 0:
		end_game()
	
	# Head scale
	var head_scale = head_sprite.scale
	var collision_scale = head_collision.scale
	if current_health >= 20:
		var head_factor = (100 - current_health)/10.0
		head_sprite.scale = Vector2(
			INIT_HEAD_SCALE.x + (INIT_HEAD_SCALE.x * 0.1 * head_factor),
			INIT_HEAD_SCALE.y + (INIT_HEAD_SCALE.y * 0.1 * head_factor)
			)
		head_collision.scale = Vector2(
			INIT_HEAD_HITBOX_SCALE.x + (INIT_HEAD_HITBOX_SCALE.x * 0.1 * head_factor),
			INIT_HEAD_HITBOX_SCALE.y + (INIT_HEAD_HITBOX_SCALE.y * 0.1 * head_factor)
			)

		
func deal_damage():
	if current_health <= MAX_HEALTH-STEP_HEALTH and current_health >= STEP_HEALTH:
		current_health = current_health + STEP_HEALTH
	var head_scale = head_sprite.scale
	var collision_scale = head_collision.scale
	if current_health < 100:
		var head_factor = (100 - current_health)/10.0
		head_sprite.scale = Vector2(
			INIT_HEAD_SCALE.x + (INIT_HEAD_SCALE.x * 0.1 * head_factor),
			INIT_HEAD_SCALE.y + (INIT_HEAD_SCALE.y * 0.1 * head_factor)
			)
		head_collision.scale = Vector2(
			INIT_HEAD_HITBOX_SCALE.x + (INIT_HEAD_HITBOX_SCALE.x * 0.1 * head_factor),
			INIT_HEAD_HITBOX_SCALE.y + (INIT_HEAD_HITBOX_SCALE.y * 0.1 * head_factor)
			)
func end_game():
		label.text =  name.to_upper() + " LOST!!"
		head_sprite.visible = false
		await get_tree().create_timer(GAME_OVER_TIMER).timeout
		get_tree().reload_current_scene()
		
func _physics_process(delta):
	if slinger.is_aiming and is_on_floor():
		SPEED = AIM_SPEED
	elif !slinger.is_aiming and is_on_floor():
		SPEED = RUN_SPEED
		
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump_"+name) and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle -1/0/1:
	var direction = Input.get_axis("move_left_" + name, "move_right_" + name)
	
	#Flip the Sprite:
	if direction > 0:
		animated_sprite.flip_h = true
	elif direction < 0:
		animated_sprite.flip_h = false
	
	#Play animations
	if is_on_floor():
		if direction == 0:
			animated_sprite.play("idle_"+name)
		else:
			animated_sprite.play("run_"+name)
	else:
		animated_sprite.play("jump_"+name)
	
	#Apply movement
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()


@export var air_dash_speed: float = 500  # Speed of the air dash
@export var air_dash_duration: float = 0.2  # Duration of the air dash
@export var air_dash_cooldown: float = 1.0  # Time before another air dash is allowed
@export var air_dash_directional: bool = true  # Allow dashing in any direction

var is_dashing: bool = false
var dash_timer: float = 0.0
var can_air_dash: bool = true
var dash_velocity: Vector2 = Vector2.ZERO
