package kr.co.zumo.app.lifeplus.activity;

import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;

import kr.co.zumo.app.R;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
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.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

public class MainActivityTest {

  @Rule
  public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);

  @Test
  public void startFragment() {
    ViewInteraction appCompatButton = onView(
      allOf(withId(R.id.button_login), withText("Login"),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          0),
        isDisplayed()));
    appCompatButton.perform(click());

    ViewInteraction button = onView(
      allOf(withId(R.id.button_sign_up),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          1),
        isDisplayed()));
    button.check(matches(isDisplayed()));

    ViewInteraction button2 = onView(
      allOf(withId(R.id.button_back),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          0),
        isDisplayed()));
    button2.check(matches(isDisplayed()));

    ViewInteraction appCompatButton2 = onView(
      allOf(withId(R.id.button_sign_up), withText("Sign Up"),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          1),
        isDisplayed()));
    appCompatButton2.perform(click());

    ViewInteraction button3 = onView(
      allOf(withId(R.id.button_back),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          0),
        isDisplayed()));
    button3.check(matches(isDisplayed()));

    ViewInteraction appCompatButton3 = onView(
      allOf(withId(R.id.button_back), withText("Back"),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          0),
        isDisplayed()));
    appCompatButton3.perform(click());

    ViewInteraction button4 = onView(
      allOf(withId(R.id.button_back),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          0),
        isDisplayed()));
    button4.check(matches(isDisplayed()));

    ViewInteraction button5 = onView(
      allOf(withId(R.id.button_sign_up),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          1),
        isDisplayed()));
    button5.check(matches(isDisplayed()));

    pressBack();

    ViewInteraction button6 = onView(
      allOf(withId(R.id.button_login),
        childAtPosition(
          childAtPosition(
            withId(R.id.container),
            0),
          0),
        isDisplayed()));
    button6.check(matches(isDisplayed()));

  }

  private static Matcher<View> childAtPosition(
    final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
      @Override
      public void describeTo(Description description) {
        description.appendText("Child at position " + position + " in parent ");
        parentMatcher.describeTo(description);
      }

      @Override
      public boolean matchesSafely(View view) {
        ViewParent parent = view.getParent();
        return parent instanceof ViewGroup && parentMatcher.matches(parent)
          && view.equals(((ViewGroup) parent).getChildAt(position));
      }
    };
  }
}