extends Node
class_name Selection
## Pawn selection + click-to-move input handler.
##
## A click on a pawn selects it; a click on a walkable tile while a pawn is
## selected pathfinds + commands the walk. Drags belong to the camera (pan)
## — we discriminate clicks from drags by motion + duration thresholds.
##
## Lives as a child of World; `_unhandled_input` runs after the camera rig
## and after any CanvasLayer UI swallows its own clicks.

const CLICK_MAX_DRIFT_PX: float = 8.0
const CLICK_MAX_DURATION_MS: int = 300

var _pathfinder: Pathfinder = null
var _selected_pawn: Pawn = null
## Currently selected workbench, or null.  Mutually exclusive with _selected_pawn —
## selecting one clears the other (see _select / _select_workbench).
var _selected_workbench: Workbench = null
var _camera = null  # Camera2D (CameraRig) — set via bind_camera(); duck-typed to avoid circular preload

# When Designation paint mode is active this flag is raised by Designation so
# Selection does not also try to select/move on the same click.
var designation_active: bool = false

var _press_screen_pos: Vector2 = Vector2.ZERO
var _press_time_ms: int = 0
var _pressing: bool = false


func bind(pathfinder: Pathfinder) -> void:
	assert(pathfinder != null, "Selection.bind: pathfinder is null")
	_pathfinder = pathfinder


## Inject the CameraRig so Tab-cycle and center-on-selection can pan to the
## selected pawn's tile.  Call from world.gd after selection.bind().
func bind_camera(rig) -> void:
	_camera = rig


func selected() -> Pawn:
	return _selected_pawn


func _unhandled_input(event: InputEvent) -> void:
	# ── Keyboard: Tab → cycle pawns (Shift+Tab reverses) ───────────────────────
	if event.is_action_pressed("pawn_next"):
		# Read shift from the event itself (set by the OS on the key event); more
		# reliable than Input.is_key_pressed(KEY_SHIFT) which doesn't reflect modifier
		# state from synthetic input events (MCP test injections).
		var reverse: bool = event is InputEventKey and event.shift_pressed
		_cycle_pawn(-1 if reverse else 1)
		get_viewport().set_input_as_handled()
		return

	# ── Keyboard: C / Home → center camera on selected pawn ─────────────────────
	if event.is_action_pressed("center_on_selection"):
		if _selected_pawn != null and _camera != null:
			_camera.pan_to_tile(_selected_pawn.tile)
			Audit.log("selection", "center_on_selection → %s" % _selected_pawn.tile)
		get_viewport().set_input_as_handled()
		return

	# ── Keyboard: Escape → deselect (lowest-priority; consumed last) ─────────────
	# Designation._input handles Escape first; panels handle it in _unhandled_input
	# before reaching here.  If we still see it and have a selection, consume it.
	if event.is_action_pressed("cancel"):
		if _selected_pawn != null:
			_deselect()
			get_viewport().set_input_as_handled()
			Audit.log("selection", "escape: deselected pawn")
			return
		if _selected_workbench != null:
			_deselect_workbench()
			get_viewport().set_input_as_handled()
			Audit.log("selection", "escape: deselected workbench")
			return

	# ── Mouse: only handle button events below ───────────────────────────────────
	if not (event is InputEventMouseButton):
		return

	# ── Right-click: cancel designation (if active) or deselect pawn / workbench ──
	if event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
		# Designation cancellation is handled by Designation._input; if we see
		# this right-click, no designation was active.  Deselect whatever is selected.
		if _selected_pawn != null:
			_deselect()
			get_viewport().set_input_as_handled()
		elif _selected_workbench != null:
			_deselect_workbench()
			get_viewport().set_input_as_handled()
		return

	if event.button_index != MOUSE_BUTTON_LEFT:
		return
	# Designation paint mode owns input while active; Selection steps aside.
	if designation_active:
		return
	if event.pressed:
		_press_screen_pos = event.position
		_press_time_ms = Time.get_ticks_msec()
		_pressing = true
		return
	if not _pressing:
		return
	_pressing = false
	var drift: float = event.position.distance_to(_press_screen_pos)
	var dt_ms: int = Time.get_ticks_msec() - _press_time_ms
	# Anything that drifted more than a few pixels or sat for more than 300 ms
	# is the camera's drag-pan; ignore it as a select/move action.
	if drift > CLICK_MAX_DRIFT_PX or dt_ms > CLICK_MAX_DURATION_MS:
		return
	_handle_click(event.position)


