extends Node

class_name MapBuilderClass

# Function to copy a TileMap layer from one scene to a target TileMap at a specific grid position
func copy_tilemap_to_target(source_scene, target_tilemap: TileMapLayer, target_layer: String, grid_position: Vector2i):
	# First, load and instantiate the source scene if it's a resource path
	Log.pr("Copying tilemap from source scene to target tilemap at grid position: ", grid_position)
	Log.pr("Source scene: ", source_scene)
	var source_instance
	if source_scene is String:
		source_instance = load(source_scene).instantiate()
	elif source_scene is PackedScene:
		Log.pr("Source scene is a PackedScene, instantiating it")
		source_instance = source_scene.instantiate()
	else:
		source_instance = source_scene
	
	# Find the source TileMap in the loaded scene
	var source_tilemap = find_tilemap_by_name(source_instance, target_layer)
	if not source_tilemap:
		push_error("Could not find TileMapLayer in source scene", source_tilemap)
		if source_instance is Node and source_instance.is_inside_tree():
			source_instance.queue_free()
		return
	
	# Get the size of the source room in tiles
	var source_used_cells = source_tilemap.get_used_cells() # 0 is the layer index
	if source_used_cells.size() == 0:
		push_warning("Source TileMapLayer has no cells")
		if source_instance is Node and source_instance.is_inside_tree():
			source_instance.queue_free()
		return

	# Calculate the offset for placement in the target grid
	var offset_x = grid_position.x * Global.ROOM_WIDTH
	var offset_y = grid_position.y * Global.ROOM_HEIGHT
	
	# Copy cells from source to target with proper offset
	for cell in source_used_cells:
		var target_cell = Vector2i(cell.x + offset_x, cell.y + offset_y)
		
		# Get the tile data from source
		#var source_tile_data = source_tilemap.get_cell_tile_data(cell)
		var source_atlas_coords = source_tilemap.get_cell_atlas_coords(cell)
		var source_alternative_tile = source_tilemap.get_cell_alternative_tile(cell)
		
		# Set the same tile in the target TileMap
		target_tilemap.set_cell(target_cell, source_tilemap.get_cell_source_id(cell), source_atlas_coords, source_alternative_tile)
	

	# Clean up the source instance if we instantiated it
	if source_instance is Node and source_instance.is_inside_tree():
		source_instance.queue_free()


# Helper function to find a TileMap by name in a scene
func find_tilemap_by_name(root_node: Node, tilemap_name: String) -> TileMapLayer:
	if root_node is TileMapLayer and root_node.name == tilemap_name:
		return root_node
	
	for child in root_node.get_children():
		var result = find_tilemap_by_name(child, tilemap_name)
		if result:
			return result
	
	return null


func redraw_terrain(positions: Array, layer: TileMapLayer, terrain_set: int, terrain: int) -> void:
	Log.pr("Filtering and redrawing surrounding tiles", positions)
	
	# Filter positions to only include cells with terrainset 0 and terrain 0
	var filtered_positions: Array = []
	for cell in positions:
		var tile_data = layer.get_cell_tile_data(cell)

		if tile_data and tile_data.get_terrain_set() == terrain_set and tile_data.get_terrain() == terrain:
			filtered_positions.append(cell)
	
	# Only redraw if we have filtered cells
	if filtered_positions.size() > 0:
		Log.pr("Redrawing filtered tiles:", filtered_positions)
		layer.set_cells_terrain_connect(filtered_positions, terrain_set, terrain)
	else:
		Log.pr("No tiles to redraw after filtering")
