extends CharacterBody3D

const SPEED = 2.5
const JUMP_VELOCITY = 4.5

var walk_animation_time = 0
var face_rotation = null
var face_left = false

var combatable = false
var health = 100

var iframes = 0

@onready var game = get_parent()
@onready var camera = game.get_node("PlayerCamera")

func hit_me():
	iframes = 0.5
	health -= 10

func _ready() -> void:
	$DreamerBody.look_at(game.get_node("PlayerCamera").global_position)
	$DreamerBody.rotation.x = 0
	face_rotation = $DreamerBody.rotation.y

func _physics_process(delta: float) -> void:
	if iframes > 0:
		iframes -= delta
		$DreamerBody/Body.modulate.a = 0.5
	else:
		$DreamerBody/Body.modulate.a = 1
	
	$HealthLabel.text = str(health)
	
	if not is_on_floor():
		velocity += get_gravity() * delta
		
	var viewport_half = (get_viewport().get_visible_rect().size / 2.0)
	var player_position = camera.unproject_position(global_transform.origin)
	
	print(viewport_half)
	print(player_position)
	
	if face_left:
		$DreamerBody.rotation.y = face_rotation + deg_to_rad(180)
	else:
		$DreamerBody.rotation.y = face_rotation
	
	if combatable:
		$DreamerBody/Body/ArmPivot.rotation.z = player_position.angle_to_point(get_viewport().get_mouse_position())
		$HitCollision.rotation.y = -$DreamerBody/Body/ArmPivot.rotation.z + face_rotation
		
		if ($DreamerBody/Body/ArmPivot.rotation_degrees.z < -90) or ($DreamerBody/Body/ArmPivot.rotation_degrees.z > 90):
			face_left = true
			$DreamerBody/Body/ArmPivot.scale.y = -1
			$DreamerBody/Body/ArmPivot.scale.x = -1
			var mouse_pos = get_viewport().get_mouse_position()
			mouse_pos.y = (viewport_half.y) + ((viewport_half.y) - mouse_pos.y)
			
			$DreamerBody/Body/ArmPivot.rotation.z = viewport_half.angle_to_point(mouse_pos)
		else:
			face_left = false
			$DreamerBody/Body/ArmPivot.scale.y = 1
			$DreamerBody/Body/ArmPivot.scale.x = 1
	else:
		$DreamerBody/Body/ArmPivot.global_rotation.y = face_rotation
		$DreamerBody/Body/ArmPivot.scale.y = 1
		$DreamerBody/Body/ArmPivot.scale.x = 1
		$DreamerBody/Body/ArmPivot.rotation.z = 0
	
	if Input.is_action_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		$DreamerBody/Animator.set("parameters/jump/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)

	var input_dir := Vector2.ZERO
	if not combatable:
		if input_dir.x > 0:
			face_left = false
		if input_dir.x < 0:
			face_left = true
	
	var direction = (camera.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
		
		$DreamerBody/Animator.set("parameters/walk/blend_amount", abs(direction.length()))
	else:
		
		if is_on_floor():
			velocity.x = move_toward(velocity.x, 0, SPEED)
			velocity.z = move_toward(velocity.z, 0, SPEED)
		
		$DreamerBody/Animator.set("parameters/walk/blend_amount", 0)
		
	if direction and is_on_floor():
		walk_animation_time += delta * 25
		
		$DreamerBody.position.y = sin(walk_animation_time + 1) / 16
		$DreamerBody.rotation.z = sin(walk_animation_time / 2) / 16
	else:
		walk_animation_time = 0
		
		$DreamerBody.rotation.z /= 1 + (delta * 2.5)
		
		$DreamerBody.position.y /= 1 + (delta * 12)
		
	if is_on_floor():
		$DreamerBody/Animator.set("parameters/fall/blend_amount", 0)
		$DreamerBody/Animator.set("parameters/leap/blend_amount", 0)
	else:
		if velocity.y < 0:
			$DreamerBody/Animator.set("parameters/fall/blend_amount", 1)
			$DreamerBody/Animator.set("parameters/leap/blend_amount", 0)
		else:
			$DreamerBody/Animator.set("parameters/fall/blend_amount", 0)
			$DreamerBody/Animator.set("parameters/leap/blend_amount", 1)

	move_and_slide() 
	
		
	$Shadow.global_position.y = $Floorcast.get_collision_point().y + 0.01
	