func _handle_click(screen_pos: Vector2) -> void:
	if _pathfinder == null:
		Audit.log("selection", "click before bind() — ignored")
		return

	var world_pos: Vector2 = get_viewport().get_canvas_transform().affine_inverse() * screen_pos
	var tile: Vector2i = Vector2i(
		floori(world_pos.x / float(Pawn.TILE_SIZE_PX)),
		floori(world_pos.y / float(Pawn.TILE_SIZE_PX)),
	)

	# Click on a pawn → select.  Pawn wins over workbench when they share a tile
	# (a pawn working at a bench is selectable; tap empty bench tile to inspect bills).
	var hit_pawn: Pawn = World.pawn_at_tile(tile)
	if hit_pawn != null:
		_select(hit_pawn)
		return

	# Click on a workbench → open the bill-editor panel.
	var hit_workbench = World.workbench_at_tile(tile)
	if hit_workbench != null:
		_select_workbench(hit_workbench)
		return

	# Empty tile with no current pawn selection → also clear any workbench selection.
	if _selected_pawn == null:
		if _selected_workbench != null:
			_deselect_workbench()
		return

	# Empty walkable tile with a selection → queue a forced job. Decision picks
	# it up on the next sim tick (preempts whatever RestProvider had assigned).
	if not _pathfinder.is_walkable(tile):
		Audit.log("selection", "destination %s not walkable" % tile)
		return
	var go_job := Job.new()
	go_job.label = "Go to %s" % tile
	go_job.toils.append(Toil.walk_to(tile))
	go_job.toils.append(Toil.idle())
	_selected_pawn.forced_job = go_job
	Audit.log("selection", "forced %s → %s" % [_selected_pawn.pawn_name, tile])


func _select(pawn: Pawn) -> void:
	if _selected_pawn == pawn:
		return
	# Mutual exclusion with workbench selection: clear it before promoting pawn.
	if _selected_workbench != null:
		_deselect_workbench()
	if _selected_pawn != null:
		_selected_pawn.set_selected(false)
		EventBus.pawn_deselected.emit()
	_selected_pawn = pawn
	pawn.set_selected(true)
	EventBus.pawn_selected.emit(pawn)
	Audit.log("selection", "selected %s at %s" % [pawn.pawn_name, pawn.tile])


## Clear the current selection without selecting another pawn.
func _deselect() -> void:
	if _selected_pawn == null:
		return
	_selected_pawn.set_selected(false)
	EventBus.pawn_deselected.emit()
	Audit.log("selection", "deselected %s" % _selected_pawn.pawn_name)
	_selected_pawn = null


## Select a workbench → opens the bill-editor panel via EventBus.
## Mutually exclusive with pawn selection: clears _selected_pawn first.
func _select_workbench(wb) -> void:
	if _selected_workbench == wb:
		return
	if _selected_pawn != null:
		_deselect()
	if _selected_workbench != null:
		EventBus.workbench_deselected.emit()
	_selected_workbench = wb
	EventBus.workbench_selected.emit(wb)
	Audit.log("selection", "selected workbench %s at %s" % [wb.label_text, wb.tile])


func _deselect_workbench() -> void:
	if _selected_workbench == null:
		return
	Audit.log("selection", "deselected workbench %s" % _selected_workbench.label_text)
	_selected_workbench = null
	EventBus.workbench_deselected.emit()


## Cycle the selection forward (dir=1) or backward (dir=-1) through World.pawns.
## Wraps around.  If no pawn currently selected, picks World.pawns[0].
## Pans the camera to the newly selected pawn's tile.
func _cycle_pawn(dir: int) -> void:
	var pawns: Array = World.pawns
	if pawns.is_empty():
		return
	var next_pawn: Pawn
	if _selected_pawn == null:
		next_pawn = pawns[0]
	else:
		var idx: int = pawns.find(_selected_pawn)
		if idx == -1:
			next_pawn = pawns[0]
		else:
			idx = posmod(idx + dir, pawns.size())
			next_pawn = pawns[idx]
	_select(next_pawn)
	if _camera != null:
		_camera.pan_to_tile(next_pawn.tile)
	Audit.log("selection", "pawn_cycle dir=%d → %s" % [dir, next_pawn.pawn_name])
