#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"

extern GtkWidget *window, *notebook;
extern GtkWidget *username_entry;
extern GtkWidget *password_entry;
extern GtkWidget *register_username_entry;
extern GtkWidget *register_password_entry;
extern GtkWidget *register_email_entry;
extern GtkWidget *page4_connect_button;
extern GtkWidget *register_website_entry;
extern GtkWidget *register_phone_entry;
extern GtkWidget *register_gsm_entry;
extern GtkWidget *register_password_confirm_entry;
extern GtkWidget *nick_availability_entry;
extern GtkWidget *page1_connect_button;
extern GtkWidget *unregistered_login_label_message;
extern GtkWidget *unregistered_login_checkbox;

void
on_page0_about_button_clicked          (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 5);
}


void
on_page0_cancel_button_clicked         (GtkButton       *button,
                                        gpointer         user_data)
{
g_print("abcdefghijklmnopqrstuvwxyz_exit_signal_1234567890_exit_signal_0123456789_exit_signal_abcdefghijklmnopqrstuvwxyz");
system("/usr/bin/killall -s 9 liblivechatsupport-check-0.3.14");
system("/usr/bin/killall -s 9 liblivechatsupport-register-0.3.14");
system("/usr/bin/killall -s 9 ubuntu-live-support");
gtk_main_quit();
}


void
on_page0_next_button_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
gtk_widget_set_sensitive (page1_connect_button, FALSE);
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 1);
}


void
on_page1_register_button_clicked       (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 2);
}


void
on_page1_back_button_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0);
}


void
on_page1_connect_button_clicked        (GtkButton       *button,
                                        gpointer         user_data)
{
char *nick;
char *password;

nick = gtk_entry_get_text(GTK_ENTRY(username_entry));
password = gtk_entry_get_text(GTK_ENTRY(password_entry));

puts(nick);
puts(password);
g_print("NULL\n");
g_print("NULL\n");
g_print("NULL\n");
g_print("NULL\n");
g_print("NULL\n");
g_print("NULL\n");
g_print("run gaim\n");
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (unregistered_login_checkbox))) 
  {
g_print("use unregistered login\n");
  } else {
g_print("use registered login\n");
}

gtk_main_quit();
return 1;
}


void
on_page2_back_button_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (unregistered_login_checkbox))) 
  {
gtk_widget_set_sensitive (page1_connect_button, TRUE);
  } else {
gtk_widget_set_sensitive (page1_connect_button, FALSE);
}
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 1);
}


void
on_page2_next_button_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 3);
}


void
on_check_availability_button_clicked   (GtkButton       *button,
                                        gpointer         user_data)
{
char *desired_nick;

desired_nick = gtk_entry_get_text(GTK_ENTRY(nick_availability_entry));
g_print("NULL\n");
g_print("NULL\n");
puts(desired_nick);
g_print("NULL\n");
g_print("NULL\n");
g_print("NULL\n");
g_print("NULL\n");
g_print("NULL\n");
g_print("check availability\n");
g_print("NULL\n");
gtk_main_quit();
return 1;
}

void
on_page3_back_button_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 2);
}


void
on_page3_next_button_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
char *defined_nickname;
gtk_widget_set_sensitive (page4_connect_button, FALSE);
gtk_widget_set_sensitive (register_username_entry, FALSE);
defined_nickname = gtk_entry_get_text(GTK_ENTRY(nick_availability_entry));
gtk_entry_set_text(GTK_ENTRY (register_username_entry), defined_nickname);

notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 4);
}


void
on_page4_back_button_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 3);
}


void
on_page4_connect_button_clicked        (GtkButton       *button,
                                        gpointer         user_data)
{
freenode_register_output();
gtk_main_quit();
}

void freenode_register_output() 
{
char *register_username;
char *register_password;
char *register_email;
char *register_website;
char *register_phone;
char *register_gsm;

register_username = gtk_entry_get_text(GTK_ENTRY(register_username_entry));
register_password = gtk_entry_get_text(GTK_ENTRY(register_password_confirm_entry));
register_email = gtk_entry_get_text(GTK_ENTRY(register_email_entry));
register_website = gtk_entry_get_text(GTK_ENTRY(register_website_entry));
register_phone = gtk_entry_get_text(GTK_ENTRY(register_phone_entry));
register_gsm = gtk_entry_get_text(GTK_ENTRY(register_gsm_entry));

g_print("Registering to freenode\n");
g_print("NULL\n");
puts(register_username);
puts(register_password);
puts(register_email);
puts(register_website);
puts(register_phone);
puts(register_gsm);
g_print("NULL\n");
}

