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

static FtdProfile *ftd_profile_parse(FtdString *buffer);

/**
 * Haalt de profiel informatie op van de FTD server
 *
 * @arg     username            De gebruikersnaam van wie we meer informatie willen weten
 * @arg     profile             De profiel-informatie
 * @arg     errmsg              Een eventueel foutbericht van de server wordt hier in geplaatst.
 *
 * @return  Een fout-code die aangeeft of het downloaden succesvol was verlopen
 */
FtdErrorCode
ftd_profile_get(const char *username, FtdProfile **profile, char **errmsg)
{
    FtdList         *params = NULL;
    FtdString       *uitvoer;
    FtdErrorCode     retval;
    FtdList         *list, *list_total;
    FtdEvent        *event;
    FTDEventlist    *eventlist;

    g_assert(username);
    g_assert(profile);
    g_assert(errmsg);

    params = ftd_list_append(params, ftd_http_parameter_new("user", username));
    retval = ftd_http_curl_execute(FTD_URL_USER, params, TRUE, &uitvoer, TRUE, NULL, FALSE);
    ftd_http_parameter_free_all(params);

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

        ftd_string_free(uitvoer);

        return retval;
    }

    *profile = ftd_profile_parse(uitvoer);

    ftd_string_free(uitvoer);

    *errmsg = NULL;

    if (!*profile) {
        return ERROR_NO_SUCH_USER_KNOWN;
    }

    return ERROR_NONE;
}

void
ftd_profile_free(FtdProfile *profile)
{
    if (profile->username) {
        g_free(profile->username);
    }

    if (profile->email) {
        g_free(profile->email);
    }

    if (profile->info) {
        g_free(profile->info);
    }

    if (profile->avatar) {
        g_free(profile->avatar);
    }

    if (profile->site) {
        g_free(profile->site);
    }

    if (profile->im) {
        g_free(profile->im);
    }

    if (profile->isp) {
        g_free(profile->isp);
    }

    if (profile->ns) {
        g_free(profile->ns);
    }

    if (profile->hobby) {
        g_free(profile->hobby);
    }

    g_free(profile);
}

static FtdProfile *
ftd_profile_parse(FtdString *buffer)
{
    xmlDocPtr        doc;
    xmlNodePtr       cur;
    FtdProfile      *profile;

    g_assert(core);
    g_assert(buffer);

    doc = xmlParseMemory(ftd_string_value(buffer), ftd_string_length(buffer));
    cur = xmlDocGetRootElement(doc);
    if (cur == NULL) {
        fprintf(stderr,"ftd_profile_parse(): empty document!\n");
        xmlFreeDoc(doc);
        return NULL;
    }

    if (strcasecmp(cur->name, "UserInfo")) {
        fprintf(stderr,"ftd_profile_parse(): document of the wrong type, root node != UserInfo");
        xmlFreeDoc(doc);
        return NULL;
    }

    cur = cur->xmlChildrenNode;
    if (!cur) {
        // Een warning is niet nodig hier
        xmlFreeDoc(doc);
        return NULL;
    }

    profile = g_malloc0(sizeof(FtdProfile));

    while (cur) {
        if (!strcasecmp(cur->name, "Name")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);

            // Dit is eigenlijk heel smerig, maar indien de gebruikersnaam onbekend
            // is, dan zal de Name tag leeg zijn.
            if (!tmp) {
                g_free(profile);
                xmlFreeDoc(doc);
                return NULL;
            }

            profile->username = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "Stamp")) {
            profile->stamp = ftd_xml_parse_int(doc, cur);
        }

        if (!strcasecmp(cur->name, "Posts")) {
            profile->numPosts = ftd_xml_parse_int(doc, cur);
        }

        if (!strcasecmp(cur->name, "Spots")) {
            profile->numSpots = ftd_xml_parse_int(doc, cur);
        }

        if (!strcasecmp(cur->name, "Kudos")) {
            profile->kudos = ftd_xml_parse_int(doc, cur);
        }

        if (!strcasecmp(cur->name, "Given")) {
            profile->given = ftd_xml_parse_int(doc, cur);
        }

        if (!strcasecmp(cur->name, "Received")) {
            profile->received = ftd_xml_parse_int(doc, cur);
        }

        if (!strcasecmp(cur->name, "Rank")) {
            profile->rank = ftd_xml_parse_int(doc, cur);
        }

        if (!strcasecmp(cur->name, "EMail")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);
            profile->email = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "Info")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);
            profile->info = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "Avatar")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);
            profile->avatar = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "Status")) {
            profile->status = ftd_xml_parse_int(doc, cur);
        }

        if (!strcasecmp(cur->name, "Site")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);
            profile->site = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "IM")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);
            profile->im = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "ISP")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);
            profile->isp = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "NS")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);
            profile->ns = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "Hobby")) {
            FtdString *tmp = ftd_xml_parse_string(doc, cur);
            profile->hobby = g_strdup(ftd_string_value(tmp));
            ftd_string_free(tmp);
        }

        if (!strcasecmp(cur->name, "PStat")) {
            profile->pstat = ftd_xml_parse_int(doc, cur);
        }

        cur = cur->next;
    }
    xmlFreeDoc(doc);

    return profile;
}
