extends CharacterBody2D
class_name Player


@onready var anim:AnimationPlayer = $AnimationPlayer
@onready var sprite:Sprite2D = $Sprite2D

@onready var bubble = preload("res://scenes/bolha.tscn")
@onready var bubble_timer:Timer = $BubbleTimer
var can_shoot:bool = true

@export var move_speed: float = 50.0
@export var jump_velocity: float = -800.0

var dist_bubble_spawn: float

enum sentido {ESQUERDA, DIREITA}
var look_dir:sentido = sentido.DIREITA

var picked_object:Objeto
var obj_on_range:Objeto
var trigger_on_range:Objeto
var shoting = false

var shoot_high = true
signal trigger_object

var cutscene = false

var range_cutscene = false
signal iteract_cutscene

func _ready() -> void:
	dist_bubble_spawn = 18
	


func _process(_delta: float) -> void:
	if cutscene: return
	sprite.flip_h = look_dir == sentido.DIREITA
	if Input.is_action_just_pressed("bubble") and can_shoot and (is_on_floor() or shoot_high):
		if not is_on_floor(): shoot_high = false
		bubble_timer.start()
		can_shoot = false
		_create_bubble()
	
	if is_on_floor(): shoot_high = true
	
	if Input.is_action_just_pressed("item"):
		if range_cutscene:
			anim.play("idle")
			sprite.flip_h = false
			iteract_cutscene.emit()
			find_child('Camera2D').make_current()
			cutscene = true
			return
			
		elif trigger_on_range:
			trigger_object.emit(trigger_on_range.trigger_text, trigger_on_range.trigger_ref)
			trigger_on_range.queue_free()
			
		elif picked_object:
			picked_object.position = position
			get_parent().add_child(picked_object)
			picked_object = null
			
		#elif obj_on_range:
			#
			#picked_object = obj_on_range.duplicate()
			#obj_on_range.queue_free()
			#obj_on_range = null
	
	
	if shoting: return
	if is_on_floor():
		if velocity.x == 0: 
			anim.speed_scale = 0.5
			anim.play("idle")
		else: 
			anim.speed_scale = 1
			anim.play("sprint")
	else:
		if velocity.y < 0: anim.play("jump")
		else: anim.play("fall")
			
			
func _physics_process(delta: float) -> void:
	if cutscene: return
	if not is_on_floor():
		velocity += get_gravity() * delta

	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity

	var direction := Input.get_axis("left", "right")
	if direction:
		velocity.x = direction * move_speed
		if direction > 0: look_dir = sentido.DIREITA
		else: look_dir = sentido.ESQUERDA
			
	else:
		velocity.x = move_toward(velocity.x, 0, move_speed)

	move_and_slide()


func _create_bubble() -> void:
	var bubble_instance:Bubble = bubble.instantiate()
	var poss_diff:Vector2
	
	if look_dir == sentido.DIREITA: 
		poss_diff = Vector2(dist_bubble_spawn, 0)
		bubble_instance.direcao = 1
	else: 
		poss_diff = Vector2(-dist_bubble_spawn, 0)
		bubble_instance.direcao = -1
		
	bubble_instance.position = position + poss_diff
	get_parent().add_child(bubble_instance)
	
	
func _on_bubble_timer_timeout() -> void:
	can_shoot = true


func _on_area_2d_body_entered(body: Objeto) -> void:
	if body.is_trigger: trigger_on_range = body
	else: obj_on_range = body


func _on_area_2d_body_exited(body: Objeto) -> void:
	if body.is_trigger: trigger_on_range = null
	elif body == obj_on_range: obj_on_range = null


func _on_animation_player_animation_finished(anim_name: StringName) -> void:
	if anim_name == 'shoot': shoting = false
