extends AnimatableBody2D

@onready var tip = $"Tip"
@onready var warnSign = $"../WarnSign"
@onready var warnBorder = $"../WarnBorder"

@export var once = false

var detecting = true

var velocity = Vector2.ZERO

@onready var startPos = global_position

func _on_anim_trigger_body_shape_entered(_body_rid, _body, _body_shape_index, _local_shape_index):
	if detecting:
		
		detecting = false
		var tween = get_tree().create_tween()
		var space_state = get_world_2d().direct_space_state
		
		var query = PhysicsRayQueryParameters2D.create(tip.global_position, tip.global_position + Vector2(0, 500).rotated(global_rotation))
		var result = space_state.intersect_ray(query)
		
		var finalPos = Vector2(0, 400).rotated(global_rotation)
		if result:
			finalPos = result.position - tip.global_position
		
		warnBorder.points[0] = Vector2(15.5, -0.25)
		warnBorder.points[1] = Vector2(-15.5, -0.25)
		warnBorder.points[3] = Vector2(15.5, (finalPos.length() / $"../".scale.y) + 4.5)
		warnBorder.points[2] = Vector2(-15.5, (finalPos.length() / $"../".scale.y) + 4.5)
		
		warnSign.global_rotation = 0
		warnSign.global_position = tip.global_position + (finalPos / 2) + Vector2(0, 4.5).rotated(global_rotation) 
		
		tween.set_trans(Tween.TRANS_CUBIC)
		tween.set_ease(Tween.EASE_IN)
		
		tween.set_parallel(true)
		tween.tween_property(self, "global_position", global_position + finalPos, 0.4).set_delay(0.05)
		tween.tween_property(self, "velocity", Vector2(0, 400).rotated(global_rotation), 0.4).set_ease(Tween.EASE_OUT)
		
		tween.tween_property(warnSign, "modulate", Color8(255, 255, 255, 255), 0.3).set_ease(Tween.EASE_OUT)
		tween.tween_property(warnBorder, "modulate", Color8(255, 0, 0, 255), 0.3).set_ease(Tween.EASE_OUT)
		tween.chain()
		tween.tween_property(warnSign, "modulate", Color8(255, 255, 255, 0), 0.5).set_ease(Tween.EASE_IN)
		tween.tween_property(warnBorder, "modulate", Color8(255, 0, 0, 0), 0.5).set_ease(Tween.EASE_IN)
		
		if not once:
			await get_tree().create_timer(0.5).timeout
		
			var launcherHitbox = $Tip/LauncherHitbox
			var hitTable = launcherHitbox.get_overlapping_bodies()
			for object in hitTable:
				if object is CharacterBody2D and "velocity" in object and object.has_method("forceLaunch"):
					object.forceLaunch(velocity.x * 3)
					#object.velocity.y = velocity.y * 1.5
					if velocity.x < 5:
						object.forceLowGrav = true
			
			tween = get_tree().create_tween()
			tween.set_parallel(true)
			tween.set_trans(Tween.TRANS_SINE)
			tween.set_ease(Tween.EASE_IN)
			
			tween.tween_property(warnSign, "modulate", Color8(255, 255, 255, 0), 0.6)
			
			tween.tween_property(self, "global_position", startPos, 1).set_delay(0.5)
			tween.tween_property(self, "velocity", Vector2.ZERO, 0.5).set_delay(0.25)
			
			#await get_tree().create_timer(0.25).timeout
			
			#constant_linear_velocity = Vector2.ZERO
		
			await tween.finished
			
			detecting = true

#func _on_launcher_hitbox_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
	#if "velocity" in body:
		#body.velocity /= 10
