package com.braintreepayments.api;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.Nullable;

import org.json.JSONObject;

/**
 * Information pertaining to the regulatory environment for a credit card if authentication insight
 * is requested during tokenization.
 */
public class AuthenticationInsight implements Parcelable {
    private static final String GRAPHQL_REGULATION_ENVIRONMENT_KEY = "customerAuthenticationRegulationEnvironment";
    private static final String REST_REGULATION_ENVIRONMENT_KEY = "regulationEnvironment";

    private String regulationEnvironment;

    static AuthenticationInsight fromJson(JSONObject json) {
        if (json == null) {
            return null;
        }

        AuthenticationInsight authInsight = new AuthenticationInsight();
        String regulationEnv;

        if (json.has(GRAPHQL_REGULATION_ENVIRONMENT_KEY)) {
            regulationEnv = Json.optString(json, GRAPHQL_REGULATION_ENVIRONMENT_KEY, null);
        } else {
            regulationEnv = Json.optString(json, REST_REGULATION_ENVIRONMENT_KEY, null);
        }

        if ("psdtwo".equalsIgnoreCase(regulationEnv)) {
            regulationEnv = "psd2";
        }

        if (regulationEnv != null ) {
            regulationEnv = regulationEnv.toLowerCase();
        }

        authInsight.regulationEnvironment = regulationEnv;

        return authInsight;
    }

    /**
     *
     * @return The regulation environment for the associated nonce to help determine the need
     * for 3D Secure.
     *
     * @see <a href="https://developers.braintreepayments.com/guides/3d-secure/advanced-options/android#authentication-insight">Documentation</a>
     * for possible values.
     */
    @Nullable
    public String getRegulationEnvironment() {
        return regulationEnvironment;
    }

    public AuthenticationInsight() {}

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(regulationEnvironment);
    }

    private AuthenticationInsight(Parcel in) {
        regulationEnvironment = in.readString();
    }

    public static final Creator<AuthenticationInsight> CREATOR = new Creator<AuthenticationInsight>() {
        public AuthenticationInsight createFromParcel(Parcel source) {
            return new AuthenticationInsight(source);
        }

        public AuthenticationInsight[] newArray(int size) {
            return new AuthenticationInsight[size];
        }
    };
}

@@ -82,24 +82,60 @@
 arcelable;%0A%0A
+import androidx.annotation.NonNull;%0A
 import andro
@@ -613,16 +613,22 @@
 private 
+final 
 String r
@@ -779,81 +779,8 @@
  %7D%0A%0A
-        AuthenticationInsight authInsight = new AuthenticationInsight();%0A
     
@@ -797,33 +797,32 @@
  regulationEnv;%0A
-%0A
         if (json
@@ -942,36 +942,34 @@
 NVIRONMENT_KEY, 
-null
+%22%22
 );%0A        %7D els
@@ -1054,20 +1054,18 @@
 NT_KEY, 
-null
+%22%22
 );%0A     
@@ -1168,33 +1168,32 @@
 ;%0A        %7D%0A
-%0A
         
 if (regulati
@@ -1180,20 +1180,16 @@
         
-if (
 regulati
@@ -1198,173 +1198,219 @@
 Env 
-!
 = 
-null ) %7B%0A            regulationEnv = regulationEnv.toLowerCase(
+regulationEnv.toLowerCase();%0A%0A        return new AuthenticationInsight(regulationEnv
 );%0A    
- 
-   
 %7D%0A%0A    
-    authInsight.regulationEnvironment = regulationEnv;%0A%0A        return authInsigh
+AuthenticationInsight(String regulationEnvironment) %7B%0A        this.regulationEnvironment = regulationEnvironmen
 t;%0A 
@@ -1741,23 +1741,22 @@
 /%0A    @N
+onN
 ull
-able
 %0A    pub
@@ -1844,47 +1844,8 @@
  %7D%0A%0A
-    public AuthenticationInsight() %7B%7D%0A%0A
     

Update AuthenticationInsight to allow regulation environment property to be @NonNull.