package com.proxerme.library.connection.messenger.request;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Size;

import com.proxerme.library.connection.ProxerResult;
import com.proxerme.library.connection.messenger.MessengerRequest;
import com.proxerme.library.connection.messenger.result.NewConferenceResult;
import com.squareup.moshi.Moshi;

import java.io.IOException;
import java.util.Collection;

import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Request to create a new Conference. This can be either a group chat or a chat with a single
 * user. (Use the {@link #NewConferenceRequest(String)} constructor for a single user and
 * either the {@link #NewConferenceRequest(String, String...)} or
 * {@link #NewConferenceRequest(String, Collection)} constructor for a group)
 * The user must be logged in to use this API.
 *
 * @author Ruben Gees
 */
public class NewConferenceRequest extends MessengerRequest<String> {

    private static final String ENDPOINT = "newconference";
    private static final String ENDPOINT_GROUP = "newconferencegroup";

    private String topic;
    private String[] users;
    private String user;

    @Nullable
    private String firstMessage;

    /**
     * The constructor. If this one is used, a non-group chat will be created.
     *
     * @param user The user.
     */
    public NewConferenceRequest(@NonNull String user) {
        this.user = user;
    }

    /**
     * The constructor. If this one is used, a group chat will be created.
     *
     * @param topic The topic of the new group chat.
     * @param users The participants.
     */
    public NewConferenceRequest(@NonNull String topic, @NonNull @Size(min = 1) String... users) {
        this.topic = topic;
        this.users = users;
    }

    /**
     * The constructor. If this one is used, a group chat will be created.
     *
     * @param topic The topic of the new group chat.
     * @param users The participants.
     */
    public NewConferenceRequest(@NonNull String topic,
                                @NonNull @Size(min = 1) Collection<String> users) {
        this.topic = topic;
        this.users = users.toArray(new String[users.size()]);
    }

    /**
     * Sets the optional first message.
     *
     * @param message The message.
     * @return This request.
     */
    public NewConferenceRequest withFirstMessage(@NonNull String message) {
        this.firstMessage = message;

        return this;
    }

    @Override
    protected ProxerResult<String> parse(@NonNull Moshi moshi, @NonNull ResponseBody body)
            throws IOException {
        return moshi.adapter(NewConferenceResult.class).fromJson(body.source());
    }

    @NonNull
    @Override
    protected String getApiEndpoint() {
        if (users != null) {
            return ENDPOINT_GROUP;
        } else {
            return ENDPOINT;
        }
    }

    @Override
    protected String getMethod() {
        return POST;
    }

    @Nullable
    @Override
    protected RequestBody getRequestBody() {
        FormBody.Builder builder = new FormBody.Builder()
                .add("topic", topic);

        if (users != null) {
            for (String user : users) {
                builder.add("users[]", user);
            }
        } else {
            builder.add("username", user);
        }

        if (firstMessage != null) {
            builder.add("text", firstMessage);
        }

        return builder.build();
    }
}

@@ -1164,16 +1164,259 @@
 roup%22;%0A%0A
+    private static final String TOPIC_PARAMETER = %22topic%22;%0A    private static final String USERS_PARAMETER = %22users%5B%5D%22;%0A    private static final String USERNAME_PARAMETER = %22username%22;%0A    private static final String TEXT_PARAMETER = %22text%22;%0A%0A
     priv
@@ -3448,23 +3448,31 @@
    .add(
-%22topic%22
+TOPIC_PARAMETER
 , topic)
@@ -3571,25 +3571,31 @@
 der.add(
-%22users%5B%5D%22
+USERS_PARAMETER
 , user);
@@ -3650,26 +3650,34 @@
 der.add(
-%22username%22
+USERNAME_PARAMETER
 , user);
@@ -3748,22 +3748,30 @@
 der.add(
-%22text%22
+TEXT_PARAMETER
 , firstM

Make parameter names in NewConferenceRequest constants