package com.nikita.recipiesapp.views.recipes;

import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.nikita.recipiesapp.R;
import com.nikita.recipiesapp.common.AppState;
import com.nikita.recipiesapp.common.models.Recipe;
import com.nikita.recipiesapp.middlewares.DataLoadingMiddleware;

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

import java.util.List;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.nikita.recipiesapp.EspressoTestUtils.callRender;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class RecipesActivityTest {

  @Rule
  public ActivityTestRule<RecipesActivity> mActivityRule = new ActivityTestRule<>(
    RecipesActivity.class);

  @Test
  public void shouldHideLoading() {
    callRender(mActivityRule, AppState.initial().withLoading(false));

    onView(withId(R.id.loading_indicator))
      .check(doesNotExist());
  }

  @Test
  public void shouldShowLoading() {
    callRender(mActivityRule, AppState.initial().withLoading(true));

    onView(withId(R.id.loading_indicator))
      .check(matches(isDisplayed()));
  }

  @Test
  public void shouldShowError() {
    String errorText = "text text";
    callRender(mActivityRule, AppState.initial().withError(errorText));

    onView(withText(errorText))
      .check(matches(isDisplayed()));
  }

  @Test
  public void shouldShowFirstRecipe() throws Exception {
    List<Recipe> dummyRecipies = DataLoadingMiddleware.getDummyRecipies();
    callRender(mActivityRule, AppState.initial().withRecipes(dummyRecipies));

    onView(withText(dummyRecipies.get(0).name))
            .check(matches(isDisplayed()));
  }

  @Test
  public void shouldShowLastRecipe() throws Exception {
    List<Recipe> dummyRecipies = DataLoadingMiddleware.getDummyRecipies();
    callRender(mActivityRule, AppState.initial().withRecipes(dummyRecipies));
    int lastIndex = dummyRecipies.size() - 1;

    onView(withId(R.id.recycler_view))
        .perform(scrollToPosition(lastIndex));

    onView(withText(dummyRecipies.get(lastIndex).name))
            .check(matches(isDisplayed()));
  }

  public void shouldOpenRecipeScreenOnItemClick() {
    // TODO add intent checking
  }
}
@@ -40,16 +40,69 @@
 cipes;%0A%0A
+import android.support.test.espresso.intent.Intents;%0A
 import a
@@ -438,16 +438,76 @@
 dleware;
+%0Aimport com.nikita.recipiesapp.views.steps.StepListActivity;
 %0A%0Aimport
@@ -665,16 +665,86 @@
 onView;%0A
+import static android.support.test.espresso.action.ViewActions.click;%0A
 import s
@@ -968,32 +968,285 @@
 Actions.
-scrollToPosition
+actionOnItemAtPosition;%0Aimport static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;%0Aimport static android.support.test.espresso.intent.Intents.intended;%0Aimport static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent
 ;%0Aimport
@@ -1661,25 +1661,20 @@
 tivity%3E 
-mActivity
+test
 Rule = n
@@ -1780,33 +1780,28 @@
  callRender(
-mActivity
+test
 Rule, AppSta
@@ -1968,33 +1968,28 @@
  callRender(
-mActivity
+test
 Rule, AppSta
@@ -2197,33 +2197,28 @@
  callRender(
-mActivity
+test
 Rule, AppSta
@@ -2480,33 +2480,28 @@
  callRender(
-mActivity
+test
 Rule, AppSta
@@ -2798,17 +2798,12 @@
 der(
-mActivity
+test
 Rule
@@ -3091,16 +3091,24 @@
 );%0A  %7D%0A%0A
+  @Test%0A
   public
@@ -3153,41 +3153,376 @@
 k() 
-%7B%0A    // TODO add intent checking
+throws Exception %7B%0A    List%3CRecipe%3E dummyRecipies = DataLoadingMiddleware.getDummyRecipies();%0A    callRender(testRule, AppState.initial().withRecipes(dummyRecipies));%0A%0A    Intents.init();%0A%0A    onView(withId(R.id.recycler_view))%0A        .perform(actionOnItemAtPosition(0, click()));%0A%0A    intended(hasComponent(StepListActivity.class.getName()));%0A%0A    Intents.release();
 %0A  %7D

Add step list activity opening test