package com.paypal.android.sdk.onetouch.core.config;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.braintreepayments.api.internal.AppHelper;
import com.paypal.android.sdk.onetouch.core.enums.Protocol;
import com.paypal.android.sdk.onetouch.core.enums.RequestTarget;
import com.paypal.android.sdk.onetouch.core.sdk.AppSwitchHelper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;

public abstract class Recipe<T extends Recipe<T>> {

    private List<String> mTargetPackagesInReversePriorityOrder;
    private RequestTarget mTarget;
    private Protocol mProtocol;
    private String mTargetComponent;
    private String mTargetIntentAction;
    private Collection<String> mSupportedLocales;

    public Recipe() {
        mTargetPackagesInReversePriorityOrder = new ArrayList<>();
        mSupportedLocales = new HashSet<>();
    }

    public T target(RequestTarget target) {
        mTarget = target;
        return getThis();
    }

    public T protocol(String protocol) {
        mProtocol = Protocol.getProtocol(protocol);
        return getThis();
    }

    public T targetPackage(String singleTargetPackage) {
        mTargetPackagesInReversePriorityOrder.add(singleTargetPackage);
        return getThis();
    }

    public List<String> getTargetPackagesInReversePriorityOrder() {
        return new ArrayList<>(mTargetPackagesInReversePriorityOrder);
    }

    public T supportedLocale(String supportedLocale) {
        mSupportedLocales.add(supportedLocale);
        return getThis();
    }

    public T targetComponent(String targetComponent) {
        mTargetComponent = targetComponent;
        return getThis();
    }

    public T targetIntentAction(String targetIntentAction) {
        mTargetIntentAction = targetIntentAction;
        return getThis();
    }

    public String getTargetComponent() {
        return mTargetComponent;
    }

    public String getTargetIntentAction() {
        return mTargetIntentAction;
    }

    public RequestTarget getTarget() {
        return mTarget;
    }

    protected abstract T getThis();

    public boolean isValidAppTarget(Context context, boolean isSecurityEnabled) {
        for (String allowedWalletTarget : getTargetPackagesInReversePriorityOrder()) {
            boolean isIntentAvailable = AppHelper.isIntentAvailable(context,
                    AppSwitchHelper.createBaseIntent(getTargetIntentAction(), getTargetComponent(),
                            allowedWalletTarget));

            String locale = Locale.getDefault().toString();
            // if no locales are specified, then presumed to be allowed for all
            boolean isLocaleAllowed =
                    mSupportedLocales.isEmpty() || mSupportedLocales.contains(locale);

            boolean isSignatureValid =
                    AppSwitchHelper.isSignatureValid(context, allowedWalletTarget, isSecurityEnabled);

            if (isIntentAvailable && isLocaleAllowed && isSignatureValid) {
                return true;
            }
        }

        return false;
    }

    public boolean isValidBrowserTarget(Context context, String browserSwitchUrl) {
        for (String allowedBrowserPackage : getTargetPackagesInReversePriorityOrder()) {
            boolean canBeResolved =
                    isValidBrowserTarget(context, browserSwitchUrl, allowedBrowserPackage);
            if (canBeResolved) {
                return true;
            }
        }

        return false;
    }

    public boolean isValidBrowserTarget(Context context, String browserSwitchUrl,
            String allowedBrowserPackage) {
        Intent intent = getBrowserIntent(browserSwitchUrl, allowedBrowserPackage);
        return (intent.resolveActivity(context.getPackageManager()) != null);
    }

    public static Intent getBrowserIntent(String browserSwitchUrl, String allowedBrowserPackage) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(browserSwitchUrl));
        if ("*".equals(allowedBrowserPackage)) {
            // don't set package - any is allowed at this point
        } else {
            intent.setPackage(allowedBrowserPackage);
        }
        return intent;
    }

    public Protocol getProtocol() {
        return mProtocol;
    }

    @Override
    public String toString() {
        return "Recipe(target=" + mTarget + ", mProtocol=" + mProtocol + ", packages=" +
                mTargetPackagesInReversePriorityOrder +
                ", targetComponent=" + mTargetComponent + ", targetIntentAction=" +
                mTargetIntentAction + ", supportedLocales=" + mSupportedLocales + ")";
    }
}

@@ -4061,16 +4061,17 @@
     if (
+!
 %22*%22.equa
@@ -4103,89 +4103,8 @@
 ) %7B%0A
-            // don't set package - any is allowed at this point%0A        %7D else %7B%0A
     
@@ -4225,24 +4225,24 @@
 rotocol() %7B%0A
+
         retu
@@ -4264,375 +4264,7 @@
    %7D
-%0A%0A    @Override%0A    public String toString() %7B%0A        return %22Recipe(target=%22 + mTarget + %22, mProtocol=%22 + mProtocol + %22, packages=%22 +%0A                mTargetPackagesInReversePriorityOrder +%0A                %22, targetComponent=%22 + mTargetComponent + %22, targetIntentAction=%22 +%0A                mTargetIntentAction + %22, supportedLocales=%22 + mSupportedLocales + %22)%22;%0A    %7D
 %0A%7D%0A

Clean up Recipe