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

static gboolean ftd_gui_profile_destroy(GtkWidget *caller, gpointer data);
static gboolean ftd_gui_profile_send_mail(GtkWidget *caller, gpointer data);

struct _profile_data
{
    char *username;
};

static char *
generate_last_seen_time(time_t time)
{
    int days;
    int hours;
    int minutes;

    if (time <= 0) {
        return g_strdup("onbekend");
    }

    days = time / 86400;
    hours = (time - (days * 86400)) / 3600;
    minutes = (time - (days * 86400) - (hours * 3600)) / 60;

    return g_strdup_printf("%i dagen, %i uur en %i minuten geleden.", days, hours, minutes);
}

void
ftd_gui_profile_show(const char *username)
{
    GtkWidget *window1;
    GtkWidget *table1;
    GtkWidget *label1;
    GtkWidget *imgAvatar;
    GtkWidget *lblWebsite;
    GtkWidget *lblICQ;
    GtkWidget *lblHobbies;
    GtkWidget *lblISP;
    GtkWidget *lblUsenet;
    GtkWidget *lblExtraInfo;
    GtkWidget *btnSendFTDMail;
    GtkWidget *alignment1;
    GtkWidget *hbox1;
    GtkWidget *image2;
    GtkWidget *label33;
    GtkWidget *label2;
    GtkWidget *label3;
    GtkWidget *label5;
    GtkWidget *label7;
    GtkWidget *label9;
    GtkWidget *label11;
    GtkWidget *label13;
    GtkWidget *label15;
    GtkWidget *label17;
    GtkWidget *label19;
    GtkWidget *label21;
    GtkWidget *label23;
    GtkWidget *label25;
    GtkWidget *label27;
    GtkWidget *label29;
    GtkWidget *label31;
    GtkWidget *lblLastSeen;
    GtkWidget *lblRegistered;
    GtkWidget *lblNumPosts;
    GtkWidget *lblKudosSpent;
    GtkWidget *lblKudosReceived;
    GtkWidget *lblRatio;
    GtkWidget *lblKudosBalance;
    GtkWidget *lblStatus;
    GtkWidget *lblEMail;
    char *title;
    FtdProfile *profile;
    char *errmsg;
    FtdErrorCode retval;
    struct _profile_data *data;
    char *filename = NULL;
    double given;
    double received;
    double ratio;

    // Haal de profielgegevens op
    retval = ftd_profile_get(username, &profile, &errmsg);
    if (retval != ERROR_NONE) {
        ftd_gui_errhandler(gui->window, FTD_ONDERDEEL_PROFILE, retval, errmsg);
        if (errmsg) {
            g_free(errmsg);
        }

        return;
    }

    data = g_malloc0(sizeof(struct _profile_data));
    data->username = g_strdup(username);

    window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window1), "Gebruikersprofiel");
    gtk_window_set_default_size(GTK_WINDOW(window1), 450, -1);

    table1 = gtk_table_new (18, 2, FALSE);
    gtk_widget_show (table1);
    gtk_container_add (GTK_CONTAINER (window1), table1);

    title = g_strdup_printf("<span size='x-large'><b>%s</b></span>", username);
    label1 = gtk_label_new (title);
    g_free(title);
    gtk_widget_show (label1);
    gtk_table_attach (GTK_TABLE (table1), label1, 0, 2, 0, 1,
                        (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                        (GtkAttachOptions) (0), 5, 10);
    gtk_label_set_use_markup (GTK_LABEL (label1), TRUE);
    gtk_label_set_justify (GTK_LABEL (label1), GTK_JUSTIFY_CENTER);
    gtk_misc_set_alignment (GTK_MISC (label1), 0, 0.5);

    if (profile->avatar && strlen(profile->avatar) > 0) {
        retval = ftd_http_curl_execute(profile->avatar, NULL, FALSE, NULL, FALSE, &filename, TRUE);
        if (retval != ERROR_NONE) {
            if (errmsg) {
                g_free(errmsg);
            }

            ftd_gui_dialog_show(NULL, "Ophalen van de avatar is mislukt", window1, FALSE);
            imgAvatar = gtk_label_new("Ophalen van avatar mislukt");
        } else {
            imgAvatar = gtk_image_new_from_file(filename);
            unlink(filename);
        }
    } else {
        imgAvatar = gtk_label_new("Gebruiker heeft geen avatar ingesteld");
    }
    gtk_widget_show (imgAvatar);
    gtk_table_attach (GTK_TABLE (table1), imgAvatar, 1, 2, 1, 2,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (imgAvatar), 0, 0.5);

    lblWebsite = gtk_label_new (profile->site);
    gtk_widget_show (lblWebsite);
    gtk_table_attach (GTK_TABLE (table1), lblWebsite, 1, 2, 11, 12,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (lblWebsite), 0, 0.5);

    lblICQ = gtk_label_new (profile->im);
    gtk_widget_show (lblICQ);
    gtk_table_attach (GTK_TABLE (table1), lblICQ, 1, 2, 12, 13,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (lblICQ), 0, 0.5);

    lblHobbies = gtk_label_new (profile->hobby);
    gtk_widget_show (lblHobbies);
    gtk_table_attach (GTK_TABLE (table1), lblHobbies, 1, 2, 13, 14,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (lblHobbies), 0, 0.5);

    lblISP = gtk_label_new (profile->isp);
    gtk_widget_show (lblISP);
    gtk_table_attach (GTK_TABLE (table1), lblISP, 1, 2, 14, 15,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (lblISP), 0, 0.5);

    lblUsenet = gtk_label_new (profile->ns);
    gtk_widget_show (lblUsenet);
    gtk_table_attach (GTK_TABLE (table1), lblUsenet, 1, 2, 15, 16,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (lblUsenet), 0, 0.5);

    lblExtraInfo = gtk_label_new (profile->info);
    gtk_widget_show (lblExtraInfo);
    gtk_table_attach (GTK_TABLE (table1), lblExtraInfo, 1, 2, 16, 17,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment (GTK_MISC (lblExtraInfo), 0, 0.5);

    btnSendFTDMail = gtk_button_new ();
    gtk_widget_show (btnSendFTDMail);
    gtk_table_attach (GTK_TABLE (table1), btnSendFTDMail, 0, 2, 17, 18,
                        (GtkAttachOptions) (GTK_SHRINK),
                        (GtkAttachOptions) (0), 0, 5);

    alignment1 = gtk_alignment_new (0.5, 0.5, 0, 0);
    gtk_widget_show (alignment1);
    gtk_container_add (GTK_CONTAINER (btnSendFTDMail), alignment1);

    hbox1 = gtk_hbox_new (FALSE, 2);
    gtk_widget_show (hbox1);
    gtk_container_add (GTK_CONTAINER (alignment1), hbox1);

    image2 = gtk_image_new_from_stock ("gtk-new", GTK_ICON_SIZE_BUTTON);
    gtk_widget_show (image2);
    gtk_box_pack_start (GTK_BOX (hbox1), image2, FALSE, FALSE, 0);

    label33 = gtk_label_new_with_mnemonic ("Stuur FTD Mail");
    gtk_widget_show (label33);
    gtk_box_pack_start (GTK_BOX (hbox1), label33, FALSE, FALSE, 0);

    label2 = gtk_label_new ("Avatar :");
    gtk_widget_show (label2);
    gtk_table_attach (GTK_TABLE (table1), label2, 0, 1, 1, 2,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label2), 0, 0.5);

    label3 = gtk_label_new ("Laatst gesignaleerd :");
    gtk_widget_show (label3);
    gtk_table_attach (GTK_TABLE (table1), label3, 0, 1, 2, 3,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);

    label5 = gtk_label_new ("Geregistreerd op :");
    gtk_widget_show (label5);
    gtk_table_attach (GTK_TABLE (table1), label5, 0, 1, 3, 4,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label5), 0, 0.5);

    label7 = gtk_label_new ("Aantal posts :");
    gtk_widget_show (label7);
    gtk_table_attach (GTK_TABLE (table1), label7, 0, 1, 4, 5,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label7), 0, 0.5);

    label9 = gtk_label_new ("Uitgegeven :");
    gtk_widget_show (label9);
    gtk_table_attach (GTK_TABLE (table1), label9, 0, 1, 5, 6,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label9), 0, 0.5);

    label11 = gtk_label_new ("Ontvangen :");
    gtk_widget_show (label11);
    gtk_table_attach (GTK_TABLE (table1), label11, 0, 1, 6, 7,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label11), 0, 0.5);

    label13 = gtk_label_new ("Ratio :");
    gtk_widget_show (label13);
    gtk_table_attach (GTK_TABLE (table1), label13, 0, 1, 7, 8,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label13), 0, 0.5);

    label15 = gtk_label_new ("Kudosbalans :");
    gtk_widget_show (label15);
    gtk_table_attach (GTK_TABLE (table1), label15, 0, 1, 8, 9,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label15), 0, 0.5);

    label17 = gtk_label_new ("Status :");
    gtk_widget_show (label17);
    gtk_table_attach (GTK_TABLE (table1), label17, 0, 1, 9, 10,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label17), 0, 0.5);

    label19 = gtk_label_new ("EMail :");
    gtk_widget_show (label19);
    gtk_table_attach (GTK_TABLE (table1), label19, 0, 1, 10, 11,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label19), 0, 0.5);

    label21 = gtk_label_new ("Website :");
    gtk_widget_show (label21);
    gtk_table_attach (GTK_TABLE (table1), label21, 0, 1, 11, 12,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label21), 0, 0.5);

    label23 = gtk_label_new ("ICQ / MSN :");
    gtk_widget_show (label23);
    gtk_table_attach (GTK_TABLE (table1), label23, 0, 1, 12, 13,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label23), 0, 0.5);

    label25 = gtk_label_new ("Interesses :");
    gtk_widget_show (label25);
    gtk_table_attach (GTK_TABLE (table1), label25, 0, 1, 13, 14,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label25), 0, 0.5);

    label27 = gtk_label_new ("Internetprovider :");
    gtk_widget_show (label27);
    gtk_table_attach (GTK_TABLE (table1), label27, 0, 1, 14, 15,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label27), 0, 0.5);

    label29 = gtk_label_new ("Nieuwsserver(s) :");
    gtk_widget_show (label29);
    gtk_table_attach (GTK_TABLE (table1), label29, 0, 1, 15, 16,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label29), 0, 0.5);

    label31 = gtk_label_new ("Extra Info :");
    gtk_widget_show (label31);
    gtk_table_attach (GTK_TABLE (table1), label31, 0, 1, 16, 17,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (label31), 0, 0.5);

    title = generate_last_seen_time(profile->status);
    lblLastSeen = gtk_label_new (title);
    g_free(title);
    gtk_widget_show (lblLastSeen);
    gtk_table_attach (GTK_TABLE (table1), lblLastSeen, 1, 2, 2, 3,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (lblLastSeen), 0, 0.5);

    title = ftd_string_date(profile->stamp);
    lblRegistered = gtk_label_new (title);
    g_free(title);
    gtk_widget_show (lblRegistered);
    gtk_table_attach (GTK_TABLE (table1), lblRegistered, 1, 2, 3, 4,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (lblRegistered), 0, 0.5);

    if (profile->numSpots > 0) {
        title = g_strdup_printf("%i (+%i spots)", profile->numPosts, profile->numSpots);
    } else {
        title = g_strdup_printf("%i", profile->numPosts);
    }
    lblNumPosts = gtk_label_new (title);
    g_free(title);
    gtk_widget_show (lblNumPosts);
    gtk_table_attach (GTK_TABLE (table1), lblNumPosts, 1, 2, 4, 5,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (lblNumPosts), 0, 0.5);

    title = g_strdup_printf("%i", profile->given);
    lblKudosSpent = gtk_label_new (title);
    g_free(title);
    gtk_widget_show (lblKudosSpent);
    gtk_table_attach (GTK_TABLE (table1), lblKudosSpent, 1, 2, 5, 6,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (lblKudosSpent), 0, 0.5);

    title = g_strdup_printf("%i", profile->received);
    lblKudosReceived = gtk_label_new (title);
    g_free(title);
    gtk_widget_show (lblKudosReceived);
    gtk_table_attach (GTK_TABLE (table1), lblKudosReceived, 1, 2, 6, 7,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (lblKudosReceived), 0, 0.5);

    given = profile->given;
    received = profile->received;
    ratio = (given / received) * 100;

    title = g_strdup_printf("%.f%%", ratio);
    lblRatio = gtk_label_new (title);
    g_free(title);
    gtk_widget_show (lblRatio);
    gtk_table_attach (GTK_TABLE (table1), lblRatio, 1, 2, 7, 8,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (lblRatio), 0, 0.5);

    title = g_strdup_printf("%i", profile->kudos);
    lblKudosBalance = gtk_label_new (title);
    g_free(title);
    gtk_widget_show (lblKudosBalance);
    gtk_table_attach (GTK_TABLE (table1), lblKudosBalance, 1, 2, 8, 9,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_misc_set_alignment (GTK_MISC (lblKudosBalance), 0, 0.5);

    if (profile->pstat == 0) {
        lblStatus = gtk_label_new ("<i>niet geauthenticeerd</i>");
    } else {
        lblStatus = gtk_label_new ("<i>geauthenticeerd</i>");
    }
    gtk_widget_show (lblStatus);
    gtk_table_attach (GTK_TABLE (table1), lblStatus, 1, 2, 9, 10,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_label_set_use_markup (GTK_LABEL (lblStatus), TRUE);
    gtk_misc_set_alignment (GTK_MISC (lblStatus), 0, 0.5);

    if (!strcmp(profile->email, "N/A")) {
        lblEMail = gtk_label_new ("<i>Verborgen</i>");
    } else {
        lblEMail = gtk_label_new (profile->email);
    }
    gtk_widget_show (lblEMail);
    gtk_table_attach (GTK_TABLE (table1), lblEMail, 1, 2, 10, 11,
                        (GtkAttachOptions) (GTK_FILL),
                        (GtkAttachOptions) (0), 0, 2);
    gtk_label_set_use_markup (GTK_LABEL (lblEMail), TRUE);
    gtk_misc_set_alignment (GTK_MISC (lblEMail), 0, 0.5);

    g_signal_connect(btnSendFTDMail, "clicked", G_CALLBACK(ftd_gui_profile_send_mail), data->username);
    g_signal_connect(window1, "destroy", G_CALLBACK(ftd_gui_profile_destroy), data);

    gtk_widget_show(window1);

    ftd_profile_free(profile);
}

static gboolean
ftd_gui_profile_destroy(GtkWidget *caller, gpointer data)
{
    struct _profile_data *profile_data = data;

    g_free(profile_data->username);
    g_free(profile_data);

    return FALSE;
}

static gboolean
ftd_gui_profile_send_mail(GtkWidget *caller, gpointer data)
{
    const char *username = data;

    ftd_gui_message_new(username, "", NULL, 0, NULL);

    return FALSE;
}
