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.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.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
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.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

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

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

  @Test
  public void mobileAuthNameValidationTest2() {
    // 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 appCompatButton2 = onView(
      allOf(withId(R.id.button_access_authority), withText("확인"),
        childAtPosition(
          childAtPosition(
            withClassName(is("android.widget.LinearLayout")),
            11),
          0)));
    appCompatButton2.perform(scrollTo(), click());

    // 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 appCompatTextView = onView(
      allOf(withId(R.id.button1_tutorial), withText("건너뛰기"),
        childAtPosition(
          childAtPosition(
            withId(R.id.tutorial_bottom_layout),
            0),
          0),
        isDisplayed()));
    appCompatTextView.perform(click());

    // 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 appCompatButton3 = onView(
      allOf(withId(R.id.button_confirm), withText("휴대폰 인증"),
        childAtPosition(
          childAtPosition(
            withClassName(is("android.widget.LinearLayout")),
            3),
          0),
        isDisplayed()));
    appCompatButton3.perform(click());

    ViewInteraction linearLayout = onView(
      allOf(withId(R.id.linear_layout_identify),
        childAtPosition(
          childAtPosition(
            withClassName(is("kr.co.zumo.app.lifeplus.view.custom.auth.MobileAuthorizationView")),
            0),
          0),
        isDisplayed()));
    linearLayout.perform(click());

    ViewInteraction clearEditText = onView(
      allOf(withId(R.id.edit_text_name),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            1),
          0),
        isDisplayed()));
    clearEditText.perform(replaceText("하"), closeSoftKeyboard());

    ViewInteraction textView = onView(
      allOf(withId(R.id.text_view_name_validation), withText("이름을 정확히 입력해주세요"),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            3),
          0),
        isDisplayed()));
    textView.check(matches(isDisplayed()));

    ViewInteraction clearEditText2 = onView(
      allOf(withId(R.id.edit_text_name), withText("하"),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            1),
          0),
        isDisplayed()));
    clearEditText2.perform(replaceText("하세"));

    ViewInteraction clearEditText3 = onView(
      allOf(withId(R.id.edit_text_name), withText("하세"),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            1),
          0),
        isDisplayed()));
    clearEditText3.perform(closeSoftKeyboard());

    textView.check(matches(not(isDisplayed())));

    ViewInteraction clearEditText4 = onView(
      allOf(withId(R.id.edit_text_name), withText("하세"),
        childAtPosition(
          childAtPosition(
            withId(R.id.linear_layout_identify),
            1),
          0),
        isDisplayed()));
    clearEditText4.perform(click());

  }

  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));
      }
    };
  }
}
