#include<gtk/gtk.h>
#include<glib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "mmyth_uisettings.h"

#include <gmyth/gmyth_backendinfo.h>

static GtkWidget *settings_dialog;
static GtkWidget *entry_hostname;
static GtkWidget *entry_dbname;
static GtkWidget *entry_username;
static GtkWidget *entry_passwd;
static GtkWidget *entry_port;

static void     settings_dialog_update_data(void);
static GtkWidget *add_entry_to_table(GtkWidget * table,
                                     const gchar * init_str,
                                     guint pos_left, guint pos_right,
                                     guint pos_top, guint pos_bottom);
static GtkWidget *add_label_to_table(GtkWidget * table, const gchar * str,
                                     guint pos_left, guint pos_right,
                                     guint pos_top, guint pos_bottom);

static GMythBackendInfo *backend_info = NULL;

gboolean
mmyth_uisettings_run(GtkWindow * main_window)
{

    GtkWidget      *settings_table;
    GtkWidget      *label_hostname,
                   *label_dbname;
    GtkWidget      *label_username,
                   *label_passwd,
                   *label_port;

    backend_info = gmyth_backend_info_new_full("localhost", "mythtv",
                                               "mythtv", "mythconverg",
                                               6543);

    settings_dialog = gtk_dialog_new_with_buttons("Settings",
                                                  main_window,
                                                  GTK_DIALOG_DESTROY_WITH_PARENT,
                                                  GTK_STOCK_OK,
                                                  GTK_RESPONSE_ACCEPT,
                                                  GTK_STOCK_CANCEL,
                                                  GTK_RESPONSE_REJECT,
                                                  NULL);

    gtk_widget_set_size_request(settings_dialog, 400, 244);

    /*
     * scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
     * gtk_widget_show (scrolledwindow1); gtk_container_add (GTK_CONTAINER 
     * (window1), scrolledwindow1); gtk_scrolled_window_set_policy
     * (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_AUTOMATIC,
     * GTK_POLICY_NEVER);
     * 
     * viewport1 = gtk_viewport_new (NULL, NULL); gtk_widget_show
     * (viewport1); gtk_container_add (GTK_CONTAINER (scrolledwindow1),
     * viewport1); gtk_viewport_set_shadow_type (GTK_VIEWPORT (viewport1), 
     * GTK_SHADOW_NONE); 
     */

    // Creates the table and attach it to the settings dialog
    settings_table = gtk_table_new(5, 2, FALSE);
    gtk_widget_show(settings_table);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(settings_dialog)->vbox),
                       settings_table, FALSE, TRUE, 0);
    gtk_container_set_border_width(GTK_CONTAINER(settings_table), 3);
    gtk_table_set_row_spacings(GTK_TABLE(settings_table), 5);
    gtk_table_set_col_spacings(GTK_TABLE(settings_table), 10);

    label_hostname =
        add_label_to_table(settings_table, "Host Name:", 0, 1, 0, 1);
    label_dbname =
        add_label_to_table(settings_table, "Database Name:", 0, 1, 1, 2);
    label_username =
        add_label_to_table(settings_table, "Username:", 0, 1, 2, 3);
    label_passwd =
        add_label_to_table(settings_table, "Password:", 0, 1, 3, 4);
    label_port =
        add_label_to_table(settings_table, "Server port:", 0, 1, 4, 5);

    entry_hostname = add_entry_to_table(settings_table,
                                        gmyth_backend_info_get_hostname
                                        (backend_info), 1, 2, 0, 1);
    entry_dbname =
        add_entry_to_table(settings_table,
                           gmyth_backend_info_get_db_name(backend_info), 1,
                           2, 1, 2);
    entry_username =
        add_entry_to_table(settings_table,
                           gmyth_backend_info_get_username(backend_info),
                           1, 2, 2, 3);
    entry_passwd =
        add_entry_to_table(settings_table,
                           gmyth_backend_info_get_password(backend_info),
                           1, 2, 3, 4);

    entry_port =
        add_entry_to_table(settings_table,
                           g_strdup_printf("%d",
                                           gmyth_backend_info_get_port
                                           (backend_info)), 1, 2, 4, 5);

    if (gtk_dialog_run(GTK_DIALOG(settings_dialog)) == GTK_RESPONSE_ACCEPT) {
        settings_dialog_update_data();
        gtk_widget_destroy(settings_dialog);
        return TRUE;
    }

    gtk_widget_destroy(settings_dialog);

    return FALSE;

}

static GtkWidget *
add_label_to_table(GtkWidget * table, const gchar * str, guint pos_left,
                   guint pos_right, guint pos_top, guint pos_bottom)
{
    GtkWidget      *tmp_label = gtk_label_new(str);

    gtk_widget_show(tmp_label);
    gtk_table_attach(GTK_TABLE(table), tmp_label,
                     pos_left, pos_right, pos_top, pos_bottom,
                     (GtkAttachOptions) (GTK_FILL),
                     (GtkAttachOptions) (0), 0, 0);
    gtk_misc_set_alignment(GTK_MISC(tmp_label), 0, 0.5);

    return tmp_label;
}

static GtkWidget *
add_entry_to_table(GtkWidget * table, const gchar * init_str,
                   guint pos_left, guint pos_right, guint pos_top,
                   guint pos_bottom)
{
    GtkWidget      *tmp_entry = gtk_entry_new();
    gtk_widget_show(tmp_entry);
    gtk_table_attach(GTK_TABLE(table), tmp_entry,
                     pos_left, pos_right, pos_top, pos_bottom,
                     (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                     (GtkAttachOptions) (0), 0, 0);
    if (init_str)
        gtk_entry_set_text(GTK_ENTRY(tmp_entry), init_str);

    // gtk_entry_set_invisible_char (GTK_ENTRY (entry_port), 9679);

    return tmp_entry;
}

static void
settings_dialog_update_data(void)
{
    // GMythSettings *backend_info = gmyth_context_get_settings();

    if (!backend_info) {
        g_warning
            ("[%s] Could not get GMythSettings instance from context\n",
             __FUNCTION__);
        return;
    }

    gmyth_backend_info_set_hostname(backend_info,
                                    gtk_entry_get_text(GTK_ENTRY
                                                       (entry_hostname)));

    gmyth_backend_info_set_db_name(backend_info,
                                   gtk_entry_get_text(GTK_ENTRY
                                                      (entry_dbname)));

    gmyth_backend_info_set_username(backend_info,
                                    gtk_entry_get_text(GTK_ENTRY
                                                       (entry_username)));

    gmyth_backend_info_set_password(backend_info,
                                    gtk_entry_get_text(GTK_ENTRY
                                                       (entry_passwd)));

    gmyth_backend_info_set_port(backend_info,
                                (gint)
                                g_ascii_strtoull(gtk_entry_get_text
                                                 (GTK_ENTRY(entry_port)),
                                                 NULL, 10));

    // gmyth_backend_info_save (backend_info);

}