/* ====================================================================
 * FTD4Linux Software License, Version 1.0
 *
 * Copyright (c) 2000-2004 The FTD4Linux Development Team.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        FTD4Linux Development Team (http://www.ftd4linux.2y.net/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "FTD" and "FTD4Linux" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact alguno@ftd4linux.2y.net.
 *
 * 5. Products derived from this software may not be called "FTD4Linux",
 *    nor may "FTD4Linux" appear in their name, without prior written
 *    permission of the FTD4Linux Development Team.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE FTD4LINUX DEVELOPMENT TEAM OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "ftd4linux.h"
#include "ftd_app_core.h"

/**
 * Registreert een nieuw account op de FTD servers.
 *
 * @arg     username        De gebruikersnaam voor het nieuwe account.
 * @arg     password        Het wachtwoord voor het nieuwe account.
 * @arg     email           Het email-adres voor het nieuwe account.
 * @arg     errmsg          Een eventueel foutbericht van de FTD servers komt hier in te staan.
 *
 * @return  Een fout-code die aangeeft of het succesvol was.
 */
FtdErrorCode
ftd_signup_register(const char *username, const char *password, const char *email, char **errmsg)
{
    FtdList    *params = NULL;
    int retval;
    FtdString *uitvoer;

    params = ftd_list_append(params, ftd_http_parameter_new("mode", "signup"));
    params = ftd_list_append(params, ftd_http_parameter_new("u", (char *) username));
    params = ftd_list_append(params, ftd_http_parameter_new("p", (char *) password));
    params = ftd_list_append(params, ftd_http_parameter_new("e", (char *) email));
    /* FIXME: er wordt ook nog een veld 'pub' verwacht, maar wat moet
     * hier in komen te staan ? Het betreffende veld in de database is een text veld
     * contact opnemen met FTD voor meer details hierover...
    params = ftd_list_append(params, ftd_http_parameter_new("pub", "")); */

    retval = ftd_http_curl_execute(FTD_URL_SIGNUP, params, TRUE, &uitvoer, FALSE, NULL, TRUE);
    ftd_http_parameter_free_all(params);

    *errmsg = NULL;

    if (retval == ERROR_MSG) {
        *errmsg = g_strdup(ftd_string_value(uitvoer));
    }

    if (strncmp(ftd_string_value(uitvoer), "OK", 2) == 0) {
        ftd_string_free( uitvoer );
        return ERROR_NONE;
    }

    if (strncmp(ftd_string_value(uitvoer), "E0", 2) == 0) {
        ftd_string_free( uitvoer );
        return ERROR_SIGNUP_E0;
    }

    if (strncmp(ftd_string_value(uitvoer), "E1", 2) == 0) {
        ftd_string_free( uitvoer );
        return ERROR_SIGNUP_E1;
    }

    if (strncmp(ftd_string_value(uitvoer), "E2", 2) == 0) {
        ftd_string_free( uitvoer );
        return ERROR_SIGNUP_E2;
    }

    if (strncmp(ftd_string_value(uitvoer), "E3", 2) == 0) {
        ftd_string_free( uitvoer );
        return ERROR_SIGNUP_E3;
    }

    *errmsg = g_strdup_printf("Onjuiste response van server :\n%s", ftd_string_value(uitvoer));

    ftd_string_free( uitvoer );

    return ERROR_PARSE;
}