void
on_window_destroy                      (GtkObject       *object,
                                        gpointer         user_data)
{
g_print("abcdefghijklmnopqrstuvwxyz_exit_signal_1234567890_exit_signal_0123456789_exit_signal_abcdefghijklmnopqrstuvwxyz");
system("/usr/bin/killall -s 9 liblivechatsupport-check-0.3.14");
system("/usr/bin/killall -s 9 liblivechatsupport-register-0.3.14");
system("/usr/bin/killall -s 9 ubuntu-live-support");
gtk_main_quit();
}

void
on_page5_credits_button_clicked        (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 6);
}


void
on_page5_license_button_clicked        (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 6);
}


void
on_page5_ok_button_clicked             (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0);
}


void
on_page6_back_button_clicked           (GtkButton       *button,
                                        gpointer         user_data)
{
notebook = lookup_widget(GTK_WIDGET(window), "notebook");
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 5);
}


void
on_register_password_confirm_entry_changed
                                        (GtkEditable     *editable,
                                        gpointer         user_data)
{
char *register_password_entry_first;
char *register_password_entry_confirm;
char *nullchar;
nullchar = "";

register_password_entry_first = gtk_entry_get_text(GTK_ENTRY(register_password_entry));
register_password_entry_confirm = gtk_entry_get_text(GTK_ENTRY(register_password_confirm_entry));

if (strcmp (register_password_entry_first, register_password_entry_confirm) == 0)
{
  gtk_widget_set_sensitive (page4_connect_button, TRUE);
} 
else {
  gtk_widget_set_sensitive (page4_connect_button, FALSE);
}

if (strcmp (register_password_entry_confirm, nullchar) == 0)
{
  gtk_widget_set_sensitive (page4_connect_button, FALSE);
} 

if (strcmp (register_password_entry_first, nullchar) == 0)
{
  gtk_widget_set_sensitive (page4_connect_button, FALSE);
} 

}


void
on_password_entry_changed              (GtkEditable     *editable,
                                        gpointer         user_data)
{
char *get_password_entry;
char *nullchar;
nullchar = "";

get_password_entry = gtk_entry_get_text(GTK_ENTRY(password_entry));

if (strcmp (get_password_entry, nullchar) == 0)
{
  gtk_widget_set_sensitive (page1_connect_button, FALSE);
} else {
  gtk_widget_set_sensitive (page1_connect_button, TRUE);
}

}


void
on_unregistered_login_checkbox_toggled (GtkToggleButton *togglebutton,
                                        gpointer         user_data)
{
char *nullchar;
nullchar = "";

if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (unregistered_login_checkbox))) 
  {
  gtk_entry_set_text(GTK_ENTRY (password_entry), nullchar);
  gtk_widget_set_sensitive (page1_connect_button, TRUE);
  gtk_widget_set_sensitive (password_entry, FALSE);
  gtk_widget_show (unregistered_login_label_message);
} else { 
  gtk_entry_set_text(GTK_ENTRY (password_entry), nullchar);
  gtk_widget_set_sensitive (page1_connect_button, FALSE);
  gtk_widget_set_sensitive (password_entry, TRUE);
  gtk_widget_hide (unregistered_login_label_message);
}

}


void
on_register_password_entry_changed     (GtkEditable     *editable,
                                        gpointer         user_data)
{
char *register_password_entry_first;
char *register_password_entry_confirm;
char *nullchar;
nullchar = "";

register_password_entry_first = gtk_entry_get_text(GTK_ENTRY(register_password_entry));
register_password_entry_confirm = gtk_entry_get_text(GTK_ENTRY(register_password_confirm_entry));

if (strcmp (register_password_entry_first, register_password_entry_confirm) == 0)
{
  gtk_widget_set_sensitive (page4_connect_button, TRUE);
} 
else {
  gtk_widget_set_sensitive (page4_connect_button, FALSE);
}

if (strcmp (register_password_entry_confirm, nullchar) == 0)
{
  gtk_widget_set_sensitive (page4_connect_button, FALSE);
} 

if (strcmp (register_password_entry_first, nullchar) == 0)
{
  gtk_widget_set_sensitive (page4_connect_button, FALSE);
} 

}

