package com.braintreepayments.demo.test;

import android.widget.EditText;

import androidx.preference.PreferenceManager;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

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

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

import java.io.IOException;

import static androidx.test.InstrumentationRegistry.getInstrumentation;
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.withResourceId;
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() throws UiObjectNotFoundException {
        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());

        UiObject codeEditText = getCodeEditTextView();
        codeEditText.waitForExists(5000);
        codeEditText.legacySetText("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")));
    }

    private static UiObject getCodeEditTextView() {
        UiDevice device = UiDevice.getInstance(getInstrumentation());
        String codeEditTextResId = "com.braintreepayments.demo:id/codeEditTextField";
        return device.findObject(new UiSelector().resourceId(codeEditTextResId));
    }
}

@@ -2199,24 +2199,22 @@
         
-UiObject
+String
  codeEdi
@@ -2222,15 +2222,48 @@
 Text
- = getC
+ResId = %22com.braintreepayments.demo:id/c
 odeE
@@ -2269,22 +2269,22 @@
 EditText
-View()
+Field%22
 ;%0A      
@@ -2277,32 +2277,56 @@
 Field%22;%0A        
+onDevice(withResourceId(
 codeEditText.wai
@@ -2321,16 +2321,23 @@
 EditText
+ResId))
 .waitFor
@@ -2347,12 +2347,8 @@
 sts(
-5000
 );%0A 
@@ -2358,30 +2358,93 @@
     
-codeEditText.legacySet
+onDevice(withResourceId(codeEditTextResId)).perform(click());%0A        onDevice().type
 Text
@@ -2924,304 +2924,7 @@
    %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

Refactor ThreeDSecureCardinalTest.