/* ====================================================================
 * 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 <libxml/tree.h>
#include "ftd_gui_main.h"
#include "ftd_gui_center.h"
#include "ftd_gui_worksheet.h"
#include "ftd_gui_center.h"
#include "ftd_gui_errhandler.h"
#include "ftd_gui_xslt.h"

void
ftd_gui_center_myftd_new(void)
{
    GtkWidget *label;
    GtkWidget *tabby;
    FtdGuiCenterMyFTD *myftd;
    int teller;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);

    // Hebben we reeds een MyFTD tabblad open staan ?
    if (gui->worksheet->center->myftd) {
        // Zoek de tab waar MyFTD op vermeld staat en activeer deze
        for (teller = 0; teller < gtk_notebook_get_n_pages(GTK_NOTEBOOK (gui->worksheet->center->widget)); teller++) {
            if (gtk_notebook_get_nth_page(GTK_NOTEBOOK (gui->worksheet->center->widget), teller) == gui->worksheet->center->myftd->widget) {
                // Activeer de tab. De signal handler van de notebook zorgt ervoor
                // dat de gegevens opnieuw aangemaakt en getoond worden.
                gtk_notebook_set_current_page (GTK_NOTEBOOK (gui->worksheet->center->widget), teller);

                return;
            }
        }
    }

    myftd = (FtdGuiCenterMyFTD *) g_malloc0(sizeof(FtdGuiCenterMyFTD));
    gui->worksheet->center->myftd = myftd;

    /* Initialiseer de inhoud van het schermpie */
    myftd->browser_data = ftd_gui_browser_new(gui->worksheet->center->widget, FALSE);
    myftd->widget = ftd_gui_browser_get_widget(myftd->browser_data);

    /* Voeg een pagina toe aan de notebook, met bijbehorend sluit-knopje */
    label = gtk_label_new ("MyFTD");
    gtk_widget_show (label);

    tabby = gtk_hbox_new(FALSE, 2);
    myftd->cmdclose = gtk_button_new();
    gtk_widget_set_size_request(GTK_WIDGET(myftd->cmdclose), 16, 16);
    gtk_container_add(GTK_CONTAINER(myftd->cmdclose), gtk_image_new_from_stock(GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
    gtk_widget_show_all (myftd->cmdclose);
    gtk_button_set_relief(GTK_BUTTON(myftd->cmdclose), GTK_RELIEF_NONE);

    gtk_box_pack_start(GTK_BOX(tabby), label, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(tabby), myftd->cmdclose, FALSE, FALSE, 0);

    g_assert (gui->worksheet->center->widget);
    gtk_notebook_set_tab_label (GTK_NOTEBOOK (gui->worksheet->center->widget), ftd_gui_browser_get_widget(myftd->browser_data), tabby);

    /* Signal handler voor het afsluiten knopje */
    g_signal_connect (GTK_WIDGET (myftd->cmdclose), "clicked", G_CALLBACK(ftd_gui_center_myftd_sluiten), NULL);

    /* Signal handler om de url klikjes vanuit het componentje op te vangen */
    ftd_gui_browser_set_link_clicked_cb(myftd->browser_data, ftd_gui_center_myftd_open_uri, NULL);

    ftd_gui_center_myftd_show(ftd_gui_browser_get_widget(myftd->browser_data), NULL);

    /* Trek gelijk de aandacht */
    //gtk_widget_grab_focus (GTK_WIDGET (watchlist->widget));
    gtk_notebook_set_current_page (GTK_NOTEBOOK (gui->worksheet->center->widget), -1);
}

void
ftd_gui_center_myftd_show (GtkWidget *caller, gpointer data)
{
    FtdGuiCenterMyFTD *myftd;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);
    g_assert(gui->worksheet->center->myftd);
    myftd = gui->worksheet->center->myftd;

    ftd_gui_browser_navigate(myftd->browser_data, HTML_MYFTD_FILE);
}

void
ftd_gui_center_myftd_sluiten (GtkWidget *caller, gpointer data)
{
    FtdGuiCenterMyFTD *myftd;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);
    g_assert(gui->worksheet->center->myftd);
    myftd = gui->worksheet->center->myftd;

    ftd_gui_browser_destroy(myftd->browser_data);
    g_free(myftd);
    gui->worksheet->center->myftd = NULL;
}

