/* ====================================================================
 * 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 <glib.h>
#include "ftd4linux.h"
#include "ftd_app_core.h"

/**
 * Stuurt een bericht naar de FTD Modjes
 *
 * @arg     post_id      Het nummer van de FTD posting waarop een modje ingeschakeld dient te worden
 * @arg     forum_id     Het nummer van een reactie binnen een FTD posting waarop een modje ingeschakeld dient te worden (of 0 indien het de FTD posting zelf betreft)
 * @arg     msg          Het bericht dat naar de modjes gestuurd moet worden
 * @arg     count        Pointer waarin opgeslagen wordt hoeveel meldingen er reeds gedaan zijn van de betreffende posting
 * @arg     errmsg       Een eventueel foutbericht van de FTD servers wordt hier in gezet.
 *
 *
 * @return  Een fout-code die aangeeft of het melden succesvol was.
 */
FtdErrorCode
ftd_modje_send(int post_id, int forum_id, const char *msg, int *count, char **errmsg)
{
    FtdErrorCode retval;
    FtdString *uitvoer;
    FtdList *params = NULL;
    char tmp[16];

    g_assert(core);
    g_assert(post_id > 0);

    snprintf(tmp, sizeof(tmp), "%i", post_id);
    params = ftd_list_append(params, ftd_http_parameter_new("id", tmp));
    snprintf(tmp, sizeof(tmp), "%i", forum_id);
    params = ftd_list_append(params, ftd_http_parameter_new("forumid", tmp));

    params = ftd_list_append(params, ftd_http_parameter_new("reason", msg));

    retval = ftd_http_curl_execute(FTD_URL_MODJE, params, TRUE, &uitvoer, TRUE, NULL, TRUE);

    if (retval != ERROR_NONE) {
        if (retval == ERROR_MSG) {
            *errmsg = g_strdup(ftd_string_value(uitvoer));
        } else {
            *errmsg = NULL;
        }

        ftd_string_free(uitvoer);

        return retval;
    }

    if (!strcmp(ftd_string_value(uitvoer), "FAIL")) {
        ftd_string_free(uitvoer);
        return ERROR_DB;
    }

    if (!strncmp(ftd_string_value(uitvoer), "OK", 2)) {
        char *tmp = ftd_string_value(uitvoer) + 2;
        if (count) {
            *count = atoi(tmp);
        }

        ftd_string_free(uitvoer);

        return ERROR_NONE;
    }

    if (!strcmp(ftd_string_value(uitvoer), "DUPE")) {
        ftd_string_free(uitvoer);

        if (forum_id > 0) {
            return ERROR_MODJE_DUPE_FORUM;
        } else {
            return ERROR_MODJE_DUPE_POST;
        }
    }

    ftd_string_free(uitvoer);

    return ERROR_RES;
}
