package com.tobi.movies.popularstream;

import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.tobi.movies.EspressoDependencies;
import com.tobi.movies.MovieApplication;
import com.tobi.movies.R;
import com.tobi.movies.backend.ConfigurableBackend;
import com.tobi.movies.matchers.PosterMatcher;
import com.tobi.movies.posterdetails.ApiMovieDetails;

import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.*;

@RunWith(AndroidJUnit4.class)
public class PopularMoviesActivityTest {

    private static final long MOVIE_ID = 293660L;
    private static final String POSTER_PATH = "deadpool.jpg";
    private static final String MOVIE_TITLE = "Deadpool";
    private static final String MOVIE_DESCRIPTION = "Awesome movie";

    private final ActivityTestRule<PopularMoviesActivity> rule = new ActivityTestRule<PopularMoviesActivity>(PopularMoviesActivity.class) {
        @Override
        protected void beforeActivityLaunched() {
            MovieApplication movieApplication = (MovieApplication) InstrumentationRegistry.getTargetContext().getApplicationContext();
            movieApplication.setDependencies(new EspressoDependencies(backend));
        }
    };

    private final ConfigurableBackend backend = new ConfigurableBackend();

    @Test
    public void shouldShowPoster() throws Exception {
        givenBackendReturnsPopularStreamWith(MOVIE_ID, POSTER_PATH);

        rule.launchActivity(null);

        onView(withId(R.id.popularMovies_recycler))
                .check(matches(PosterMatcher.hasPosterAt(0, POSTER_PATH)));
    }

    @Test
    public void shouldNavigateToMovieDetails() throws Exception {
        givenBackendReturnsPopularStreamWith(MOVIE_ID, POSTER_PATH);
        givenBackendReturnsMovieDetails(MOVIE_ID, MOVIE_TITLE, MOVIE_DESCRIPTION, POSTER_PATH);
        rule.launchActivity(null);

        onView(withId(R.id.popularMovies_recycler))
                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

        onView(withText(MOVIE_TITLE)).check(matches(isDisplayed()));
        onView(withText(MOVIE_DESCRIPTION)).check(matches(isDisplayed()));
    }

    private void givenBackendReturnsPopularStreamWith(long movieId, String posterPath) {
        ApiMoviePoster poster = new ApiMoviePoster();
        poster.movieId = movieId;
        poster.posterPath = posterPath;
        backend.addToPopularStream(poster);
    }

    private void givenBackendReturnsMovieDetails(long movieId, String movieTitle, String movieOverview, String posterPath) {
        ApiMovieDetails apiMovieDetails = new ApiMovieDetails();
        apiMovieDetails.originalTitle = movieTitle;
        apiMovieDetails.movieId = movieId;
        apiMovieDetails.overview = movieOverview;
        apiMovieDetails.posterPath = posterPath;
        backend.addMovieDetails(apiMovieDetails);
    }
}

@@ -445,30 +445,37 @@
 ies.
-matchers.PosterMatcher
+posterdetails.ApiMovieDetails
 ;%0Aim
@@ -499,39 +499,51 @@
 ies.
-posterdeta
+ut
 ils.
-Api
 Movie
-Details;%0A
+Robot;%0A%0Aimport org.junit.Before;
 %0Aimp
@@ -1704,29 +1704,75 @@
 end();%0A%0A    
-@Test
+private ApiMoviePoster apiMoviePoster;%0A%0A    @Before
 %0A    public 
@@ -1781,23 +1781,12 @@
 id s
-houldShowPoster
+etUp
 () t
@@ -1815,134 +1815,278 @@
     
-givenBackendReturnsPopularStreamWith(MOVIE_ID, POSTER_PATH);%0A%0A        rule.launchActivity(null);%0A%0A        onView(withId(R.id.p
+apiMoviePoster = createApiMoviePoster(MOVIE_ID, POSTER_PATH);%0A    %7D%0A%0A    @Test%0A    public void shouldShowPoster() throws Exception %7B%0A        MovieRobot%0A                .createRobot(backend)%0A                .withRemoteMoviePosters(apiMoviePoster)%0A                .launchP
 opul
@@ -2089,34 +2089,29 @@
 opularMovies
-_recycler)
+(rule
 )%0A          
@@ -2126,42 +2126,43 @@
 heck
-(matches(PosterMatcher.hasPosterAt
+PosterWithPathIsDisplayedAtPosition
 (0, 
@@ -2173,18 +2173,16 @@
 ER_PATH)
-))
 ;%0A    %7D%0A
@@ -2761,49 +2761,43 @@
 ate 
-void givenBackendReturnsPopularStreamWith
+ApiMoviePoster createApiMoviePoster
 (lon
@@ -2968,42 +2968,21 @@
     
-backend.addToPopularStream(
+return 
 poster
-)
 ;%0A  
@@ -3002,32 +3002,30 @@
 ate 
-void givenBackendReturns
+ApiMovieDetails create
 Movi
@@ -3380,47 +3380,437 @@
     
-backend.addMovieDetails(apiMovieDetails
+return apiMovieDetails;%0A    %7D%0A%0A    private void givenBackendReturnsPopularStreamWith(long movieId, String posterPath) %7B%0A        backend.addToPopularStream(createApiMoviePoster(movieId, posterPath));%0A    %7D%0A%0A    private void givenBackendReturnsMovieDetails(long movieId, String movieTitle, String movieOverview, String posterPath) %7B%0A        backend.addMovieDetails(createMovieDetails(movieId, movieTitle, movieOverview, posterPath)
 );%0A 

Use MovieRobot in test shouldShowPoster