var http = require('http')
var request = require('request')
var util = require('../util')

exports.rate = function(req, res) {
	if (!req.session['uid']) {
		res.redirect('/login');
		return;
	}
	var redirect = req.query['redirect'] ? req.query['redirect'] :'/clip'

	var movie_id = req.query['movie_id'];
	var rating = req.query['rating'];

	var film_uri = '/rest/1.0/film/'+movie_id+'/'
	var ratingNum = parseInt(rating)
	if (!ratingNum) {
			util.getCurrentUser(req, function(user) {
				if (!user.skips) {
					user.skips = [];
				}
				user.skips.push(movie_id);
				console.log(user.skips);
				user.save(function() {
					res.redirect(redirect)
				})
			});
	} else {
		if (ratingNum > 5) {
			ratingNum = '';
		} else {
			ratingNum = ratingNum * 2; // translate to filmaster scale of 1 to 10
		}
		request ( {
			'uri': 'http://api-test.filmaster.tv/rest/1.0/user/'+req.session['uid']+'/ratings/',
			'auth': {
				'user': 'test_imdb',
				'pass': 'test'
			},
			'method': 'POST',
			'form': {
				rating: ratingNum,
				film_uri: film_uri
			},
		}, function (error, result, body) {
			if (error) {
				console.log(error)
			}

			request( {
				'uri': 'http://www.omdbapi.com/?i=tt'+movie_id
			}, function (error, result, body) {
				var info = JSON.parse(body);
				res.render('rate', {
					'title': info['Title'],
					'rating': rating,
					'img_url': info['Poster']
				})
			});

		});

	}


}
@@ -1151,31 +1151,32 @@
 ( %7B%0A%09%09%09%09
+%09%09
 'uri':
- 
 'http://
 www.omdb
@@ -1171,111 +1171,279 @@
 p://
-www.omdbapi.com/?i=tt'+movie_id%0A%09%09%09%7D, function (error, result, body) %7B%0A%09%09%09%09var info = JSON.parse(body);
+api.themoviedb.org/3/find/tt'+movie_id+'?api_key=2a6cef5c6e0babed20a40c35e56881cd&external_source=imdb_id'%0A%09%09%09%7D, function (error, result, body) %7B%0A%09%09%09%09var movie = JSON.parse(body)%5B'movie_results'%5D%5B0%5D%0A%0A%09%09%09%09if (!movie) %7B%0A%09%09%09%09%09movie = JSON.parse(body)%5B'tv_results'%5D%5B0%5D%0A%09%09%09%09%7D%0A
 %0A%09%09%09
@@ -1482,15 +1482,16 @@
 e': 
-info%5B'T
+movie%5B't
 itle
@@ -1537,20 +1537,63 @@
 l': 
-info%5B'P
+'http://image.tmdb.org/t/p/original'+movie%5B'p
 oster
+_path
 '%5D%0A%09

use themoviedb for /rate also.