extends Node2D

@onready var grid: Grid = get_parent()

const UNIT_COLOR = Color.CHOCOLATE
const EMPTY_COLOR = Color(Color.CORNFLOWER_BLUE,1.0)

var units: Array[Vector2]
var empty: Array[Vector2]

func _ready() -> void:
	GameplaySignalBus.highlight_empty.connect(on_highlight_empty)
	GameplaySignalBus.highlight_units.connect(on_highlight_units)

func _draw() -> void:
	for unit in units:
		draw_rect(Rect2(unit,grid.cell_size),UNIT_COLOR)
	for empty_cell in empty:
		draw_rect(Rect2(empty_cell,grid.cell_size),EMPTY_COLOR)

func on_highlight_units(new_units: Array[Unit]):
	units.clear()
	units.assign(new_units.map(func(unit : Unit): return grid.snap_position(unit.global_position)))
	queue_redraw()

func on_highlight_empty(new_empty: Array[int]):
	empty.clear()
	empty.assign(new_empty.map(func(cell: int): return grid.from_index(cell)))
	queue_redraw()
