/* ====================================================================
 * 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 <ctype.h>
#include "ftd_gui_config.h"
#include "ftd_gui_signup.h"

static void ftd_gui_config_user_callback_ratingScale(GtkWidget *caller, gpointer data);

void
ftd_gui_config_user_new(FtdGuiConfig *config)
{
    GtkWidget   *frame4;
    GtkWidget   *label6;
    GtkWidget   *label17;
    GtkWidget   *label18;
    GtkWidget   *frame5;
    GtkWidget   *vbox3;
    GtkWidget   *vbox4;
    GtkWidget   *label10;
    GtkWidget   *label7;
    GtkWidget   *table;
    GtkWidget   *table2;
    GtkWidget   *table3;
    GtkWidget   *align;
    GtkWidget   *align2;
    GtkWidget   *align3;
    GtkWidget   *align4;
    GtkWidget   *align5;
    GtkWidget   *label8;
    GtkWidget   *hbox1;
    GtkWidget   *btnRegistreer;
    FtdGuiConfigUser *user;
    double val;

    g_assert(config);

    user = g_malloc0(sizeof(FtdGuiConfigUser));
    config->user = user;

    user->widget = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (user->widget); /* vbox2 */
    gtk_container_set_border_width (GTK_CONTAINER (user->widget), 5);

    frame4 = gtk_frame_new (NULL);
    gtk_widget_show (frame4);
    gtk_box_pack_start (GTK_BOX (user->widget), frame4, FALSE, FALSE, 5);

    label6 = gtk_label_new ("Account informatie");
    gtk_widget_show (label6);
    gtk_frame_set_label_widget (GTK_FRAME (frame4), label6);
    gtk_label_set_justify (GTK_LABEL (label6), GTK_JUSTIFY_LEFT);

    table = gtk_table_new(3,2,FALSE);
    gtk_widget_show(table);
    gtk_container_add (GTK_CONTAINER (frame4), table);

    align = gtk_alignment_new(0,1,0,0);
    label17 = gtk_label_new ("Gebruikersnaam:");
    gtk_widget_show (label17);
    gtk_container_add (GTK_CONTAINER (align), label17);
    gtk_widget_show (align);
    gtk_table_attach(GTK_TABLE(table), align, 0,1,0,1,
                     (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) NULL, 3, 3);

    user->username = gtk_entry_new();
    gtk_widget_show(user->username);
    gtk_table_attach(GTK_TABLE(table), user->username, 1,2,0,1,
                     (GtkAttachOptions) NULL, (GtkAttachOptions) NULL, 3, 3);
    if (ftd_config_get()->username) {
        gtk_entry_set_text(GTK_ENTRY(user->username), ftd_string_value(ftd_config_get()->username));
    }

    align2 = gtk_alignment_new(0,1,0,0);
    label18 = gtk_label_new ("Wachtwoord:");
    gtk_widget_show (label18);
    gtk_container_add (GTK_CONTAINER (align2), label18);
    gtk_widget_show (align2);
    gtk_table_attach(GTK_TABLE(table), align2, 0,1,1,2,
                     (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) NULL, 3, 3);

    user->password = gtk_entry_new ();
    gtk_entry_set_visibility(GTK_ENTRY(user->password), FALSE);
    gtk_widget_show (user->password);
    if (ftd_config_get()->password) {
        gtk_entry_set_text(GTK_ENTRY(user->password), ftd_string_value(ftd_config_get()->password));
    }
    gtk_table_attach(GTK_TABLE(table), user->password, 1,2,1,2,
                     (GtkAttachOptions) NULL, (GtkAttachOptions) NULL, 3, 3);

    btnRegistreer = gtk_button_new();
    gtk_button_set_label(GTK_BUTTON(btnRegistreer), "Registreer nieuw FTD account");
    gtk_widget_show(btnRegistreer);
    gtk_table_attach(GTK_TABLE(table), btnRegistreer, 1, 3, 2, 3,
                     (GtkAttachOptions) NULL, (GtkAttachOptions) NULL, 3, 3);

    frame5 = gtk_frame_new (NULL);
    gtk_widget_show (frame5);
    gtk_box_pack_start (GTK_BOX (user->widget), frame5, FALSE, FALSE, 5);

    label7 = gtk_label_new ("Verbinding");
    gtk_widget_show (label7);
    gtk_frame_set_label_widget (GTK_FRAME (frame5), label7);
    gtk_label_set_justify (GTK_LABEL (label7), GTK_JUSTIFY_LEFT);

    vbox4 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox4);
    gtk_container_add (GTK_CONTAINER (frame5), vbox4);

    table2 = gtk_table_new(3, 1, FALSE);
    gtk_widget_show(table2);
    gtk_box_pack_start (GTK_BOX (vbox4), table2, FALSE, FALSE, 0);

    align3 = gtk_alignment_new(0,1,0,0);
    label10 = gtk_label_new ("Proxy server/poort:");
    gtk_widget_show (label10);
    gtk_container_add (GTK_CONTAINER (align3), label10);
    gtk_widget_show (align3);
    gtk_table_attach(GTK_TABLE(table2), align3, 0,1,0,1,
                     (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) NULL, 3, 3);

    user->proxyHost = gtk_entry_new ();
    gtk_widget_show (user->proxyHost);
    if (ftd_config_get()->proxyHost) {
        gtk_entry_set_text(GTK_ENTRY(user->proxyHost), ftd_string_value(ftd_config_get()->proxyHost));
    }
    gtk_table_attach(GTK_TABLE(table2), user->proxyHost, 1,2,0,1,
                     (GtkAttachOptions) NULL, (GtkAttachOptions) NULL, 3, 3);

    user->proxyPort = gtk_entry_new ();
    gtk_widget_show (user->proxyPort);
    gtk_entry_set_max_length (GTK_ENTRY (user->proxyPort), 5);
    gtk_entry_set_width_chars (GTK_ENTRY (user->proxyPort), 6);

    if (ftd_config_get()->proxyPort) {
        gtk_entry_set_text(GTK_ENTRY(user->proxyPort), ftd_string_value(ftd_config_get()->proxyPort));
    }
    gtk_table_attach(GTK_TABLE(table2), user->proxyPort, 2,3,0,1,
                     (GtkAttachOptions) NULL, (GtkAttachOptions) NULL, 3, 3);

    user->proxyAuth = gtk_check_button_new_with_mnemonic ("Mijn proxyserver vereist authentificatie");
    gtk_widget_show (user->proxyAuth);
    gtk_box_pack_start (GTK_BOX (vbox4), user->proxyAuth, FALSE, FALSE, 0);

    table3 = gtk_table_new(2, 2, FALSE);
    gtk_widget_show(table3);
    gtk_box_pack_start (GTK_BOX (vbox4), table3, FALSE, FALSE, 0);

    align4 = gtk_alignment_new(0, 1, 0, 0);
    user->lblProxyUser = gtk_label_new ("Gebruikersnaam:");
    gtk_widget_show (user->lblProxyUser);
    gtk_container_add (GTK_CONTAINER (align4), user->lblProxyUser);
    gtk_widget_show(align4);
    gtk_table_attach(GTK_TABLE(table3), align4, 0,1,0,1,
                     (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) NULL, 3, 3);

    user->proxyUser = gtk_entry_new ();
    gtk_widget_show (user->proxyUser);
    gtk_table_attach(GTK_TABLE(table3), user->proxyUser, 1,2,0,1,
                     (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) NULL, 3, 3);

    align5 = gtk_alignment_new(0, 1, 0, 0);
    user->lblProxyPass = gtk_label_new ("Wachtwoord:");
    gtk_widget_show (user->lblProxyPass);
    gtk_container_add (GTK_CONTAINER (align5), user->lblProxyPass);
    gtk_widget_show (align5);
    gtk_table_attach(GTK_TABLE(table3), align5, 0,1,1,2,
                     (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) NULL, 3, 3);

    user->proxyPass = gtk_entry_new ();
    gtk_widget_show (user->proxyPass);
    gtk_table_attach(GTK_TABLE(table3), user->proxyPass, 1,2,1,2,
                     (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) NULL, 3, 3);

    vbox3 = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (vbox3);
    gtk_box_pack_start (GTK_BOX (user->widget), vbox3, FALSE, FALSE, 0);

    hbox1 = gtk_hbox_new(FALSE, 0);
    gtk_widget_show(hbox1);
    gtk_box_pack_start(GTK_BOX(user->widget), hbox1, FALSE, FALSE, 0);

    label8 = gtk_label_new("Verwijder posts na X dagen : ");
    gtk_widget_show(label8);
    gtk_box_pack_start(GTK_BOX(hbox1), label8, TRUE, TRUE, 0);

    user->comboVerwijderPostsNaXDagen = gtk_combo_box_new_text();
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE1);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE2);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE3);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE4);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE5);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE6);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE7);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE8);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE9);
    gtk_combo_box_append_text(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), CACHE_OPTIE10);
    gtk_combo_box_set_active(GTK_COMBO_BOX(user->comboVerwijderPostsNaXDagen), ftd_config_get()->verwijderPostsNaXDagen);

    gtk_widget_show(user->comboVerwijderPostsNaXDagen);
    gtk_box_pack_end(GTK_BOX(hbox1), user->comboVerwijderPostsNaXDagen, TRUE, TRUE, 0);

    hbox1 = gtk_hbox_new(FALSE, 5);
    gtk_widget_show(hbox1);
    gtk_box_pack_start(GTK_BOX(user->widget), hbox1, FALSE, FALSE, 5);

    user->lblRatingScale = gtk_label_new("Ratingschaal : ");
    gtk_widget_show(user->lblRatingScale);
    gtk_box_pack_start(GTK_BOX(hbox1), user->lblRatingScale, TRUE, TRUE, 0);

    user->ratingScale = gtk_hscrollbar_new(NULL);
    gtk_range_set_range(GTK_RANGE(user->ratingScale), 5.0, 200.0);
    gtk_widget_show(user->ratingScale);
    gtk_box_pack_end(GTK_BOX(hbox1), user->ratingScale, TRUE, TRUE, 0);

    g_signal_connect (G_OBJECT (user->proxyAuth), "toggled", G_CALLBACK (ftd_gui_config_user_callback_proxyAuth), config);
    g_signal_connect (G_OBJECT (user->proxyPort), "insert_text", G_CALLBACK (ftd_gui_config_user_callback_proxyPort), config);
    g_signal_connect (G_OBJECT (btnRegistreer), "clicked", G_CALLBACK (ftd_gui_config_user_callback_registreer), config);

    g_signal_connect (G_OBJECT (user->ratingScale), "value-changed", G_CALLBACK(ftd_gui_config_user_callback_ratingScale), config);

    val = ftd_config_get()->ratingScale;
    gtk_range_set_value(GTK_RANGE(user->ratingScale), val);
}

