ProviderBase = require './ProviderBase'
Metacritic = require './Metacritic'
IGN = require './IGN'
async = require 'async'

class AllRatings extends ProviderBase
	constructor: (@data) ->

	ratings: (args, callback) ->
		additonal = 
			ratings: {}

		async.concat [Metacritic, IGN], (Provider, next) =>
			provider = new Provider @data
			provider.ratings(args, 
				(E, add) =>
					next E, (add.ratings or [])
				)
		,(E, ratings) =>
			ratings.sort (a,b) ->
				return a.score - b.score 

			async.reduce ratings, 0, (memo, item, next) ->
				next null, (memo + item.score)
			,(E, overall) ->
				additonal['ratings'].overall = ((overall / 20) / ratings.length)
				additonal['ratings'].ratings = ratings
				callback E, additonal
				

module.exports = AllRatings