/* ====================================================================
 * 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 "ftd_gui_kudos.h"
#include "ftd_gui_center_eventlist.h"
#include "ftd_gui_errhandler.h"

void
ftd_gui_kudos_new(int post_id, int saldo, int aantal)
{
    FtdGuiKudos *kudos;
    GtkWidget *vbox3;
    GtkWidget *table1;
    GtkWidget *label9;
    GtkWidget *label10;
    GtkWidget *label11;
    GtkWidget *label12;
    GtkWidget *btnGeef;
    FtdEvent *event;
    FTDEventlist *eventlist;
    char tmp[16];

    g_assert(gui);

    // Kijk of er soms reeds een kudos venster geopend is
    if (gui->kudos) {
        gtk_widget_grab_focus(gui->kudos->window);
        return;
    }

    if (post_id == 0) {
        g_warning("ftd_gui_kudos_new(): post_id == 0\n");
        return;
    }

    eventlist = ftd_eventlist_get();
    event = ftd_eventlist_lookup_id(eventlist, post_id);
    g_object_unref(eventlist);
    if (!event) {
        g_warning("ftd_gui_kudos_new(): event == NULL\n");
        return;
    }

    kudos = (FtdGuiKudos *) g_malloc(sizeof(FtdGuiKudos));
    gui->kudos = kudos;
    kudos->post_id = post_id;

    kudos->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (kudos->window), "Kudos");
    gtk_window_set_position (GTK_WINDOW (kudos->window), GTK_WIN_POS_CENTER);
    gtk_window_set_modal (GTK_WINDOW (kudos->window), TRUE);
    gtk_window_set_transient_for(GTK_WINDOW(kudos->window), GTK_WINDOW(gui->window));

    vbox3 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox3);
    gtk_container_add (GTK_CONTAINER (kudos->window), vbox3);

    table1 = gtk_table_new (4, 2, FALSE);
    gtk_widget_show (table1);
    gtk_box_pack_start (GTK_BOX (vbox3), table1, FALSE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (table1), 15);

    label9 = gtk_label_new ("Titel :");
    gtk_widget_show (label9);
    gtk_table_attach (GTK_TABLE (table1), label9, 0, 1, 0, 1,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_label_set_justify (GTK_LABEL (label9), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (label9), 0, 0.5);

    label10 = gtk_label_new ("Poster :");
    gtk_widget_show (label10);
    gtk_table_attach (GTK_TABLE (table1), label10, 0, 1, 1, 2,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_label_set_justify (GTK_LABEL (label10), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (label10), 0, 0.5);

    kudos->lblPoster = gtk_label_new (ftd_string_value(event->poster));
    gtk_widget_show (kudos->lblPoster);
    gtk_table_attach (GTK_TABLE (table1), kudos->lblPoster, 1, 2, 1, 2,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_label_set_justify (GTK_LABEL (kudos->lblPoster), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (kudos->lblPoster), 0, 0.5);

    label11 = gtk_label_new ("Kudos Saldo :");
    gtk_widget_show (label11);
    gtk_table_attach (GTK_TABLE (table1), label11, 0, 1, 2, 3,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_label_set_justify (GTK_LABEL (label11), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (label11), 0, 0.5);

    snprintf(tmp, sizeof(tmp), "%i", saldo);
    kudos->lblSaldo = gtk_label_new (tmp);
    gtk_widget_show (kudos->lblSaldo);
    gtk_table_attach (GTK_TABLE (table1), kudos->lblSaldo, 1, 2, 2, 3,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_label_set_justify (GTK_LABEL (kudos->lblSaldo), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (kudos->lblSaldo), 0, 0.5);

    kudos->lblAantal = gtk_label_new ("1");
    gtk_widget_show (kudos->lblAantal);
    gtk_table_attach (GTK_TABLE (table1), kudos->lblAantal, 1, 2, 3, 4,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_label_set_justify (GTK_LABEL (kudos->lblAantal), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (kudos->lblAantal), 0, 0.5);

    label12 = gtk_label_new ("Aantal te geven kudos :");
    gtk_widget_show (label12);
    gtk_table_attach (GTK_TABLE (table1), label12, 0, 1, 3, 4,
                       (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                       (GtkAttachOptions) (0), 0, 0);
    gtk_label_set_justify (GTK_LABEL (label12), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (label12), 0, 0.5);

    kudos->lblTitel = gtk_label_new (ftd_string_value(event->title));
    gtk_widget_show (kudos->lblTitel);
    gtk_table_attach (GTK_TABLE (table1), kudos->lblTitel, 1, 2, 0, 1,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_label_set_justify (GTK_LABEL (kudos->lblTitel), GTK_JUSTIFY_LEFT);
    gtk_misc_set_alignment (GTK_MISC (kudos->lblTitel), 0, 0.5);

    kudos->scrollbar_kudos = gtk_hscrollbar_new (GTK_ADJUSTMENT (gtk_adjustment_new (0, 0, aantal, 1, 1, 1)));
    gtk_widget_show (kudos->scrollbar_kudos);
    gtk_box_pack_start (GTK_BOX (vbox3), kudos->scrollbar_kudos, TRUE, TRUE, 0);

    btnGeef = gtk_button_new_with_mnemonic ("Geef Kudos");
    gtk_widget_show (btnGeef);
    gtk_box_pack_start (GTK_BOX (vbox3), btnGeef, FALSE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (btnGeef), 15);

    gtk_widget_show(kudos->window);

    g_signal_connect(G_OBJECT(kudos->scrollbar_kudos), "value-changed",
                     G_CALLBACK(ftd_gui_kudos_scroll), NULL);
    g_signal_connect(G_OBJECT(btnGeef), "clicked",
                     G_CALLBACK(ftd_gui_kudos_geef_clicked), NULL);
    g_signal_connect(G_OBJECT(kudos->window), "destroy",
                     G_CALLBACK(ftd_gui_kudos_callback_afsluiten), NULL);

    ftd_event_free(event);
}

gboolean
ftd_gui_kudos_geef_clicked(GtkWidget *caller, gpointer data)
{
    FtdErrorCode retval;
    char url[32];
    int pos = (int) gtk_range_get_value(GTK_RANGE(gui->kudos->scrollbar_kudos));
    char *errmsg;

    g_assert(gui);
    g_assert(gui->kudos);

    retval = ftd_kudos_give(gui->kudos->post_id, pos + 1, &errmsg);

    if (retval != ERROR_NONE) {
        ftd_gui_errhandler(gui->kudos->window, FTD_ONDERDEEL_KUDOS_GIVE, retval, errmsg);

        if (errmsg) {
            g_free(errmsg);
        }

        return FALSE;
    }

    snprintf(url, sizeof(url), "iets.html#post%i", gui->kudos->post_id);

    gtk_widget_destroy(gui->kudos->window);
    g_free(gui->kudos);
    gui->kudos = NULL;

    /* De event info moet nu opnieuw opgehaald en getoont worden */
    ftd_gui_center_eventlist_open_uri(NULL, url, NULL);

    return FALSE;
}

gboolean
ftd_gui_kudos_scroll(GtkWidget *caller, gpointer data)
{
    int pos;
    char tmp[16];

    g_assert(gui);
    g_assert(gui->kudos);

    pos = (int) gtk_range_get_value(GTK_RANGE(gui->kudos->scrollbar_kudos));

    snprintf(tmp, sizeof(tmp), "%i", pos + 1);
    gtk_label_set_text(GTK_LABEL(gui->kudos->lblAantal), tmp);

    return FALSE;
}

gboolean
ftd_gui_kudos_callback_afsluiten(GtkWidget *caller, gpointer data)
{
    g_assert(gui);
    g_assert(gui->kudos);
    gtk_widget_destroy(gui->kudos->window);
    g_free(gui->kudos);
    gui->kudos = NULL;
    return FALSE;
}
