class_name StandardWorldGenerator
extends WorldGenerator

@export
var rooms: Array[PackedScene] = []

var _rooms_left: Array[PackedScene] = []
var _rooms_right: Array[PackedScene] = []
var _rooms_top: Array[PackedScene] = []
var _rooms_bottom: Array[PackedScene] = []
var _rooms_blocking: Array[PackedScene] = []

var _progress_tracker: ProgressTracker = ProgressTracker.new(1)

func _generate(map: TileMap) -> void:
	self._sort_rooms(_progress_tracker.next_step("Sorting Rooms"))

func _sort_rooms(step_tracker: ProgressStepTracker) -> void:
	step_tracker.complete.call_deferred()
	
	step_tracker.set_substeps(len(self.rooms))
	for room_scene in rooms:
		step_tracker.substep(room_scene.get_name())
		var room: StandardRoom = room_scene.instantiate()
		if room.left:
			self._rooms_left.append(room_scene)
		if room.right:
			self._rooms_right.append(room_scene)
		if room.top:
			self._rooms_top.append(room_scene)
		if room.bottom:
			self._rooms_bottom.append(room_scene)
		if room.is_block():
			self._rooms_blocking.append(room_scene)
			
		room.queue_free()
	
	if len(self._rooms_left) == 0:
		push_error("0 left rooms!")
	if len(self._rooms_right) == 0:
		push_error("0 right rooms!")
	if len(self._rooms_top) == 0:
		push_error("0 top rooms!")
	if len(self._rooms_bottom) == 0:
		push_error("0 bottom rooms!")
	if len(self._rooms_blocking) == 0:
		push_warning("0 blocking rooms!")

func _get_progress_tracker() -> ProgressTracker:
	return self._progress_tracker