void
ftd_gui_config_user_free(FtdGuiConfig *config)
{
    g_assert(config);
    g_assert(config->user);

    g_free(config->user);
    config->user = NULL;
}

static void
ftd_gui_config_user_callback_ratingScale(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;
    char *txt;

    g_assert(config);

    txt = g_strdup_printf("Ratingschaal : (%.f)", gtk_range_get_value(GTK_RANGE(caller)));
    gtk_label_set_text(GTK_LABEL(config->user->lblRatingScale), txt);
    g_free(txt);
}

void
ftd_gui_config_user_proxyAuth_controle(FtdGuiConfig *config, gboolean setup)
{
    gboolean state;
    g_assert(config);
    g_assert(config->user);

    if (setup) {
        /* Dit is de eerste keer dat we hier zijn */
        if (ftd_config_get()->proxyUser || ftd_config_get()->proxyPassword) {
            /* We moeten de invoervelden AAN zetten en vullen met de config gegeven(s) */
            gtk_widget_set_sensitive(config->user->proxyUser, TRUE);
            gtk_widget_set_sensitive(config->user->proxyPass, TRUE);
            gtk_widget_set_sensitive(config->user->lblProxyUser, TRUE);
            gtk_widget_set_sensitive(config->user->lblProxyPass, TRUE);
            gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(config->user->proxyAuth), TRUE);
            gtk_widget_grab_focus (config->user->proxyUser);
            if (ftd_config_get()->proxyUser) {
                gtk_entry_set_text(GTK_ENTRY(config->user->proxyUser), ftd_string_value(ftd_config_get()->proxyUser));
            }
            if (ftd_config_get()->proxyPassword) {
                gtk_entry_set_text(GTK_ENTRY(config->user->proxyPass), ftd_string_value(ftd_config_get()->proxyPassword));
            }
        } else {
            /* Invoervelden UIT, dit is de eerste keer dat we hier zijn dus tekstvelden zijn nog leeg */
            gtk_widget_set_sensitive(config->user->proxyUser, FALSE);
            gtk_widget_set_sensitive(config->user->proxyPass, FALSE);
            gtk_widget_set_sensitive(config->user->lblProxyUser, FALSE);
            gtk_widget_set_sensitive(config->user->lblProxyPass, FALSE);
        }
    } else {
        /* We zijn hier al eens vaker geweest, kijk nu ALLEEN naar de status van het knopje */
        state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(config->user->proxyAuth));
        if (state) {
            /* invoervelden staan gedisabled, zet ze terug aan */
            gtk_widget_set_sensitive(config->user->proxyUser, TRUE);
            gtk_widget_set_sensitive(config->user->proxyPass, TRUE);
            gtk_widget_set_sensitive(config->user->lblProxyUser, TRUE);
            gtk_widget_set_sensitive(config->user->lblProxyPass, TRUE);
            gtk_widget_grab_focus(config->user->proxyUser);
        } else {
            /* Invoervelden staan enabled, zet ze uit en maak de tekstvelden leeg */
            if (ftd_config_get()->proxyUser) {
                ftd_string_free(ftd_config_get()->proxyUser);
            }

            if (ftd_config_get()->proxyPassword) {
                ftd_string_free(ftd_config_get()->proxyPassword);
            }

            gtk_widget_set_sensitive(config->user->proxyUser, FALSE);
            gtk_widget_set_sensitive(config->user->proxyPass, FALSE);
            gtk_widget_set_sensitive(config->user->lblProxyUser, FALSE);
            gtk_widget_set_sensitive(config->user->lblProxyPass, FALSE);
            gtk_entry_set_text(GTK_ENTRY(config->user->proxyUser), "");
            gtk_entry_set_text(GTK_ENTRY(config->user->proxyPass), "");
        }
    }
}

