/* ====================================================================
 * 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 "ftd_gui_main.h"
#include "images/status_bg.xpm"
#include "images/icon.xpm"

static FtdList *tasks;
static GtkWidget *window;

static gboolean     ftd_gui_status_destroy(GtkWidget *, GdkEvent *, gpointer);
static void         ftd_gui_status_printTasks(GtkWidget *);

void
ftd_gui_status_new(FtdList *status_tasks, GtkWidget *parent)
{
    GdkPixmap *pixmap;
    GtkStyle *style;
    GtkWidget *logo;
    GtkWidget *fixed;
    int height;
    GtkWidget *label;
    char *path;
    char *title;

    g_assert(window == NULL);

    tasks = status_tasks;

    if (!ftd_config_get()->toonStatusBijLaden) {
        return;
    }

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "OpenFTD Status");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
    gtk_window_set_modal(GTK_WINDOW(window), TRUE);
    gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
    gtk_widget_set_app_paintable(window, TRUE);
    gtk_widget_set_size_request(window, 450, 200);
    gtk_window_set_default_size(GTK_WINDOW(window), 450, 200);
    gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
    gtk_window_set_icon(GTK_WINDOW(window), gdk_pixbuf_new_from_xpm_data((const char **)icon_xpm));
    gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), TRUE);
    if (parent) {
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);
        gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
    }

    g_signal_connect(G_OBJECT(window), "delete-event", G_CALLBACK(ftd_gui_status_destroy), NULL);

    fixed = gtk_fixed_new();
    gtk_widget_show(fixed);
    gtk_container_add(GTK_CONTAINER(window), fixed);

    gtk_widget_show(window);

    pixmap = gdk_pixmap_create_from_xpm_d(window->window, NULL, NULL, status_bg_xpm);
    style = gtk_widget_get_default_style();
    style->bg_pixmap[0] = pixmap;
    gtk_widget_set_style(window, style);

    path = ftd_file_parse_data_dir("/images/logo.png");
    logo = gtk_image_new_from_file(path);
    g_free(path);

    height = gdk_pixbuf_get_height(gtk_image_get_pixbuf(GTK_IMAGE(logo)));

    gtk_fixed_put(GTK_FIXED(fixed), logo, 10, (200 / 2) - (height / 2) + 15);
    gtk_widget_show(logo);

    if (ftd_svn_get_revision()) {
        title = g_strdup_printf("<span font_desc='Sans Bold 18'>OpenFTD - SVN Revision %s</span>", ftd_svn_get_revision());
    } else {
        title = g_strdup_printf("<span font_desc='Sans Bold 18'>OpenFTD - Versie 1.0 Beta 6</span>");
    }
    label = gtk_label_new(title);
    g_free(title);
    gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
    gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
    gtk_widget_show(label);
    gtk_fixed_put(GTK_FIXED(fixed), label, 0, 5);
    gtk_widget_set_size_request(label, 450, -1);

    ftd_gui_status_printTasks(fixed);

    while (gtk_events_pending ())
        gtk_main_iteration ();
}

void
ftd_gui_status_disable_keep_above(void)
{
    if (!ftd_config_get()->toonStatusBijLaden) {
        return;
    }

    gtk_window_set_keep_above(GTK_WINDOW(window), FALSE);
}

void
ftd_gui_status_set_parent(GtkWidget *parent)
{
    if (!ftd_config_get()->toonStatusBijLaden) {
        return;
    }

    gtk_window_set_modal(GTK_WINDOW(window), TRUE);
    gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(parent));
    gtk_window_present(GTK_WINDOW(window));
    while (gtk_events_pending ())
        gtk_main_iteration ();
}

static gboolean
ftd_gui_status_destroy(GtkWidget *caller, GdkEvent *event, gpointer data)
{
#if 0       // Moeten we deze instructie wel actief laten ?
    /* Laten we de gebruiker eens lekker pesten en het sluiten
       van het status venster onmogelijk maken. Dit hoort namelijk
       automatisch te gebeuren zodra alle taken klaar zijn */
    return TRUE;
