package com.cloudmine.api.rest;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.cloudmine.api.LibrarySpecificClassCreator;
import com.cloudmine.api.Strings;
import com.cloudmine.api.rest.options.CMServerFunction;
import com.cloudmine.api.rest.response.CMResponse;
import me.cloudmine.annotations.Expand;
import me.cloudmine.annotations.Optional;

import java.util.Map;

/**
 *
 */
public class BaseChangeUserIdentifierRequest extends CloudMineRequest<CMResponse> {

    public static final int REQUEST_TYPE = 412;

    @Expand(isStatic = true)
    public static CloudMineRequest changeUserEmail(String userIdentifier, String password, String newEmail, @Optional CMServerFunction serverFunction, @Optional Response.Listener<CMResponse> successListener, @Optional Response.ErrorListener errorListener){
        return new BaseChangeUserIdentifierRequest(userIdentifier, password, newEmail, null, serverFunction, successListener, errorListener);
    }

    @Expand(isStatic = true)
    public static CloudMineRequest changeUserName(String userIdentifier, String password, String newUserName, @Optional CMServerFunction serverFunction, @Optional Response.Listener<CMResponse> successListener, @Optional Response.ErrorListener errorListener){
        return new BaseChangeUserIdentifierRequest(userIdentifier, password, null, newUserName, serverFunction, successListener, errorListener);
    }

    private static String getJsonBody(String newEmail, String newUserName) {
        StringBuilder jsonBuilder = new StringBuilder("{");
        String separator = "";
        if(Strings.isNotEmpty(newEmail)) {
            jsonBuilder.append("\"email\": \"").append(newEmail).append("\"");
            separator = ", ";
        }
        if(Strings.isNotEmpty(newUserName)) {
            jsonBuilder.append(separator).append("\"username\": \"").append(newUserName).append("\"");
        }
        return jsonBuilder.append("}").toString();
    }

    private final String userIdentifier;
    private final String password;

    @Expand
    public BaseChangeUserIdentifierRequest(String userIdentifier, String password, String newEmail, String newUserName, @Optional CMServerFunction serverFunction, @Optional Response.Listener<CMResponse> successListener, @Optional Response.ErrorListener errorListener) {
        super(Method.POST, "/account/credentials", getJsonBody(newEmail, newUserName), null, serverFunction, successListener, errorListener);
        this.userIdentifier = userIdentifier;
        this.password = password;
    }

    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = super.getHeaders();
        headers.put("Authorization", "Basic " + LibrarySpecificClassCreator.getCreator().getEncoder().encode(userIdentifier + ":" + password));
        return headers;
    }

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

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

@@ -460,146 +460,689 @@
 *%0A *
-%0A */%0Apublic class BaseChangeUserIdentifierRequest extends CloudMineRequest%3CCMResponse%3E %7B%0A%0A    public static final int REQUEST_TYPE = 412;%0A
+ A Request for changing a user's identifier - their e-mail or username that they use to%0A * log in. Changing a user identifier invalidates all old sessions.%0A * %3Cbr%3ECopyright CloudMine LLC. All rights reserved%0A * %3Cbr%3E See LICENSE file included with SDK for details.%0A */%0Apublic class BaseChangeUserIdentifierRequest extends CloudMineRequest%3CCMResponse%3E %7B%0A%0A    public static final int REQUEST_TYPE = 412;%0A%0A    /**%0A     * Create a new BaseChangeUserIdentifierRequest for changing a user's e-mail address%0A     * @param currentEmail%0A     * @param password%0A     * @param newEmail%0A     * @param serverFunction%0A     * @param successListener%0A     * @param errorListener%0A     * @return%0A     */
 %0A   
@@ -1221,38 +1221,36 @@
 mail(String 
-userIdentifier
+currentEmail
 , String pas
@@ -1469,38 +1469,36 @@
 fierRequest(
-userIdentifier
+currentEmail
 , password, 
@@ -1565,24 +1565,297 @@
 er);%0A    %7D%0A%0A
+    /**%0A     * Create a new BaseChangeUserIdentifierRequest for changing a user's username%0A     * @param userName%0A     * @param password%0A     * @param newUserName%0A     * @param serverFunction%0A     * @param successListener%0A     * @param errorListener%0A     * @return%0A     */%0A
     @Expand(
@@ -1928,34 +1928,28 @@
 (String user
-Identifier
+Name
 , String pas
@@ -2179,26 +2179,20 @@
 est(user
-Identifier
+Name
 , passwo
@@ -2894,16 +2894,444 @@
 sword;%0A%0A
+    /**%0A     * Create a new BaseChangeUserIdentifierRequest for updating a user's email or username%0A     * @param userIdentifier the user's current username or e-mail%0A     * @param password the user's current password%0A     * @param newEmail the new e-mail address. May be null%0A     * @param newUserName the new username. May be null%0A     * @param serverFunction%0A     * @param successListener%0A     * @param errorListener%0A     */%0A
     @Exp

Comment ChangeUserIdentifierRequest