package com.setiawanpaiman.tmdb.android.moviedetail;

import com.setiawanpaiman.tmdb.android.data.source.MovieRepository;
import com.setiawanpaiman.tmdb.android.data.viewmodel.MovieViewModel;
import com.setiawanpaiman.tmdb.android.util.scheduler.BaseSchedulerProvider;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;

/**
 * Created by Setiawan Paiman on 1/7/17.
 */

class MovieDetailPresenter implements MovieDetailContract.Presenter {

    private final long mMovieId;
    private final MovieDetailContract.View mMovieDetailView;
    private final MovieRepository mMovieRepository;
    private final BaseSchedulerProvider mSchedulerProvider;

    @NonNull
    private CompositeDisposable mComposite;

    @Inject
    MovieDetailPresenter(@NonNull long movieId,
                         MovieDetailContract.View movieDetailView,
                         MovieRepository movieRepository,
                         BaseSchedulerProvider schedulerProvider) {
        this.mMovieId = movieId;
        this.mMovieDetailView = movieDetailView;
        this.mMovieRepository = movieRepository;
        this.mSchedulerProvider = schedulerProvider;

        this.mComposite = new CompositeDisposable();
    }

    @Override
    public void subscribe() {
        //no-op
    }

    @Override
    public void unsubscribe() {
        this.mComposite.clear();
    }

    @Override
    public void loadTrailers() {
        mComposite.add(mMovieRepository.getTrailers(mMovieId)
                .subscribeOn(mSchedulerProvider.io())
                .observeOn(mSchedulerProvider.ui())
                .subscribe(mMovieDetailView::showTrailers));
    }

    @Override
    public void loadReviews() {
        mComposite.add(mMovieRepository.getReviews(mMovieId)
                .subscribeOn(mSchedulerProvider.io())
                .observeOn(mSchedulerProvider.ui())
                .subscribe(mMovieDetailView::showReviews));
    }

    @Override
    public boolean isFavoriteMovie(MovieViewModel movieViewModel) {
        return mMovieRepository.isFavoriteMovie(movieViewModel);
    }

    @Override
    public boolean addFavoriteMovie(MovieViewModel movieViewModel) {
        return mMovieRepository.addFavoriteMovie(movieViewModel);
    }

    @Override
    public boolean removeFavoriteMovie(MovieViewModel movieViewModel) {
        return mMovieRepository.removeFavoriteMovie(movieViewModel);
    }
}

@@ -263,16 +263,45 @@
 vider;%0A%0A
+import java.util.ArrayList;%0A%0A
 import j
@@ -1725,16 +1725,107 @@
 Trailers
+,%0A                        (t) -%3E mMovieDetailView.showTrailers(new ArrayList%3C%3E()), () -%3E %7B%7D
 ));%0A    
@@ -2096,16 +2096,106 @@
 wReviews
+,%0A                        (t) -%3E mMovieDetailView.showReviews(new ArrayList%3C%3E()), () -%3E %7B%7D
 ));%0A    

Handle error of load trailers and reviews