## Ambulance — drives in from off-screen right when Emergency Room is entered; tap to toggle.
class_name Ambulance extends Node2D

const OFFSCREEN_OFFSET: float = 1200.0
const BUTTON_HALF_WIDTH: float = 90.0
const BUTTON_HALF_HEIGHT: float = 50.0
const DRIVE_DURATION: float = 1.2

@export var trigger_floor: int = 0
@export var trigger_room: int = 3

var _parked_x: float = 0.0
var _is_parked: bool = false
var _is_animating: bool = false


func _ready() -> void:
	_parked_x = position.x
	position.x = _parked_x + OFFSCREEN_OFFSET
	RoomNavigator.room_changed.connect(_on_room_changed)


func _exit_tree() -> void:
	if RoomNavigator.room_changed.is_connected(_on_room_changed):
		RoomNavigator.room_changed.disconnect(_on_room_changed)


func _on_room_changed(floor_index: int, room_index: int) -> void:
	if floor_index == trigger_floor and room_index == trigger_room:
		if not _is_parked and not _is_animating:
			_drive_in()


func _input(event: InputEvent) -> void:
	if _is_animating:
		return
	var screen_pos: Vector2
	if event is InputEventScreenTouch and event.pressed:
		screen_pos = event.position
	elif event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
		screen_pos = event.position
	else:
		return
	var canvas_transform: Transform2D = get_viewport().get_canvas_transform()
	var world_pos: Vector2 = canvas_transform.affine_inverse() * screen_pos
	var local_pos: Vector2 = to_local(world_pos)
	if abs(local_pos.x) <= BUTTON_HALF_WIDTH and abs(local_pos.y) <= BUTTON_HALF_HEIGHT:
		if _is_parked:
			_drive_out()
		else:
			_drive_in()


func _drive_in() -> void:
	_is_animating = true
	_is_parked = false
	var tween: Tween = create_tween()
	tween.set_ease(Tween.EASE_OUT)
	tween.set_trans(Tween.TRANS_QUAD)
	tween.tween_property(self, "position:x", _parked_x, DRIVE_DURATION)
	tween.finished.connect(func() -> void:
		_is_parked = true
		_is_animating = false
		_play_stop_bounce()
	)


func _drive_out() -> void:
	_is_animating = true
	_is_parked = false
	var tween: Tween = create_tween()
	tween.set_ease(Tween.EASE_IN)
	tween.set_trans(Tween.TRANS_QUAD)
	tween.tween_property(self, "position:x", _parked_x + OFFSCREEN_OFFSET, DRIVE_DURATION)
	tween.finished.connect(func() -> void:
		_is_animating = false
	)


func _play_stop_bounce() -> void:
	var tween: Tween = create_tween()
	tween.tween_property(self, "scale", Vector2(1.08, 0.94), 0.08)
	tween.tween_property(self, "scale", Vector2(0.96, 1.06), 0.08)
	tween.tween_property(self, "scale", Vector2(1.0, 1.0), 0.08)
