package com.firebase.ui.auth.util;

import android.net.Uri;
import android.text.TextUtils;

import com.firebase.ui.auth.IdpResponse;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.firebase.auth.FirebaseUser;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;

/**
 * Utility class for working with {@link Credential} objects.
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class CredentialUtils {
    private CredentialUtils() {
        throw new AssertionError("No instance for you!");
    }

    /**
     * Build a credential for the specified {@link FirebaseUser} with optional password and {@link
     * IdpResponse}.
     * <p>
     * If the credential cannot be built (for example, empty email) then will return {@code null}.
     */
    @Nullable
    public static Credential buildCredential(@NonNull FirebaseUser user,
                                             @Nullable String password,
                                             @Nullable String accountType) {
        String email = user.getEmail();
        String phone = user.getPhoneNumber();
        Uri profilePictureUri =
                user.getPhotoUrl() == null ? null : Uri.parse(user.getPhotoUrl().toString());

        if (TextUtils.isEmpty(email) && TextUtils.isEmpty(phone)) { return null; }
        if (password == null && accountType == null) { return null; }

        Credential.Builder builder =
                new Credential.Builder(TextUtils.isEmpty(email) ? phone : email)
                        .setName(user.getDisplayName())
                        .setProfilePictureUri(profilePictureUri);

        if (TextUtils.isEmpty(password)) {
            builder.setAccountType(accountType);
        } else {
            builder.setPassword(password);
        }

        return builder.build();
    }

    /**
     * @see #buildCredential(FirebaseUser, String, String)
     */
    @NonNull
    public static Credential buildCredentialOrThrow(@NonNull FirebaseUser user,
                                                    @Nullable String password,
                                                    @Nullable String accountType) {
        Credential credential = buildCredential(user, password, accountType);
        if (credential == null) {
            throw new IllegalStateException("Unable to build credential");
        }
        return credential;
    }
}

@@ -83,16 +83,41 @@
 xtUtils;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -519,16 +519,75 @@
 Utils %7B%0A
+%0A    private static final String TAG = %22CredentialUtils%22;%0A%0A
     priv
@@ -1437,16 +1437,149 @@
 hone)) %7B
+%0A            Log.w(TAG, %22User (accountType=%22 + accountType + %22) has no email or phone number, cannot build credential.%22);%0A           
  return 
@@ -1583,16 +1583,24 @@
 rn null;
+%0A       
  %7D%0A     
@@ -1648,16 +1648,117 @@
  null) %7B
+%0A            Log.w(TAG, %22User has no accountType or password, cannot build credential.%22);%0A           
  return 
@@ -1762,16 +1762,24 @@
 rn null;
+%0A       
  %7D%0A%0A    

Add more logging when buildCredential fails (#1763)