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

static gboolean ftd_gui_modje_callback_afsluiten(GtkWidget *caller, gpointer data);
static gboolean ftd_gui_modje_waarschuw_clicked(GtkWidget *caller, gpointer data);

struct _ftd_gui_modje {
    GtkWidget   *window;
    GtkWidget   *comboReason;
    int          post_id;
    int          forum_id;
};

FtdGuiModje *
ftd_gui_modje_create (int post_id, int forum_id)
{
    FtdGuiModje *modje;
    GtkWidget *vbox1;
    GtkWidget *lblTekst;
    GtkWidget *frame1;
    GtkWidget *vbox2;
    GtkWidget *label3;
    GtkWidget *label2;
    GtkWidget *hbuttonbox1;
    GtkWidget *btnWaarschuw;

    g_return_if_fail(post_id > 0);

    if (gui->modje) {
        gtk_widget_grab_focus(gui->modje->window);
        return;
    }

    modje = g_malloc0(sizeof(FtdGuiModje));
    gui->modje = modje;

    modje->post_id = post_id;
    modje->forum_id = forum_id;

    modje->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (modje->window), "FTD Modjes");
    gtk_window_set_position (GTK_WINDOW (modje->window), GTK_WIN_POS_CENTER);
    gtk_window_set_modal (GTK_WINDOW (modje->window), TRUE);
    gtk_window_set_transient_for(GTK_WINDOW(modje->window), GTK_WINDOW(gui->window));

    vbox1 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox1);
    gtk_container_add (GTK_CONTAINER (modje->window), vbox1);

    if (forum_id > 0) {
        char *msg;
        FTDEventlist *eventlist;
        FtdEvent *event;
        FtdEventForumReactie *reactie = NULL;
        FtdList *list;
        char *stamp;

        eventlist = ftd_eventlist_get();
        event = ftd_eventlist_lookup_id(eventlist, post_id);
        g_object_unref(eventlist);

        g_assert(event->forum);

        list = event->forum->forum_reacties;
        while (list) {
            reactie = ftd_list_value(list);

            if (reactie->messageId == forum_id) {
                break;
            }

            reactie = NULL;

            list = ftd_list_next(list);
        }

        g_return_if_fail(reactie);

        stamp = ftd_string_date(reactie->stamp);

        msg = g_strdup_printf("Je hebt aangegeven een modje te willen wijzen op een\nforumbericht, geplaatst door %s op %s.\n\nHet forumbericht zal worden verwijderd en de persoon krijgt een waarschuwing als dit bericht:\n\n- Informatie bevat die mogelijk bedreigend is voor de legaliteit van FTD\n- Taalgebruik of inhoud bevat dat/die ongepast geacht wordt\n\nNamens de modjes van FTD, heel erg bedankt voor je inzet om FTD schoon te houden.", ftd_string_value(reactie->author), stamp);

        lblTekst = gtk_label_new (msg);

        g_free(stamp);
        ftd_event_free(event);
        g_free(msg);
    } else {
        char *posting;
        char *msg;
        FTDEventlist *eventlist;
        FtdEvent *event;

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

        msg = g_strdup_printf("Je hebt aangegeven een modje te willen waarschuwen voor de volgende post :\n\n%s\n\nDe post zal worden verwijderd en de poster krijgt een waarschuwing als deze post :\n\n- Een zogenaamde animopost is\n- Hinderlijk onjuiste postinformatie bevat\n- Informatie bevat die mogelijk bedreigend is voor de legaliteit van FTD\n- Taalgebruik of inhoud bevat dat/die ongepast geacht wordt\n\nNamens de modjes van FTD, heel erg bedankt voor je inzet om FTD schoon te houden.", ftd_string_value(event->title));

        lblTekst = gtk_label_new (msg);

        g_free(msg);
    }

    gtk_widget_show (lblTekst);
    gtk_box_pack_start (GTK_BOX (vbox1), lblTekst, FALSE, FALSE, 0);
    gtk_misc_set_padding (GTK_MISC (lblTekst), 8, 8);

    frame1 = gtk_frame_new (NULL);
    gtk_widget_show (frame1);
    gtk_box_pack_start (GTK_BOX (vbox1), frame1, TRUE, TRUE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (frame1), 8);

    vbox2 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox2);
    gtk_container_add (GTK_CONTAINER (frame1), vbox2);

    label3 = gtk_label_new ("Geef hieronder aan waarom jij vindt dat een modje deze post zou moeten bekijken.\nJe kan een standaard tekst uit de lijst kiezen, of zelf een (korte) tekst invoeren.");
    gtk_widget_show (label3);
    gtk_box_pack_start (GTK_BOX (vbox2), label3, FALSE, FALSE, 0);
    gtk_misc_set_padding (GTK_MISC (label3), 4, 4);

    modje->comboReason = gtk_combo_box_entry_new_text ();
    gtk_widget_show (modje->comboReason);
    gtk_box_pack_start (GTK_BOX (vbox2), modje->comboReason, TRUE, TRUE, 5);
    gtk_combo_box_append_text (GTK_COMBO_BOX (modje->comboReason), "Post is reeds aangemeld/gespot op FTD");
    gtk_combo_box_append_text (GTK_COMBO_BOX (modje->comboReason), "Post is een animopost");
    gtk_combo_box_append_text (GTK_COMBO_BOX (modje->comboReason), "Post is in verkeerde (sub)categorie aangemeld");
    gtk_combo_box_append_text (GTK_COMBO_BOX (modje->comboReason), "Post bevat informatie die legaliteit FTD bedreigt");
    gtk_combo_box_append_text (GTK_COMBO_BOX (modje->comboReason), "Post bevat hinderlijk onjuiste postinformatie");

    label2 = gtk_label_new ("");
    gtk_widget_show (label2);
    gtk_frame_set_label_widget (GTK_FRAME (frame1), label2);
    gtk_label_set_use_markup (GTK_LABEL (label2), TRUE);

    hbuttonbox1 = gtk_hbutton_box_new ();
    gtk_widget_show (hbuttonbox1);
    gtk_box_pack_start (GTK_BOX (vbox1), hbuttonbox1, TRUE, TRUE, 0);

    btnWaarschuw = gtk_button_new_with_mnemonic ("Waarschuw een modje");
    gtk_widget_show (btnWaarschuw);
    gtk_container_add (GTK_CONTAINER (hbuttonbox1), btnWaarschuw);
    gtk_container_set_border_width (GTK_CONTAINER (btnWaarschuw), 8);
    GTK_WIDGET_SET_FLAGS (btnWaarschuw, GTK_CAN_DEFAULT);

    gtk_widget_show_all(modje->window);

    g_signal_connect(G_OBJECT(btnWaarschuw), "clicked",
                     G_CALLBACK(ftd_gui_modje_waarschuw_clicked), NULL);

    g_signal_connect(G_OBJECT(modje->window), "destroy",
                     G_CALLBACK(ftd_gui_modje_callback_afsluiten), NULL);

    return modje;
}

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

