 #  tags
 ##}
 #]
-##+##
+#
+
+
+

Util = require 'util'

prefix = 'https://phet.colorado.edu'

#Scrape the list of Phet sims
f = new Future()


		sims.push {name: name, url: url}

	#for sim in sims
	#	console.log "#{sim.name} @ #{sim.url}"
	console.log "#{sims.length} simulations"
	f.return(sims)

#Scrape each sim page individually
f.then( (sims) ->
	urls = _.pluck sims, 'url'
	failed = []

	scrape.concurrent urls, 5, (url, next) ->
		if url
			#Find the current sim by URL. findWhere->ref. Not a copy.
			_sim = _.findWhere sims, {url: url}

			#console.log "Scraping simulation@: #{url}"
			scrape.request url, (err, $)->
				if err
					console.log "#{err} #{_sim.name}"# Most often->connection failure
					failed.push _sim
				else
					

					#Extract information for this sim's page
					#idk why but some dont have a download link [...?]. Skip these.
					_sim['download_url'] 	= ($('#sim-download').first()?.attribs.href || (console.log("#{_sim.name}--no download url"); next())) #Skip if no download url.
					_sim['latest_version'] 	= toVersion $('.sim-version').first().text
					_sim['image_url']		= $('#simulation-main-screenshot-image').first().attribs.src
					_sim['type']			= getSimType $('.sim-page-badge').first()

					console.log "#{_sim.name} :: type –> #{_sim.type}"

				next()
		else #Called when done
			#return Future({failed: failed, sims: sims})
			for element in (_.pluck failed, 'name')
				console.log "Failed: #{element} "
			return f.return(sims)
).then( (sims)->
	#writeConfig({sims: sims})

	#console.log sims
)


##
