package com.cloudmine.api.rest;

import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.cloudmine.api.CMCreditCard;
import com.cloudmine.api.CMSessionToken;
import com.cloudmine.api.rest.options.CMServerFunction;
import com.cloudmine.api.rest.response.PaymentResponse;
import me.cloudmine.annotations.Expand;
import me.cloudmine.annotations.Optional;
import me.cloudmine.annotations.Single;

import java.util.Collection;

/**
 * <br>Copyright CloudMine LLC. All rights reserved
 * <br> See LICENSE file included with SDK for details.
 */
public class BaseAddPaymentMethodRequest extends CloudMineRequest<PaymentResponse> {
    public static final int REQUEST_TYPE = 415;

    private static final String URL = "/payments/account/methods/card";

    private static String toCreditCardArray(Collection<CMCreditCard> creditCards) {
        StringBuilder ccArray = new StringBuilder("{\"payments\":[");
        if(creditCards != null) {
            String separator = "";
            for(CMCreditCard card : creditCards) {
                ccArray.append(separator).append(card.getPaymentTransportRepresentation());
                separator = ", ";
            }
        }
        ccArray.append("]}");
        return ccArray.toString();
    }

    @Expand
    public BaseAddPaymentMethodRequest(@Single Collection<CMCreditCard> cards, CMSessionToken sessionToken, @Optional CMServerFunction serverFunction, Response.Listener<PaymentResponse> successListener, @Optional Response.ErrorListener errorListener) {
        super(Method.POST, URL, toCreditCardArray(cards), sessionToken, serverFunction, successListener, errorListener);
    }

    @Override
    protected Response parseNetworkResponse(NetworkResponse networkResponse) {
        return Response.success(new PaymentResponse(new String(networkResponse.data), networkResponse.statusCode), getCacheEntry(networkResponse));
    }

    @Override
    public int getRequestType() {
        return REQUEST_TYPE;
    }
}

@@ -461,112 +461,116 @@
 %0A * 
-%3Cbr%3ECopyright CloudMine LLC. All rights reserved%0A * %3Cbr%3E See LICENSE file included with SDK for details.
+A Volley %7B@link com.android.volley.Request%7D for adding a payment method for a user. Requires a session token
 %0A */
@@ -1269,24 +1269,552 @@
 g();%0A    %7D%0A%0A
+%0A    /**%0A     * Add all of the specified %7B@link com.cloudmine.api.CMCreditCard%7D to the user associated with the given %7B@link com.cloudmine.api.CMSessionToken%7D%0A     * @param cards A collection of the cards to add%0A     * @param sessionToken a valid session token. The credit cards will be added to the user whose session token this is%0A     * @param serverFunction A nullable server function to call after adding the credit cards to the user%0A     * @param successListener callback called if the call succeeds on the server%0A     */%0A
     @Expand%0A

Document add payment method