gboolean
ftd_gui_center_myftd_open_uri (GtkWidget *embed, const char *uri, gpointer data)
{
    FtdGuiCenterMyFTD *myftd;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->center);
    g_assert(gui->worksheet->center->myftd);
    myftd = gui->worksheet->center->myftd;

    if (strstr(uri, "http://")) {
        ftd_gui_dialog_show(NULL, "Not yet implemented", gui->window, TRUE);
        return TRUE;
    }

    if (strstr(uri, "#post")) {
        return ftd_gui_center_eventlist_open_uri(embed, uri, NULL);
    }

    if (strstr(uri, "#myftd-remove")) {
        ftd_gui_dialog_show(NULL, "Not yet implemented", gui->window, TRUE);
        return TRUE;
    }

    if (strstr(uri, "#myftd-edit")) {
        ftd_gui_dialog_show(NULL, "Not yet implemented", gui->window, TRUE);
        return TRUE;
    }

    if (strstr(uri, "#myftd-update")) {
        FtdErrorCode retval;
        char *errmsg = NULL;
        int postid = atoi(strstr(uri, "#myftd-update") + 13);

        retval = ftd_myftd_update(postid, &errmsg);
        if (retval != ERROR_NONE) {
            ftd_gui_errhandler(gui->window, FTD_ONDERDEEL_EVENTINFO, retval, errmsg);

            if (errmsg) {
                g_free(errmsg);
            }

            return TRUE;
        }

        if (errmsg) {
            g_free(errmsg);
        }

        retval = ftd_gui_center_myftd_create(&errmsg);
        if (retval != ERROR_NONE) {
            ftd_gui_errhandler(gui->window, FTD_ONDERDEEL_MYFTD, retval, errmsg);

            if (errmsg) {
                g_free(errmsg);
            }

            return FALSE;
        }

        if (errmsg) {
            g_free(errmsg);
        }

        ftd_gui_center_myftd_show(ftd_gui_browser_get_widget(myftd->browser_data), NULL);

        return TRUE;
    }

    return FALSE;
}

/**
 * Vraagt de gegevens in MyFTD op bij de FTD servers en maakt een mooie HTML uitvoer hiervan.
 *
 * @arg     errmsg      Een eventueel foutbericht van de server wordt hier in geplaatst.
 *
 * @return  Een fout-code die aangeeft of dit succesvol was.
 */
FtdErrorCode
ftd_gui_center_myftd_create(char **errmsg)
{
    FtdList *list;
    int retval;
    xmlDocPtr doc;
    xmlChar *xml_data;
    FtdString *uitvoer;
    char *xslt_path;

    retval = ftd_http_curl_execute(FTD_URL_MYFTD, NULL, TRUE, &uitvoer, TRUE, NULL, TRUE);
    if (retval) {
        if (retval == ERROR_MSG) {
            *errmsg = g_strdup(ftd_string_value(uitvoer));
        } else {
            *errmsg = NULL;
        }

        return retval;
    }

    retval = ftd_myftd_parse(uitvoer, &list);
    if (retval != 0) {
        ftd_string_free(uitvoer);
        *errmsg = NULL;
        return ERROR_PARSE;
    }

    ftd_string_free(uitvoer);

    doc = ftd_myftd_print(list);

    // FIXME: Bij geen mogelijkheid tot testen: wordt list hierna niet volledig discarded en zou deze niet opgeruimd moeten worden?

    xmlDocDumpMemory(doc, &xml_data, &retval);

    uitvoer = ftd_string_new(xml_data);
    xmlFree(xml_data);
    xslt_path = XSLT_TYPE_MYFTD;

    retval = ftd_gui_xslt_process(uitvoer, xslt_path, HTML_MYFTD_FILE);

    g_free(xslt_path);

    if (retval) {
        ftd_string_free( uitvoer );
        *errmsg = NULL;
        return ERROR_PARSE;
    }

    ftd_string_free(uitvoer);

    xmlFreeDoc(doc);
    //xmlCleanupParser();

    return ERROR_NONE;
}
