/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.firebase.ui.auth.ui;

import android.content.Intent;
import android.support.annotation.NonNull;

import com.firebase.ui.auth.R;
import com.firebase.ui.auth.ui.account_link.WelcomeBackIdpPrompt;
import com.firebase.ui.auth.ui.email.RegisterEmailActivity;
import com.firebase.ui.auth.ui.email.SignInActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.EmailAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.ProviderQueryResult;

import java.util.Arrays;
import java.util.List;

public class AcquireEmailHelper {
    private static final String TAG = "AcquireEmailHelper";
    public static final int RC_REGISTER_ACCOUNT = 14;
    public static final int RC_WELCOME_BACK_IDP = 15;
    public static final int RC_SIGN_IN = 16;
    public static final List<Integer> REQUEST_CODES = Arrays.asList(
            RC_REGISTER_ACCOUNT,
            RC_WELCOME_BACK_IDP,
            RC_SIGN_IN
    );

    private ActivityHelper mActivityHelper;

    public AcquireEmailHelper(ActivityHelper activityHelper) {
        mActivityHelper = activityHelper;
    }

    public void checkAccountExists(final String email) {
        FirebaseAuth firebaseAuth = mActivityHelper.getFirebaseAuth();
        mActivityHelper.showLoadingDialog(R.string.progress_dialog_loading);
        if (email != null && !email.isEmpty()) {
            firebaseAuth
                    .fetchProvidersForEmail(email)
                    .addOnFailureListener(
                            new TaskFailureLogger(TAG, "Error fetching providers for email"))
                    .addOnCompleteListener(
                            new OnCompleteListener<ProviderQueryResult>() {
                                @Override
                                public void onComplete(@NonNull Task<ProviderQueryResult> task) {
                                    if (task.isSuccessful()) {
                                        startEmailHandler(email, task.getResult().getProviders());
                                    } else {
                                        mActivityHelper.dismissDialog();
                                    }
                                }
                            });
        }
    }

    private void startEmailHandler(String email, List<String> providers) {
        mActivityHelper.dismissDialog();
        if (providers == null || providers.isEmpty()) {
            // account doesn't exist yet
            Intent registerIntent = RegisterEmailActivity.createIntent(
                    mActivityHelper.getApplicationContext(),
                    mActivityHelper.getFlowParams(),
                    email);
            mActivityHelper.startActivityForResult(registerIntent, RC_REGISTER_ACCOUNT);
        } else {
            // account does exist
            for (String provider : providers) {
                if (provider.equalsIgnoreCase(EmailAuthProvider.PROVIDER_ID)) {
                    Intent signInIntent = SignInActivity.createIntent(
                            mActivityHelper.getApplicationContext(),
                            mActivityHelper.getFlowParams(),
                            email);
                    mActivityHelper.startActivityForResult(signInIntent, RC_SIGN_IN);
                    return;
                }

                Intent intent = WelcomeBackIdpPrompt.createIntent(
                        mActivityHelper.getApplicationContext(),
                        mActivityHelper.getFlowParams(),
                        provider,
                        null,
                        email);
                mActivityHelper.startActivityForResult(intent, RC_WELCOME_BACK_IDP);
                return;
            }

            Intent signInIntent = new Intent(
                    mActivityHelper.getApplicationContext(), SignInActivity.class);
            signInIntent.putExtra(ExtraConstants.EXTRA_EMAIL, email);
            mActivityHelper.startActivityForResult(signInIntent, RC_SIGN_IN);
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (REQUEST_CODES.contains(requestCode)) {
            mActivityHelper.finish(resultCode, data);
        }
    }
}

@@ -709,16 +709,41 @@
 nNull;%0A%0A
+import android.util.Log;%0A
 import c
@@ -3523,13 +3523,8 @@
     
-for (
 Stri
@@ -3535,17 +3535,17 @@
 rovider 
-:
+=
  provide
@@ -3542,32 +3542,33 @@
  = providers
-) %7B%0A    
+.get(0);%0A
             
@@ -3639,36 +3639,32 @@
                 
-    
 Intent signInInt
@@ -3714,36 +3714,32 @@
                 
- 
-   
 mActivityHelper.
@@ -3755,36 +3755,32 @@
 ationContext(),%0A
-    
                 
@@ -3836,36 +3836,32 @@
                 
-    
 email);%0A        
@@ -3860,36 +3860,32 @@
                 
-  
-  
 mActivityHelper.
@@ -3950,42 +3950,16 @@
     
-        return;%0A                %7D%0A
+%7D else %7B
 %0A   
@@ -4034,36 +4034,32 @@
                 
-    
 mActivityHelper.
@@ -4095,36 +4095,32 @@
                 
-  
-  
 mActivityHelper.
@@ -4128,36 +4128,32 @@
 etFlowParams(),%0A
-    
                 
@@ -4190,22 +4190,14 @@
     
-    
 null,%0A
-    
     
@@ -4321,312 +4321,9 @@
     
-    return;%0A            %7D%0A%0A            Intent signInIntent = new Intent(%0A                    mActivityHelper.getApplicationContext(), SignInActivity.class);%0A            signInIntent.putExtra(ExtraConstants.EXTRA_EMAIL, email);%0A            mActivityHelper.startActivityForResult(signInIntent, RC_SIGN_IN);
+%7D
 %0A   

Clean up the logic around starting the linking flow (#377)