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

import android.content.Context;
import android.os.Parcel;

import com.paypal.android.sdk.onetouch.core.config.BillingAgreementRecipe;
import com.paypal.android.sdk.onetouch.core.config.OtcConfiguration;
import com.paypal.android.sdk.onetouch.core.config.Recipe;
import com.paypal.android.sdk.onetouch.core.enums.RequestTarget;

public class BillingAgreementRequest extends CheckoutRequest {
    private static final String TAG = BillingAgreementRequest.class.getSimpleName();

    public BillingAgreementRequest() {
        // default constructor needed in addition to parcelable constructor
    }

    public BillingAgreementRequest pairingId(String pairingId) {
        super.pairingId(pairingId);
        return this;
    }

    public BillingAgreementRequest approvalURL(String approvalURL) {
        super.approvalURL(approvalURL);
        return this;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(getClientMetadataId());
        dest.writeString(getClientId());
        dest.writeString(getEnvironment());

        dest.writeString(mApprovalUrl);
        dest.writeString(mTokenQueryParamKey);
    }

    private BillingAgreementRequest(Parcel source) {
        clientMetadataId(source.readString());
        clientId(source.readString());
        environment(source.readString());

        mApprovalUrl = source.readString();
        mTokenQueryParamKey = source.readString();
    }

    /**
     * required by {@link android.os.Parcelable}
     */
    public static final Creator<BillingAgreementRequest> CREATOR =
            new Creator<BillingAgreementRequest>() {

                @Override
                public BillingAgreementRequest[] newArray(int size) {
                    return new BillingAgreementRequest[size];
                }

                @Override
                public BillingAgreementRequest createFromParcel(Parcel source) {
                    return new BillingAgreementRequest(source);
                }
            };

    @Override
    protected BillingAgreementRequest getThis() {
        return this;
    }

    @Override
    public Recipe getRecipeToExecute(Context context, OtcConfiguration config,
            boolean isSecurityEnabled) {
        for (BillingAgreementRecipe recipe : config.getBillingAgreementRecipes()) {
            if (RequestTarget.wallet == recipe.getTarget()) {
                if (recipe.isValidAppTarget(context, isSecurityEnabled)) {
                    return recipe;
                }
            } else if (RequestTarget.browser == recipe.getTarget()) {
                String browserSwitchUrl = getBrowserSwitchUrl(context, config);

                if (recipe.isValidBrowserTarget(context, browserSwitchUrl)) {
                    return recipe;
                }
            }
        }
        return null;
    }
}

@@ -434,215 +434,8 @@
 st %7B
-%0A    private static final String TAG = BillingAgreementRequest.class.getSimpleName();%0A%0A    public BillingAgreementRequest() %7B%0A        // default constructor needed in addition to parcelable constructor%0A    %7D
 %0A%0A  
@@ -690,32 +690,907 @@
 rn this;%0A    %7D%0A%0A
+    @Override%0A    protected BillingAgreementRequest getThis() %7B%0A        return this;%0A    %7D%0A%0A    @Override%0A    public Recipe getRecipeToExecute(Context context, OtcConfiguration config,%0A            boolean isSecurityEnabled) %7B%0A        for (BillingAgreementRecipe recipe : config.getBillingAgreementRecipes()) %7B%0A            if (RequestTarget.wallet == recipe.getTarget()) %7B%0A                if (recipe.isValidAppTarget(context, isSecurityEnabled)) %7B%0A                    return recipe;%0A                %7D%0A            %7D else if (RequestTarget.browser == recipe.getTarget()) %7B%0A                String browserSwitchUrl = getBrowserSwitchUrl(context, config);%0A%0A                if (recipe.isValidBrowserTarget(context, browserSwitchUrl)) %7B%0A                    return recipe;%0A                %7D%0A            %7D%0A        %7D%0A        return null;%0A    %7D%0A%0A    public BillingAgreementRequest() %7B%7D%0A%0A
     @Override%0A  
@@ -2160,73 +2160,8 @@
  %7D%0A%0A
-    /**%0A     * required by %7B@link android.os.Parcelable%7D%0A     */%0A
     
@@ -2635,32 +2635,32 @@
               %7D%0A
+
             %7D;%0A%0A
@@ -2661,841 +2661,7 @@
   %7D;
-%0A%0A    @Override%0A    protected BillingAgreementRequest getThis() %7B%0A        return this;%0A    %7D%0A%0A    @Override%0A    public Recipe getRecipeToExecute(Context context, OtcConfiguration config,%0A            boolean isSecurityEnabled) %7B%0A        for (BillingAgreementRecipe recipe : config.getBillingAgreementRecipes()) %7B%0A            if (RequestTarget.wallet == recipe.getTarget()) %7B%0A                if (recipe.isValidAppTarget(context, isSecurityEnabled)) %7B%0A                    return recipe;%0A                %7D%0A            %7D else if (RequestTarget.browser == recipe.getTarget()) %7B%0A                String browserSwitchUrl = getBrowserSwitchUrl(context, config);%0A%0A                if (recipe.isValidBrowserTarget(context, browserSwitchUrl)) %7B%0A                    return recipe;%0A                %7D%0A            %7D%0A        %7D%0A        return null;%0A    %7D
 %0A%7D%0A

Reorder parcelable code