--[[
	Randomly picks a tool type depending on weight, and spawns it in specified position.
]]

local function createRandomToolAtPosition(position)
	local randomPick = random:NextInteger(1, 100)

	local tool
	if randomPick > 92 then
		tool = torch:Clone()
	elseif randomPick > 80 then
		tool = gravityFuel:Clone()
	elseif randomPick > 10 then
		tool = repairKit:Clone()
	elseif randomPick > 5 then
		tool = smallCargo:Clone()
	elseif randomPick > 3 then
		tool = largeCargo:Clone()
	else
		tool = broom:Clone()
	end

	tool.Position = position

	tool.Parent = workspace.Tools

	tool:SetNetworkOwner(nil)
end

return function()
	local rooms = RoomManager.getRooms()

	for _, room in pairs(rooms) do
		local regions = room:getRegions()
		local toolsToSpawnPerRegion = TOTAL_TOOLS_PER_ROOM / #regions

		for _, region in pairs(regions) do
			for i = 1, toolsToSpawnPerRegion do
				local position = getRandomClearLocationInRegionAsync(region, AREA_CLEAR_SIZE)

				createRandomToolAtPosition(position)
			end
		end
	end
end
