extends CharacterBody2D
class_name Objeto

var bubbled = false
@onready var audio_player_create:AudioStreamPlayer2D = $AudioStreamPlayer2D
@onready var audio_player_pop:AudioStreamPlayer2D = $AudioStreamPlayer2D2

@onready var bubble_sprite:Sprite2D = $Bubble
@onready var collider:CollisionShape2D = $CollisionShape2D

@onready var item_bubble_anim:AnimationPlayer = $ItemBubble

@onready var shake_anim:AnimationPlayer = $ShakePlayer
var shaking:bool = false
@onready var shake_speed:float = 0
@onready var shake_accel:float = 0.5
@onready var shake_max:float = 2

@export var velocidade_inicial:float = 300.0
@export var desaceleracao:float = 200

@onready var timer:Timer = $Timer

var on_fish:bool = false
@onready var fish_area:CollisionShape2D = $Area2D/CollisionShape2D
var destino

@export var is_trigger:bool = false
@export var trigger_text:String = ''
@export var trigger_ref:int = 0
var triggered:bool = false

var cont_init:int = 0
@export var bubble_time:float = 3

func _ready() -> void:
	$Timer.wait_time = bubble_time
	
	
func _process(delta: float) -> void:

	if shaking:
		shake_speed += shake_accel*delta
		shake_anim.speed_scale = shake_speed
		if shake_speed >= shake_max: bubble_pop()


func _physics_process(delta: float) -> void:
	if bubbled: 
		item_bubble_anim.speed_scale = randf_range(0, 3)
		if destino:

			var direction = position.direction_to(destino).x
			velocity.x = 200 * direction
			move_and_slide()
			
			if position.distance_to(destino) < 0.1:
				position.x = destino.x
				destino = null
			
			return
			
		if velocity.y != 0: 
			velocity.y += desaceleracao * delta
			if velocity.y >= 0:
				velocity.y = 0
		
		move_and_slide()
		
		for i in get_slide_collision_count():
			var collision = get_slide_collision(i).get_collider()
			if collision is Player and on_fish: bubble_pop()
		if cont_init > 0: cont_init -= 1
	else:
		if not is_on_floor():
			velocity += get_gravity() * delta
		move_and_slide()



func bubble_up():
	bubbled = true
	velocity.y = -velocidade_inicial
	set_collision_layer_value(5, false)
	set_collision_layer_value(4, true)
	fish_area.call_deferred('set','disabled', false)
	bubble_sprite.visible = true
	cont_init = 4
	shake_anim.play('RESET')
	item_bubble_anim.play('on_buble')
	timer.start()
	audio_player_create.play()
	

func bubble_pop():
	bubbled = false
	set_collision_layer_value(5, true)
	set_collision_layer_value(4, false)
	fish_area.call_deferred('set', 'disabled', true)
	#bubble_sprite.visible = false
	cont_init = 0
	timer.stop()
	
	item_bubble_anim.play('RESET')
	
	shaking = false
	shake_anim.play('RESET')
	shake_anim.play('pop')
	shake_speed = 0
	audio_player_pop.play()


func _on_area_2d_2_body_exited(_body: Node2D) -> void:
	if not bubbled: return
	bubble_pop()


func _on_timer_timeout() -> void:
	if not bubbled: return
	shake_anim.play('shake')
	shake_anim.speed_scale = shake_speed
	shaking = true


func _on_area_2d_body_entered(body: Node2D) -> void:
	on_fish = true
	if cont_init > 0: bubble_pop()
	velocity.x = body.velocity.x


func _on_area_2d_body_exited(_body: Node2D) -> void:
	on_fish = false
	velocity.x = 0