static gboolean
ftd_gui_modje_waarschuw_clicked(GtkWidget *caller, gpointer data)
{
    char *msg;
    int count = 0;
    FtdErrorCode retval;
    char *errmsg = NULL;

    msg = gtk_combo_box_get_active_text(GTK_COMBO_BOX(gui->modje->comboReason));
    if (!msg || strlen(msg) == 0) {
        ftd_gui_dialog_show(NULL, "Je dient eerst een reden opgeven alvorens je een modje kan inschakelen", gui->modje->window, FALSE);

        if (msg) {
            g_free(msg);
        }

        return FALSE;
    }

    retval = ftd_modje_send(gui->modje->post_id, gui->modje->forum_id, msg, &count, &errmsg);

    g_free(msg);

    if (retval != ERROR_NONE) {
        ftd_gui_errhandler(gui->modje->window, FTD_ONDERDEEL_MODJE, retval, errmsg);
        if (errmsg) {
            g_free(errmsg);
        }

        ftd_gui_modje_callback_afsluiten(NULL, NULL);

        return FALSE;
    }

    msg = g_strdup_printf("Bedankt, jouw waarschuwing is aangekomen en een modje is op pad gestuurd.\n\nEr zijn inmiddels %i klacht(en) binnengekomen.", count);

    ftd_gui_dialog_show(NULL, msg, gui->modje->window, FALSE);

    g_free(msg);

    ftd_gui_modje_callback_afsluiten(NULL, NULL);

    return FALSE;
}