void
ftd_gui_config_user_callback_proxyAuth(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;

    g_assert(caller);
    g_assert(config);

    ftd_gui_config_user_proxyAuth_controle (config, FALSE);
}

void
ftd_gui_config_user_callback_proxyPort(GtkWidget   *caller,
                                       const gchar *text,
                                       gint         length,
                                       gint         *position,
                                       gpointer     data)
{
    int i, count = 0;
    gchar *result;
    GtkEditable *editable;

    g_assert(caller);

    result = g_new (gchar, length);

    editable = GTK_EDITABLE(caller);

    for (i=0; i < length; i++) {
        if (!isdigit(text[i]))
            continue;
        result[count++] = text[i];
    }

    if (count > 0) {
        g_signal_handlers_block_by_func (G_OBJECT (editable),
                                         G_CALLBACK (ftd_gui_config_user_callback_proxyPort),
                                         data);
        gtk_editable_insert_text (editable, result, count, position);
        g_signal_handlers_unblock_by_func (G_OBJECT (editable),
                                           G_CALLBACK (ftd_gui_config_user_callback_proxyPort),
                                           data);
    }

    g_signal_stop_emission_by_name (G_OBJECT (editable), "insert_text");

    g_free (result);
}

gboolean
ftd_gui_config_user_callback_registreer(GtkWidget *caller, gpointer data)
{
    FtdGuiConfig *config = (FtdGuiConfig *) data;

    g_assert(config);

    if (ftd_config_get()->proxyHost) {
        ftd_string_free (ftd_config_get()->proxyHost);
    }
    if (ftd_config_get()->proxyPort) {
        ftd_string_free (ftd_config_get()->proxyPort);
    }
    if (ftd_config_get()->proxyUser) {
        ftd_string_free (ftd_config_get()->proxyUser);
    }
    if (ftd_config_get()->proxyPassword) {
        ftd_string_free (ftd_config_get()->proxyPassword);
    }

    ftd_config_get()->proxyHost = strlen(gtk_entry_get_text(GTK_ENTRY(config->user->proxyHost))) > 0 ?
                                ftd_string_new(gtk_entry_get_text(GTK_ENTRY(config->user->proxyHost))) : NULL;
    ftd_config_get()->proxyPort = strlen(gtk_entry_get_text(GTK_ENTRY(config->user->proxyPort))) > 0 ?
                                ftd_string_new(gtk_entry_get_text(GTK_ENTRY(config->user->proxyPort))) : NULL;
    ftd_config_get()->proxyUser = strlen(gtk_entry_get_text(GTK_ENTRY(config->user->proxyUser))) > 0 ?
                                ftd_string_new(gtk_entry_get_text(GTK_ENTRY(config->user->proxyUser))) : NULL;
    ftd_config_get()->proxyPassword = strlen(gtk_entry_get_text(GTK_ENTRY(config->user->proxyPass))) > 0 ?
                                ftd_string_new(gtk_entry_get_text(GTK_ENTRY(config->user->proxyPass))) : NULL;

    ftd_gui_signup_new(config);

    return FALSE;
}
