/* ====================================================================
 * 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 <time.h>
#include "ftd_gui_main.h"
#include "ftd_gui_worksheet.h"
#include "ftd_gui_center.h"
#include "ftd_gui_center_eventlist.h"
#include "ftd_gui_categories.h"
#include "ftd_gui_xslt.h"
#include "ftd_gui_parse.h"

gboolean     ftd_gui_dayselector_day_selected(GtkWidget *, gpointer);
gboolean     ftd_gui_dayselector_do_create(gpointer);

void
ftd_gui_dayselector_new(void)
{
    GtkWidget *calendar;

    g_assert(gui);

    gui->dayselector = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_title (GTK_WINDOW (gui->dayselector), "Day selector");
    gtk_window_set_position (GTK_WINDOW (gui->dayselector), GTK_WIN_POS_CENTER_ALWAYS);
    gtk_window_set_modal(GTK_WINDOW(gui->dayselector), TRUE);
    gtk_window_set_transient_for(GTK_WINDOW(gui->dayselector), GTK_WINDOW(gui->window));
    gtk_window_set_resizable (GTK_WINDOW(gui->dayselector), FALSE);

    calendar = gtk_calendar_new();
    gtk_widget_show(calendar);
    gtk_container_add(GTK_CONTAINER(gui->dayselector), calendar);

    g_signal_connect(G_OBJECT(calendar), "day-selected",
                     G_CALLBACK(ftd_gui_dayselector_day_selected), NULL);

    gtk_widget_show(gui->dayselector);
}

gboolean
ftd_gui_dayselector_day_selected(GtkWidget *caller, gpointer data)
{
    struct tm time;
    struct tm *ptr_time;
    time_t tmp;
    FtdList *tasks = NULL;
    FtdStatusTask *task;
    FtdFilter *filter;
    FTDEventlist *eventlist;

    filter = ftd_filter_new_filter();

    /* stel het filter goed in */
    if (ftd_config_get()->toonCategoriseerdeLijstBijKnopNieuw) {
        filter->categoriseerd = TRUE;
    }

    time.tm_sec = 59;
    time.tm_min = 59;
    time.tm_hour = 23;
    time.tm_isdst = -1;
    gtk_calendar_get_date(GTK_CALENDAR(caller), &time.tm_year, &time.tm_mon, &time.tm_mday);
    time.tm_year -= 1900;
    tmp = mktime(&time);
    ptr_time = localtime(&tmp);
    filter->end_time = mktime(ptr_time);

    time.tm_sec = 0;
    time.tm_min = 0;
    time.tm_hour = 0;
    time.tm_isdst = -1;
    gtk_calendar_get_date(GTK_CALENDAR(caller), &time.tm_year, &time.tm_mon, &time.tm_mday);
    time.tm_year -= 1900;
    tmp = mktime(&time);
    ptr_time = localtime(&tmp);
    filter->start_time = mktime(ptr_time);

    filter->daySelector = TRUE;

    eventlist = ftd_eventlist_get();
    ftd_eventlist_apply_filter(eventlist, filter);
    g_object_unref(eventlist);

    ftd_filter_free_filter(filter);

    gtk_widget_destroy(gui->dayselector);
    gui->dayselector = NULL;

    task = (FtdStatusTask *) g_malloc0(sizeof(FtdStatusTask));
    task->tekst = ftd_string_new("Samenstellen van lijst");
    tasks = ftd_list_append(tasks, task);

    task = (FtdStatusTask *) g_malloc0(sizeof(FtdStatusTask));
    task->tekst = ftd_string_new("Laden van lijst");
    tasks = ftd_list_append(tasks, task);

    ftd_gui_status_new(tasks, gui->window);

    g_idle_add(ftd_gui_dayselector_do_create, FALSE);

    return FALSE;
}

gboolean
ftd_gui_dayselector_do_create(gpointer data)
{
    int pagenum;
#ifndef NO_EXTERNAL_BROWSER
    FtdString *uitvoer;

    uitvoer = ftd_events_sort_filtered(ftd_gui_parse);
    if (ftd_config_get()->toonCategoriseerdeLijstBijKnopNieuw) {
        ftd_gui_xslt_process (uitvoer, XSLT_TYPE_LIST, HTML_LIST_FILE);
    } else {
        ftd_gui_xslt_process (uitvoer, XSLT_TYPE_LIST_TOTAL, HTML_LIST_FILE);
    }

    ftd_string_free (uitvoer);
#endif

    // Zijn we wel op het juiste tabblad actief?
    pagenum = gtk_notebook_page_num(GTK_NOTEBOOK(gui->worksheet->center->widget), gui->worksheet->center->eventlist->widget);
    gtk_notebook_set_current_page(GTK_NOTEBOOK(gui->worksheet->center->widget), pagenum);

    ftd_gui_status_push();

    ftd_gui_categories_fill();

    ftd_gui_center_eventlist_show(NULL);

    return FALSE;
}
