extends Node

func _ready() -> void:
	Log.pr("CoordUtility ready")


func all_cells(start: Vector2i, end: Vector2i) -> Array:
	# Returns all cells between start and end
	var cells = []
	
	for y in range(start.y, end.y + 1):
		for x in range(start.x, end.x + 1):
			cells.append(Vector2i(x, y))

	cells.sort()
	
	return cells

## Return all the perimeter cells between vector2i points 
func perimeter_cells(start: Vector2i, end: Vector2i, inclusive: bool = true, width: int = 1) -> Array[Vector2i]:
	# Returns cells on the perimeter between start and end
	# inclusive: if true, includes the area cells, if false, excludes them
	# width: the thickness of the perimeter (default: 1)
	var cells: Array[Vector2i] = []
	var min_x: int = min(start.x, end.x)
	var max_x: int = max(start.x, end.x)
	var min_y: int = min(start.y, end.y)
	var max_y: int = max(start.y, end.y)
	
	# Calculate the inner and outer bounds based on inclusive flag and width
	var inner_min_x: int
	var inner_min_y: int
	var inner_max_x: int
	var inner_max_y: int
	var outer_min_x: int
	var outer_min_y: int
	var outer_max_x: int
	var outer_max_y: int
	
	if inclusive:
		Log.pr("Drawing within bounds of the original rectangle")
		# For inclusive, inner bounds are the original rectangle
		inner_min_x = min_x
		inner_min_y = min_y
		inner_max_x = max_x
		inner_max_y = max_y
		
		# Outer bounds expand by width-1 in each direction
		outer_min_x = min_x - (width - 1)
		outer_min_y = min_y - (width - 1)
		outer_max_x = max_x + (width - 1)
		outer_max_y = max_y + (width - 1)
	else:
		Log.pr("Drawing outside bounds of the original rectangle")
		# For exclusive, inner bounds are the original rectangle
		inner_min_x = min_x
		inner_min_y = min_y
		inner_max_x = max_x
		inner_max_y = max_y
		
		# For exclusive, outer bounds expand by exact width
		outer_min_x = min_x - width
		outer_min_y = min_y - width
		outer_max_x = max_x + width
		outer_max_y = max_y + width
	
	# Add all cells in the outer rectangle
	for x in range(outer_min_x, outer_max_x + 1):
		for y in range(outer_min_y, outer_max_y + 1):
			var pos: Vector2i = Vector2i(x, y)
			
			# For inclusive, include cells on or outside inner bounds
			# For exclusive, only include cells that are outside inner bounds
			if inclusive:
				if x <= inner_min_x or x >= inner_max_x or y <= inner_min_y or y >= inner_max_y:
					cells.append(pos)
			else:
				if x < inner_min_x or x > inner_max_x or y < inner_min_y or y > inner_max_y:
					cells.append(pos)
	
	return cells

func get_surrounding_tiles(given_tile: Vector2i, spread: int = 1) -> Array[Vector2i]:
	Log.pr("Getting surrounding tiles for tile: ", given_tile, " with spread: ", spread)
	var surrounding_tiles: Array[Vector2i] = []
	
	# Loop from -spread to +spread in both directions
	for y in range(-spread, spread + 1):
		for x in range(-spread, spread + 1):
			# Skip the center tile if you don't want it included
			if x == 0 and y == 0:
				continue
				
			var target_tile = given_tile + Vector2i(x, y)
			surrounding_tiles.append(target_tile)
			
	Log.pr("Surrounding tiles: ", surrounding_tiles)
	return surrounding_tiles