package com.braintreepayments.demo.test;

import android.widget.EditText;

import androidx.preference.PreferenceManager;
import androidx.test.runner.AndroidJUnit4;

import com.braintreepayments.demo.test.utilities.TestHelper;

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

import static androidx.test.InstrumentationRegistry.getTargetContext;
import static com.lukekorth.deviceautomator.AutomatorAction.click;
import static com.lukekorth.deviceautomator.AutomatorAction.setText;
import static com.lukekorth.deviceautomator.AutomatorAssertion.text;
import static com.lukekorth.deviceautomator.DeviceAutomator.onDevice;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withClass;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withText;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withTextStartingWith;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.StringEndsWith.endsWith;

@RunWith(AndroidJUnit4.class)
public class ThreeDSecureCardinalTest extends TestHelper {

    @Before
    public void setup() {
        super.setup();
        onDevice(withText("Credit or Debit Cards")).waitForEnabled().perform(click());

        PreferenceManager.getDefaultSharedPreferences(getTargetContext())
                .edit()
                .putBoolean("enable_three_d_secure", true)
                .commit();
    }

    @Test(timeout = 40000)
    public void threeDSecure_authenticates() {
        onDevice(withText("Card Number")).perform(setText("4000000000001091"));
        fillInExpiration("01", "2022");
        onDevice(withText("CVV")).perform(setText("123"));
        onDevice(withText("Postal Code")).perform(setText("12345"));
        onDevice(withText("Purchase")).perform(click());

        onDevice(withClass(EditText.class)).perform(setText("1234"));
        onDevice(withText("Submit")).perform(click());

        getNonceDetails().check(text(containsString("Card Last Two: 91")));
        getNonceDetails().check(text(containsString("isLiabilityShifted: true")));
        getNonceDetails().check(text(containsString("isLiabilityShiftPossible: true")));

        onDevice(withText("Create a Transaction")).perform(click());
        onDevice(withTextStartingWith("created")).check(text(endsWith("authorized")));
    }
}

@@ -156,16 +156,207 @@
 dJUnit4;
+%0Aimport androidx.test.uiautomator.UiDevice;%0Aimport androidx.test.uiautomator.UiObject;%0Aimport androidx.test.uiautomator.UiObjectNotFoundException;%0Aimport androidx.test.uiautomator.UiSelector;
 %0A%0Aimport
@@ -493,16 +493,117 @@
 nWith;%0A%0A
+import java.io.IOException;%0A%0Aimport static androidx.test.InstrumentationRegistry.getInstrumentation;%0A
 import s
@@ -1010,16 +1010,92 @@
 hClass;%0A
+import static com.lukekorth.deviceautomator.UiObjectMatcher.withResourceId;%0A
 import s
@@ -1849,16 +1849,49 @@
 icates()
+ throws UiObjectNotFoundException
  %7B%0A     
@@ -2203,53 +2203,125 @@
     
-onDevice(withClass(EditText.class)).perform(s
+UiObject codeEditText = getCodeEditTextView();%0A        codeEditText.waitForExists(5000);%0A        codeEditText.legacyS
 etTe
@@ -2330,19 +2330,19 @@
 (%221234%22)
-)
 ;%0A
+%0A
         
@@ -2703,24 +2703,24 @@
 m(click());%0A
-
         onDe
@@ -2799,11 +2799,308 @@
 );%0A    %7D
+%0A%0A    private static UiObject getCodeEditTextView() %7B%0A        UiDevice device = UiDevice.getInstance(getInstrumentation());%0A        String codeEditTextResId = %22com.braintreepayments.demo:id/codeEditTextField%22;%0A        return device.findObject(new UiSelector().resourceId(codeEditTextResId));%0A    %7D
 %0A%7D%0A

Fix 3D Secure Cardinal Test.