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.posterdetails.ApiMovieDetails;
import com.tobi.movies.utils.MovieRobot;

import org.junit.Before;
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();

    private ApiMoviePoster apiMoviePoster;

    @Before
    public void setUp() throws Exception {
        apiMoviePoster = createApiMoviePoster(MOVIE_ID, POSTER_PATH);
    }

    @Test
    public void shouldShowPoster() throws Exception {
        MovieRobot
                .createRobot(backend)
                .withRemoteMoviePosters(apiMoviePoster)
                .launchPopularMovies(rule)
                .checkPosterWithPathIsDisplayedAtPosition(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 ApiMoviePoster createApiMoviePoster(long movieId, String posterPath) {
        ApiMoviePoster poster = new ApiMoviePoster();
        poster.movieId = movieId;
        poster.posterPath = posterPath;
        return poster;
    }

    private ApiMovieDetails createMovieDetails(long movieId, String movieTitle, String movieOverview, String posterPath) {
        ApiMovieDetails apiMovieDetails = new ApiMovieDetails();
        apiMovieDetails.originalTitle = movieTitle;
        apiMovieDetails.movieId = movieId;
        apiMovieDetails.overview = movieOverview;
        apiMovieDetails.posterPath = posterPath;
        return apiMovieDetails;
    }

    private void givenBackendReturnsPopularStreamWith(long movieId, String posterPath) {
        backend.addToPopularStream(createApiMoviePoster(movieId, posterPath));
    }

    private void givenBackendReturnsMovieDetails(long movieId, String movieTitle, String movieOverview, String posterPath) {
        backend.addMovieDetails(createMovieDetails(movieId, movieTitle, movieOverview, posterPath));
    }
}

@@ -90,74 +90,8 @@
 ry;%0A
-import android.support.test.espresso.contrib.RecyclerViewActions;%0A
 impo
@@ -137,16 +137,16 @@
 stRule;%0A
+
 import a
@@ -278,34 +278,8 @@
 on;%0A
-import com.tobi.movies.R;%0A
 impo
@@ -508,286 +508,8 @@
 h;%0A%0A
-import static android.support.test.espresso.Espresso.onView;%0Aimport static android.support.test.espresso.action.ViewActions.click;%0Aimport static android.support.test.espresso.assertion.ViewAssertions.matches;%0Aimport static android.support.test.espresso.matcher.ViewMatchers.*;%0A%0A
 @Run
@@ -1376,16 +1376,58 @@
 ePoster;
+%0A    private ApiMovieDetails movieDetails;
 %0A%0A    @B
@@ -1537,32 +1537,130 @@
 , POSTER_PATH);%0A
+        movieDetails = createMovieDetails(MOVIE_ID, MOVIE_TITLE, MOVIE_DESCRIPTION, POSTER_PATH);%0A
     %7D%0A%0A    @Test
@@ -1788,20 +1788,19 @@
        .
-with
+add
 RemoteMo
@@ -1808,17 +1808,16 @@
 iePoster
-s
 (apiMovi
@@ -2038,229 +2038,188 @@
     
-givenBackendReturnsPopularStreamWith(MOVIE_ID, POSTER_PATH);%0A        givenBackendReturnsMovieDetails(MOVIE_ID, MOVIE_TITLE, MOVIE_DESCRIPTION, POSTER_PATH);%0A        rule.launchActivity(null);%0A%0A        onView(withId(R.id.p
+MovieRobot%0A                .createRobot(backend)%0A                .addRemoteMoviePoster(apiMoviePoster)%0A                .addRemoteMovieDetails(movieDetails)%0A                .launchP
 opul
@@ -2230,18 +2230,13 @@
 vies
-_recycler)
+(rule
 )%0A  
@@ -2254,48 +2254,20 @@
    .
-perform(RecyclerViewActions.actionOnItem
+selectPoster
 AtPo
@@ -2278,163 +2278,134 @@
 on(0
-, click()));%0A%0A        onView(withText(MOVIE_TITLE)).check(matches(isDisplayed()));%0A        onView(withText(MOVIE_DESCRIPTION)).check(matches(isDisplayed())
+)%0A                .checkMovieTitleIsDisplayed(MOVIE_TITLE)%0A                .checkMovieDescriptionIsDisplayed(MOVIE_DESCRIPTION
 );%0A 
@@ -3025,32 +3025,32 @@
 h = posterPath;%0A
+
         return a
@@ -3074,415 +3074,7 @@
    %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    %7D
 %0A%7D%0A

Test shouldNavigateToMovieDetails with MovieRobot