package dynamo.trakt;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;

import org.apache.oltu.oauth2.common.exception.OAuthSystemException;

import com.omertron.themoviedbapi.MovieDbException;
import com.uwetrottmann.trakt5.entities.Movie;

import dynamo.core.Enableable;
import dynamo.core.Language;
import dynamo.core.configuration.ClassDescription;
import dynamo.core.manager.ErrorManager;
import dynamo.movies.model.MovieManager;
import dynamo.suggesters.movies.MovieSuggester;

@ClassDescription(label="Trakt Recommendations")
public class TraktMovieSuggester implements MovieSuggester, Enableable {
	
	@Override
	public boolean isEnabled() {
		return TraktManager.getInstance().isEnabled();
	}

	@Override
	public void suggestMovies() throws IOException, URISyntaxException {

		if (!TraktManager.getInstance().isEnabled()) {
			return;
		}

		try {
			List<Movie> recommandations = TraktManager.getInstance().getMovieRecommandations();
			for (Movie recommendation : recommandations) {
				try {
					MovieManager.getInstance().suggestImdbId( recommendation.ids.imdb, null, Language.EN, recommendation.homepage );
				} catch (MovieDbException | ParseException | InterruptedException e) {
					ErrorManager.getInstance().reportThrowable( e );
				}
			}
		} catch (OAuthSystemException e) {
			ErrorManager.getInstance().reportThrowable( e );
		}
	}

}

@@ -1045,16 +1045,52 @@
 ons();%0D%0A
+%09%09%09if (recommandations != null) %7B%0D%0A%09
 %09%09%09for (
@@ -1136,23 +1136,25 @@
  %7B%0D%0A%09%09%09%09
+%09
 try %7B%0D%0A
+%09
 %09%09%09%09%09Mov
@@ -1260,24 +1260,25 @@
 omepage );%0D%0A
+%09
 %09%09%09%09%7D catch 
@@ -1346,16 +1346,17 @@
 %7B%0D%0A%09%09%09%09%09
+%09
 ErrorMan
@@ -1393,24 +1393,32 @@
 able( e );%0D%0A
+%09%09%09%09%09%7D%0D%0A
 %09%09%09%09%7D%0D%0A%09%09%09%7D%0D

Fix possible NPE