ProviderBase = require './ProviderBase'
request = require 'request'
torify = require 'torify'
cheerio = require 'cheerio'
async = require 'async'

class FindTheBest extends ProviderBase
	constructor: (@data) ->
		@find_the_best_metadata()

	find_the_best_metadata: (callback) ->
		return callback null
		torify
			url: 'http://video-games.findthebest.com/ajax_search_bar'
			jar: request.jar()
			qs:
				s: (@data.title).replace(/\s/ig, '+')
				pt: 'detail'
				category_id: 0
			timeout: (20 * 1000)
			no_cache: true
			retry_if: (E, res, body, callback) ->
				callback (body and body.indexOf('rate-limit-why') isnt -1), res, body
			headers:
				'Host': 'video-games.findthebest.com'
				'Referer': 'http://non-profit-organizations.findthebest.com'
				'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36'
			,(E, res, body) =>
				try 
					@find_the_best = JSON.parse(body).sections.shift().results.filter(
						(x) -> x.id
					).shift()
					@app_id = body.split('"app_id":').pop().split(',').shift().replace(/\"/ig, '')
					torify 
						url: @find_the_best.url
						jar: request.jar()
						timeout: (20 * 1000)
						headers: 
							'Referer': 'http://video-games.findthebest.com'
							'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36'
					,(E, res, body) =>
						try 
							@asin = body.split('asin":"').pop().split('"').shift()
							callback()
						catch error
							callback and callback(error)
				catch error
					callback and callback(error)


	ratings: (args, callback) ->
		@find_the_best_metadata () =>
			if !@find_the_best or !@find_the_best.id
				return callback null, {}
			additonal = {}
			additonal['scores'] = []

			torify
				url: 'http://video-games.findthebest.com/ajax_smart_rating'
				qs:
					type: 'get_listing_rating'
					listing_id: @find_the_best.id
					timeout: (1000 * 10)
				,(E, res, body) =>
					try
						data = JSON.parse(body)

						additonal['ratings'] = 
							overall: parseFloat(data.rating)
							makeup: ((x) ->
								return Object.keys(x)
								.filter (y) => return x[y][0] isnt null
								.map (y) =>
									return {
										source: y
										score: parseFloat(x[y][0])
									}
								)(data.sr_buckets)

						callback null, additonal
					catch error
						callback and callback error




	prices: (args, callback) ->
		@find_the_best_metadata () =>
			additonal = {}
			additonal['prices'] = []

			torify
				timeout: (1000 * 10)
				url: 'http://video-games.findthebest.com/ajax_pricing'
				qs:
					'd[0][ret_id]':2
					,'d[0][asin]': @asin
					,'d[0][i]':1
					,'d[1][ret_id]':1
					,'d[1][asin]': @asin
					,'d[1][i]':0
				,(E, res, body) =>
					try
						data = ((JSON.parse body))
						async.map data.i1, (price, next) =>
							next null, 
							store: price.ret
							price: parseFloat(price.prc)
						,(E, prices) =>
							additonal['prices'] = prices
							callback E, additonal
					catch error
						callback error, additonal

module.exports = FindTheBest