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


import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
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.hamcrest.core.IsInstanceOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import kr.co.zumo.app.R;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
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;

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

  @Rule
  public ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);

  @Test
  public void mobileAuthBirthDateValidationTest() {
    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
      Thread.sleep(700);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    ViewInteraction clearEditText = onView(
      allOf(withId(R.id.edit_text_birth_date),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            4),
          0),
        isDisplayed()));
    clearEditText.perform(replaceText("1994"), closeSoftKeyboard());

    ViewInteraction textView = onView(
      allOf(withId(R.id.text_view_birth_validation), withText("YYYYMMDD 숫자를 입력해 주세요"),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            6),
          0),
        isDisplayed()));
    textView.check(matches(isDisplayed()));

    ViewInteraction clearEditText2 = onView(
      allOf(withId(R.id.edit_text_birth_date), withText("1994"),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            4),
          0),
        isDisplayed()));
    clearEditText2.perform(replaceText("19940120"));

    ViewInteraction clearEditText3 = onView(
      allOf(withId(R.id.edit_text_birth_date), withText("19940120"),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            4),
          0),
        isDisplayed()));
    clearEditText3.perform(closeSoftKeyboard());

    ViewInteraction linearLayout = onView(
      allOf(childAtPosition(
        allOf(withId(R.id.linear_layout_identify),
          childAtPosition(
            IsInstanceOf.<View>instanceOf(android.widget.ScrollView.class),
            0)),
        6),
        isDisplayed()));
    linearLayout.check(doesNotExist());

    ViewInteraction clearEditText4 = onView(
      allOf(withId(R.id.edit_text_birth_date),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            4),
          0),
        isDisplayed()));
    clearEditText4.perform(replaceText("20150101"), closeSoftKeyboard());

    ViewInteraction textView3 = onView(
      allOf(withId(R.id.text_view_birth_validation), withText("만 14세 미만 청소년의 경우 가입이 제한 됩니다"),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            6),
          0),
        isDisplayed()));
    textView3.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));
      }
    };
  }
}
