/* ====================================================================
 * 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 <stdio.h>
#include <stdlib.h>
#include "ftd4linux.h"
#include "ftd_events.h"

/**
 * Voegt de basis-gegevens van een event in een XML boom in.
 *
 * @arg     event       Het event waarvan de gegevens weggeschreven moeten worden.
 * @arg     doc         Het XML document waar de gegevens in gezet moeten worden.
 *
 * @return  Een nieuwe XML node die het event omvat.
 */
xmlNodePtr
ftd_events_xml_add_basic(FtdEvent *event, xmlDocPtr doc)
{
    xmlNodePtr node, cdataNode, tmpNode;
    FtdString *str;

    g_assert(event);

    node = xmlNewNode(NULL, BAD_CAST "Event");
    xmlNewProp(node, "Code", "1"); /* <Event Code="1"> */

    str = ftd_string_integer(event->postId);
    xmlNewTextChild(node, NULL, "PostID", ftd_string_value(str));
    ftd_string_free(str);

    str = ftd_string_integer(event->stamp);
    xmlNewTextChild(node, NULL, "Stamp", ftd_string_value(str));
    ftd_string_free(str);

    str = ftd_string_integer(event->updated);
    xmlNewTextChild(node, NULL, "Updated", ftd_string_value(str));
    ftd_string_free(str);

    str = ftd_string_integer(event->status);
    xmlNewTextChild(node, NULL, "Status", ftd_string_value(str));
    ftd_string_free(str);

    if (event->groups) {
        str = ftd_splitted_list_concat(event->groups);
        tmpNode = xmlNewNode(NULL, "Groups");
        cdataNode = xmlNewCDataBlock(doc, ftd_string_value(str), ftd_string_length(str));
        xmlAddChild(tmpNode, cdataNode);
        xmlAddChild(node, tmpNode);
        //xmlNewTextChild(node, NULL, "Groups", ftd_string_value(str));
        ftd_string_free(str);
    }

    // Het veld filename is bij MP3-postings niet bekend
    if (event->fileName) {
        tmpNode = xmlNewNode(NULL, "FName");
        cdataNode = xmlNewCDataBlock(doc, ftd_string_value(event->fileName), ftd_string_length(event->fileName));
        xmlAddChild(tmpNode, cdataNode);
        xmlAddChild(node, tmpNode);
    }

    if (event->title) { // Tijdelijke workaround..iemand heeft een FTD posting zonder title in de testdb gemikt
        tmpNode = xmlNewNode(NULL, "Title");
        cdataNode = xmlNewCDataBlock(doc, ftd_string_value(event->title), ftd_string_length(event->title));
        xmlAddChild(tmpNode, cdataNode);
        xmlAddChild(node, tmpNode);
    }

    tmpNode = xmlNewNode(NULL, "Poster");
    cdataNode = xmlNewCDataBlock(doc, ftd_string_value(event->poster), ftd_string_length(event->poster));
    xmlAddChild(tmpNode, cdataNode);
    xmlAddChild(node, tmpNode);

    // Niet elke FTD posting heeft een link
    if (event->link) {
        tmpNode = xmlNewNode(NULL, "Link");
        cdataNode = xmlNewCDataBlock(doc, ftd_string_value(event->link), ftd_string_length(event->link));
        xmlAddChild(tmpNode, cdataNode);
        xmlAddChild(node, tmpNode);
    }

    str = ftd_string_integer(event->rating);
    xmlNewTextChild(node, NULL, "Rating", ftd_string_value(str));
    ftd_string_free(str);

    str = ftd_string_integer(event->cat);
    xmlNewTextChild(node, NULL, "Cat", ftd_string_value(str));
    ftd_string_free(str);

    str = ftd_string_integer(event->sCat);
    xmlNewTextChild(node, NULL, "SCat", ftd_string_value(str));
    ftd_string_free(str);

    if (event->tags) {
        str = ftd_splitted_list_concat(event->tags);
        tmpNode = xmlNewNode(NULL, "Tags");
        cdataNode = xmlNewCDataBlock(doc, ftd_string_value(str), ftd_string_length(str));
        xmlAddChild(tmpNode, cdataNode);
        xmlAddChild(node, tmpNode);
        //xmlNewTextChild(node, NULL, "Groups", ftd_string_value(str));
        ftd_string_free(str);
    }

    if (event->extra_tag) {
        tmpNode = xmlNewNode(NULL, "Tag");
        cdataNode = xmlNewCDataBlock(doc, ftd_string_value(event->extra_tag), ftd_string_length(event->extra_tag));
        xmlAddChild(tmpNode, cdataNode);
        xmlAddChild(node, tmpNode);
    }

    return node;
}

/**
 * Voegt alle gegevens van een event in een XML document in.
 *
 * @arg     event       Het event waarvan de gegevens weggeschreven moeten worden.
 * @arg     doc         Het XML document waar de gegevens in gezet moeten worden.
 *
 * @return  Een nieuwe XML node die het event omvat.
 */
xmlNodePtr
ftd_events_xml_add_all(FtdEvent *event, xmlDocPtr doc)
{
    xmlNodePtr node;

    g_assert(event);
    g_assert(doc);

    node = ftd_events_xml_add_basic(event, doc);

    return node;
}
