extends LimboState

@export var grap_hook_shooting_time: float

var _timer: float

func _setup() -> void:
	self.add_event_handler(&"completed_grap_hook",_handle_hook_input_completed)
	agent.spawn_hook_comp.hook_reach_finished.connect(_handle_hook_stretching_end)

func _enter() -> void:
	agent.locomotion_comp.suspend_movement()
	agent.locomotion_comp.stop_dash(true)
	
	var d = agent.get_move_input() as Vector2
	agent.spawn_hook_comp.spawn_grap_hook_inst(d)

func _update(delta: float) -> void:
	pass

func _exit() -> void:
	#agent.locomotion_comp.enable_movement()
	pass

func _handle_hook_input_completed() -> bool:
	agent.spawn_hook_comp.suspend_hook_stretching(false)
	
	##如果鈎爪沒有找到任何錨點，則直接退出
	if not agent.spawn_hook_comp.has_hook_anchor():
		_exit_with_no_anchor()
	return true

func _handle_hook_stretching_end(reach_end: bool,Anchor:Node2D) -> void:
	
	##如果鈎爪沒有找到任何錨點，則直接退出
	if not agent.spawn_hook_comp.has_hook_anchor():
		_exit_with_no_anchor()
	
	self.dispatch(self.EVENT_FINISHED)

func _exit_with_no_anchor() -> void:
	if agent.is_on_floor():
		self.dispatch(&"exit_on_ground")
	else:
		self.dispatch(&"exit_on_air")
	
	agent.spawn_hook_comp.release_cached_hook()
	return