#else
    FtdList *list = tasks;
    window = NULL;

    while (list) {
        FtdStatusTask *task = ftd_list_value(list);
        ftd_string_free(task->tekst);
        g_free(task);
        list = ftd_list_next(list);
    }

    ftd_list_free(tasks);
    tasks = NULL;

    return FALSE;
#endif
}

static void
ftd_gui_status_printTasks(GtkWidget *fixed)
{
    FtdList *list = tasks;
    int block_height = 0;
    int pos = 0;
    char *filename;

    g_assert(tasks);

    while (list) {
        FtdStatusTask *task = ftd_list_value(list);

        task->label = gtk_label_new(ftd_string_value(task->tekst));
        if (pos == 0) {
            int count = ftd_list_length(list);

            // Berekenen wat de meest optimale afmeting is voor het verspreiden van de tekst
            block_height = 150 / count;

            if (block_height < 24) {	// 24 == hoogte afbeeldingen
                g_print("Clipping will occur...\n");
            }

            gtk_label_set_markup(GTK_LABEL(task->label), g_strdup_printf("<b>%s</b>", ftd_string_value(task->tekst)));
        }

        gtk_fixed_put(GTK_FIXED(fixed), task->label, 175, block_height * pos + 50);
        gtk_widget_show(task->label);

        if (pos == 0) {
            filename = ftd_file_parse_data_dir("/gui/green.png");
            if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
                g_free(filename);
                filename = ftd_file_parse_data_dir("/images/green.png");
            }
            task->image = gtk_image_new_from_file(filename);
            g_free(filename);
        } else {
            task->image = gtk_image_new();
        }
        gtk_fixed_put(GTK_FIXED(fixed), task->image, 150, block_height * pos + 46);
        gtk_widget_show(task->image);

        pos++;

        list = ftd_list_next(list);
    }
}

void
ftd_gui_status_push(void)
{
    static int pos = 1;
    int teller = 0;
    FtdList *list = tasks;
    char *filename;

    if (!list) {
        g_print("ftd_gui_status_push(): list = NULL\n");
        pos = 1;
        return;
    }

    if (!ftd_config_get()->toonStatusBijLaden) {
        pos++;

        if (pos == ftd_list_length(tasks)) {
            list = tasks;
            while (list) {
                FtdStatusTask *task = ftd_list_value(list);
                ftd_string_free(task->tekst);
                g_free(task);
                list = ftd_list_next(list);
            }
            ftd_list_free(tasks);

            tasks = NULL;
            pos = 1;
        }

        return;
    }

    while (list) {
        FtdStatusTask *task = ftd_list_value(list);

        if (teller == pos - 1) {
            gtk_label_set_text(GTK_LABEL(task->label), ftd_string_value(task->tekst));

            filename = ftd_file_parse_data_dir("/gui/gray.png");
            if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
                g_free(filename);
                filename = ftd_file_parse_data_dir("/images/gray.png");
            }
            gtk_image_set_from_file(GTK_IMAGE(task->image), filename);
            g_free(filename);
        }

        if (teller == pos) {
            gchar *tekst = g_strdup_printf("<b>%s</b>", ftd_string_value(task->tekst));
            gtk_label_set_markup(GTK_LABEL(task->label), tekst);
            g_free(tekst);

            filename = ftd_file_parse_data_dir("/gui/green.png");
            if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
                g_free(filename);
                filename = ftd_file_parse_data_dir("/images/green.png");
            }
            gtk_image_set_from_file(GTK_IMAGE(task->image), filename);
            g_free(filename);
        }

        teller++;

        list = ftd_list_next(list);
    }

    if (pos == ftd_list_length(tasks)) {
        g_assert(window);
        gtk_widget_destroy(window);
        window = NULL;

        list = tasks;
        while (list) {
            FtdStatusTask *task = ftd_list_value(list);
            ftd_string_free(task->tekst);
            g_free(task);
            list = ftd_list_next(list);
        }
        ftd_list_free(tasks);
        tasks = NULL;

        pos = 1;
        return;
    }

    pos++;

    while (gtk_events_pending ())
        gtk_main_iteration ();
}

GtkWidget *
ftd_gui_status_get_window(void)
{
    return window;
}

