extends CharacterBody2D
class_name Bubble


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

@onready var area_check:Area2D = $Area2D

@onready var squish_anim:AnimationPlayer = $SquishPlayer
@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
var has_player = false
var player

var direcao:int = 1
var on_fish:bool = false

var destino = null
var cont_init:int = 4
var pop_count = -1
var life_time:float = 3

func _ready() -> void:
	player = get_tree().get_first_node_in_group("player")
	velocity = Vector2(velocidade_inicial*direcao, velocity.y)
	squish_anim.play('create')
	$Timer.wait_time = life_time
	$Timer.start()

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()
	
	if pop_count > 0:
		pop_count -= delta
		if pop_count <= 0: bubble_pop()


func _physics_process(delta: float) -> void:
	
	if velocity.y > 0:
		if not destino: velocity.y =0
		
	if velocity.y < 0 and player:
		var old_vel = player.velocity
		player.velocity = Vector2(0, velocity.y)
		player.move_and_slide()
		player.velocity = old_vel
		
	if on_fish: 
		move_and_slide()
		for i in get_slide_collision_count():
			var collision = get_slide_collision(i).get_collider()
			if collision is Player:
				bubble_pop()

	elif 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

	elif velocity.x != 0:
		velocity.x -= desaceleracao * direcao * delta

		if direcao == 1:
			if velocity.x <= 0: velocity.x = 0
		else:
			if velocity.x >= 0: velocity.x = 0
		
		move_and_slide()
		
	if cont_init > 0: cont_init -= 1
	
	#for get_slide_collision()

func bubble_pop():
	velocity.x = 0
	get_node("CollisionShape2D").set_deferred('disabled', true)
	area_check.get_node("CollisionShape2D").set_deferred('disabled', true)
	audio_player_pop.play()
	squish_anim.play('pop')
	await get_tree().create_timer(0.5).timeout
	
	queue_free()


func _on_area_2d_body_entered(body: Node2D) -> void:
	if body == self: return
	if body is Bubble:
		body.bubble_pop()
	
	if body is Objeto and not body.bubbled:
		body.bubble_up()
		queue_free()
		
	elif body is Fish:
		on_fish = true
		if cont_init > 0: bubble_pop()
		velocity.x = body.velocity.x
	
	elif body is Player:
		if on_fish: bubble_pop()
	else: bubble_pop()





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


func _on_area_2d_body_exited(body: Node2D) -> void:
	if body is Fish:
		on_fish = false
		velocity.x = 0


func _on_area_2d_2_body_entered(body: Player) -> void:
	if pop_count > 0:
		pop_count = -1
	else:
		squish_anim.play('Squish')
	player = body
	
	
func _on_area_2d_2_body_exited(_body: Node2D) -> void:
	pop_count = 0.5
	#bubble_pop()


func _on_area_2d_3_body_entered(_body: Node2D) -> void:
	bubble_pop()
