package pl.acieslinski.moviefun.managers;

import android.content.Context;

import pl.acieslinski.moviefun.connection.ApiAdapter;
import pl.acieslinski.moviefun.models.Search;
import pl.acieslinski.moviefun.models.Video;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Arkadiusz Cieśliński 26.03.16.
 *         <acieslinski@gmail.com>
 */

public class ApiManager {
    private ApiAdapter mApiAdapter;

    public interface MoviesCallback {
        void onVideo(Video video);
        void onCompleted();
        void onError(Throwable error);
    }

    public ApiManager(Context context) {
        mApiAdapter = new ApiAdapter(context);
    }

    public void fetchMovies(Search search, MoviesCallback callback) {
        mApiAdapter.searchMovies(search)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.newThread())
                .filter(video -> video.isPosterAvailable())
                .doOnNext(video -> callback.onVideo(video))
                .doOnCompleted(() -> callback.onCompleted())
                .doOnError((error) -> callback.onError(error))
                .subscribe();
    }
}

@@ -214,16 +214,38 @@
 .Video;%0A
+import rx.Subscriber;%0A
 import r
@@ -1012,52 +1012,162 @@
     
-.doOnNext(video -%3E callback.onVideo(video))%0A
+// must be this way - https://github.com/square/retrofit/issues/1214%0A                .subscribe(new Subscriber%3CVideo%3E() %7B%0A                    @Override%0A  
     
@@ -1178,20 +1178,31 @@
         
-.doO
+  public void o
 nComplet
@@ -1208,128 +1208,408 @@
 ted(
-(
 ) 
--%3E callback.onCompleted())%0A                .doOnError((error) -%3E callback.onError(error))%0A                .subscribe(
+%7B%0A                        callback.onCompleted();%0A                    %7D%0A%0A                    @Override%0A                    public void onError(Throwable e) %7B%0A                        callback.onError(e);%0A                    %7D%0A%0A                    @Override%0A                    public void onNext(Video video) %7B%0A                        callback.onVideo(video);%0A                    %7D%0A                %7D
 );%0A 

fix for the rxjava error