/* ====================================================================
 * 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 "ftd_gui_main.h"
#include "ftd_gui_search.h"
#include "ftd_gui_center_eventlist.h"
#include "ftd_gui_errhandler.h"
#include "ftd_gui_xslt.h"
#include "ftd_gui_parse.h"

void
ftd_gui_search_new(void)
{
    FtdGuiSearch *search = (FtdGuiSearch *) g_malloc(sizeof(FtdGuiSearch));
    GSList *radiogroup = NULL;

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->sidebar);
    gui->worksheet->sidebar->search = search;

    search->widget = gtk_vbox_new (FALSE, 5);
    search->hbox = gtk_hbox_new (FALSE, 5);
    gtk_widget_show (search->hbox);
    gtk_widget_show (search->widget);

    gtk_box_pack_start (GTK_BOX (search->widget), search->hbox, TRUE, TRUE, 0);

    search->table = gtk_table_new(4, 2, FALSE);
    gtk_widget_show(search->table);

    gtk_box_pack_end (GTK_BOX (search->widget), search->table, TRUE, TRUE, 0);

    search->entry = gtk_entry_new ();
    gtk_widget_show (search->entry);
    gtk_box_pack_start (GTK_BOX (search->hbox), search->entry, TRUE, TRUE, 0);
    gtk_entry_set_activates_default(GTK_ENTRY(search->entry), TRUE);

    search->button = gtk_button_new_from_stock ("gtk-ok");
    gtk_widget_show (search->button);
    gtk_box_pack_start (GTK_BOX (search->hbox), search->button, FALSE, FALSE, 0);
    g_signal_connect_after (G_OBJECT(search->button), "clicked", G_CALLBACK(ftd_gui_search_clicked), NULL);

    search->titel = gtk_radio_button_new_with_mnemonic (NULL, "Titel");
    gtk_widget_show (search->titel);
    gtk_table_attach (GTK_TABLE (search->table), search->titel, 0, 1, 0, 1,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);
    gtk_radio_button_set_group(GTK_RADIO_BUTTON (search->titel), radiogroup);
    radiogroup = gtk_radio_button_get_group (GTK_RADIO_BUTTON (search->titel));

    search->tag = gtk_radio_button_new_with_mnemonic (NULL, "Tag");
    gtk_widget_show (search->tag);
    gtk_table_attach (GTK_TABLE (search->table), search->tag, 0, 1, 3, 4,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);

    gtk_radio_button_set_group(GTK_RADIO_BUTTON(search->tag), radiogroup);
    radiogroup = gtk_radio_button_get_group(GTK_RADIO_BUTTON (search->tag));

    search->ftd_id = gtk_radio_button_new_with_mnemonic (NULL, "FTD ID");
    gtk_widget_show (search->ftd_id);
    gtk_table_attach (GTK_TABLE (search->table), search->ftd_id, 0, 1, 2, 3,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);

    gtk_radio_button_set_group(GTK_RADIO_BUTTON(search->ftd_id), radiogroup);
    radiogroup = gtk_radio_button_get_group(GTK_RADIO_BUTTON (search->ftd_id));

    search->poster = gtk_radio_button_new_with_mnemonic (NULL, "Posternaam");
    gtk_widget_show (search->poster);
    gtk_table_attach (GTK_TABLE (search->table), search->poster, 0, 1, 1, 2,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);

    gtk_radio_button_set_group(GTK_RADIO_BUTTON(search->poster), radiogroup);
    radiogroup = gtk_radio_button_get_group(GTK_RADIO_BUTTON (search->poster));

    search->profiles = gtk_radio_button_new_with_mnemonic (NULL, "Profielen");
    gtk_widget_show (search->profiles);
    gtk_table_attach (GTK_TABLE(search->table), search->profiles, 1, 2, 0, 1,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);
    gtk_radio_button_set_group (GTK_RADIO_BUTTON (search->profiles), radiogroup);
    radiogroup = gtk_radio_button_get_group(GTK_RADIO_BUTTON(search->profiles));

    search->updates = gtk_check_button_new_with_mnemonic ("Updates");
    gtk_widget_show (search->updates);
    gtk_table_attach (GTK_TABLE (search->table), search->updates, 1, 2, 2, 3,
                      (GtkAttachOptions) (GTK_FILL),
                      (GtkAttachOptions) (0), 0, 0);

    gtk_signal_connect_object (GTK_OBJECT (search->entry), "activate",
        GTK_SIGNAL_FUNC (gtk_button_clicked), GTK_OBJECT (search->button));
}

void
ftd_gui_search_clicked (GtkWidget *caller, gpointer data)
{
    FtdGuiSearch *search;
    FtdFilter *oudFilter;
    FtdFilter *nieuwFilter;
    FTDEventlist *eventlist;
    char *xslt_path;
#ifndef NO_EXTERNAL_BROWSER
    FtdString *uitvoer;
#endif

    g_assert(gui);
    g_assert(gui->worksheet);
    g_assert(gui->worksheet->sidebar);
    search = gui->worksheet->sidebar->search;

    g_assert (search);

    if (!strcmp(gtk_entry_get_text(GTK_ENTRY(search->entry)), "")) {
        ftd_gui_dialog_show(NULL, "Gelieve eerst een zoekterm in te voeren", gui->window, FALSE);
        return;
    }

    // Bewaar een kopie van het huidige filter..als we maar 1 zoekresultaat
    // hebben moeten we het originele filter weer herstellen
    eventlist = ftd_eventlist_get();
    oudFilter = ftd_eventlist_get_filter(eventlist);

    nieuwFilter = ftd_filter_new_filter();

    nieuwFilter->veld = FTDEVENT_SORT_DATUM;

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->titel))) {
        nieuwFilter->title = ftd_string_new(gtk_entry_get_text(GTK_ENTRY(search->entry)));
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->poster))) {
        nieuwFilter->poster = ftd_string_new(gtk_entry_get_text(GTK_ENTRY(search->entry)));
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->tag))) {
        nieuwFilter->extra_tag = ftd_string_new(gtk_entry_get_text(GTK_ENTRY(search->entry)));
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->profiles))) {
        ftd_gui_profile_show(gtk_entry_get_text(GTK_ENTRY(search->entry)));;

        ftd_filter_free_filter(nieuwFilter);
        ftd_filter_free_filter(oudFilter);

        return;
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->ftd_id))) {
        char *err_pos;
        nieuwFilter->postId = strtol(gtk_entry_get_text(GTK_ENTRY(search->entry)), &err_pos, 10);
        if (strcmp(err_pos, "")) {
            ftd_gui_dialog_show(NULL, "Ongeldige FTD post ID ingevoerd.", gui->window, TRUE);

            ftd_filter_free_filter(nieuwFilter);

            return;
        }
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->updates))) {
        nieuwFilter->prevCheck = ftd_config_get()->prevCheck;
    }

    nieuwFilter->categoriseerd = FALSE;

    ftd_eventlist_apply_filter(eventlist, nieuwFilter);

    ftd_filter_free_filter(nieuwFilter);

    if (ftd_eventlist_get_count(eventlist, TRUE) == 1 && ftd_config_get()->toonInfoBijEenZoekresultaat) {
        // Toon direct de posting die aan onze voorwaarden voldoet
        char posting[32];
        FtdEvent *event = ftd_eventlist_get_first_filtered_event(eventlist);

        g_assert(event);

        snprintf(posting, sizeof(posting), "list.html#post%i", event->postId);
        ftd_gui_center_eventlist_open_uri(NULL, posting, NULL);

        // Herstel het originele filter
        ftd_eventlist_apply_filter(eventlist, oudFilter);

        ftd_filter_free_filter(oudFilter);
        ftd_event_free(event);
        g_object_unref(eventlist);

        return;
    }

    g_object_unref(eventlist);

#ifndef NO_EXTERNAL_BROWSER
    uitvoer = ftd_events_sort_filtered(ftd_gui_parse);

    if (uitvoer == NULL) {
        g_warning("ftd_gui_search_clicked(): Sorteren van lijst mislukt\n");
        return;
    }

    xslt_path = XSLT_TYPE_LIST_TOTAL;
    ftd_gui_xslt_process(uitvoer, xslt_path, HTML_LIST_FILE);

    g_free(xslt_path);
    ftd_string_free (uitvoer);
#endif

    ftd_gui_center_eventlist_show(NULL);

    ftd_filter_free_filter(oudFilter);
}
