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

#include <gtk/gtk.h>

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

void
on_window1_destroy                     (GtkObject       *object,
                                        gpointer         user_data)
{
  gtk_main_quit();
}


void
on_button1_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
  FILE * output_file;
  
  /* get pointers to all the relevant widgets in order to get data from them */
  GtkWidget *name, *team, *music, *sound, *gamma, *stats, *flightsim,
            *show_dialog, *resolution;
  
  if ((output_file = fopen("autoexec.cfg", "w")) == NULL) {
    fprintf(stderr, "Unable to open autoexec.cfg for writing\n");
    fclose(output_file);
    gtk_main_quit();
  }
  
  name = lookup_widget(GTK_WIDGET(button), "name_textbox");
  team = lookup_widget(GTK_WIDGET(button), "team_textbox");
  music = lookup_widget(GTK_WIDGET(button), "music_spin");
  sound = lookup_widget(GTK_WIDGET(button), "sound_spin");
  gamma = lookup_widget(GTK_WIDGET(button), "gamma_spin");
  stats = lookup_widget(GTK_WIDGET(button), "show_statistics");
  flightsim = lookup_widget(GTK_WIDGET(button), "flightsim_mode");
  resolution = lookup_widget(GTK_WIDGET(button), "resolution_box");
  show_dialog = lookup_widget(GTK_WIDGET(button), "show_dialog_again");
  
  fprintf(output_file, "// put all your personal settings in this file,\n");
  fprintf(output_file, "// and all your keybindings that are different from the default (see keymap.cfg)\n");
  fprintf(output_file, "name \"%s\"\n", gtk_entry_get_text(GTK_ENTRY(name)));
  fprintf(output_file, "team \"%s\"\n\n", gtk_entry_get_text(GTK_ENTRY(team)));
  fprintf(output_file, "show_gtk_configuration_tool ");
  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(show_dialog)) == TRUE)
    fprintf(output_file, "1");
  else
    fprintf(output_file, "0");
  fprintf(output_file, "\ninvmouse ");
  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(flightsim)) == TRUE)
    fprintf(output_file, "1");
  else
    fprintf(output_file, "0");
  fprintf(output_file, "\t\t// 1 for flightsim mode\nsensitivity 30\nsensitivityscale 10\n\n");
  fprintf(output_file, "fov 105\t\t// field of view- pretty reasonable\n\n");
  fprintf(output_file, "alias myfov 105\t\t// code for zooming in\nbind MOUSE2 [\nfov 30;\n");
  fprintf(output_file, "onrelease [ fov $myfov ]\n]\n\n");
  fprintf(output_file, "// set higher if you want (max 255, default 150)\n");
  fprintf(output_file, "musicvol %d\n", gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(music)));
  fprintf(output_file, "soundvol %d\n\n", gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(sound)));
  fprintf(output_file, "// *don't modify* unless you understand it fully\n");
  fprintf(output_file, "// on slow machines, try setting to 20 25\n// on fast machines, don't touch\n");
  fprintf(output_file, "fpsrange 30 40\n\nminlod 100\t\t// on slow machines, try as low as 25 (bad visuals)");
  fprintf(output_file, "\ndynlight 16\t\t// set 0 if it slows you down\n");
  fprintf(output_file, "gamma %d", gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(gamma)));
  fprintf(output_file, "\t\t// set to your liking, 100 = default\n\nhidestats ");
  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(stats)) == TRUE)
    fprintf(output_file, "0");
  else
    fprintf(output_file, "1");
  fprintf(output_file, "\t\t// if you don't want to see the numbers in the corner,\n");
  fprintf(output_file, "\t\t\t//turn this to \"hidestats 1\"\n");
  fprintf(output_file, "\n\n// Note that the \"undomegs\" pragma is no longer used.\n\n");
  fprintf(output_file, "alias sp    [ mode -2; map $arg1 ]\nalias dmsp  [ mode -1; map $arg1 ]\n");
  fprintf(output_file, "alias ffa   [ mode  0; map $arg1 ]\nalias insta [ mode  4; map $arg1 ]\n");
  fprintf(output_file, "alias modifier 0\n\n");
  fprintf(output_file, "alias domodifier [ alias modifier $arg1; onrelease [ alias modifier 0 ] ]\n");
  fprintf(output_file, "bind Q [ domodifier 1 ]\n");
  fprintf(output_file, "bind Z [ domodifier 2 ]\n");
  fprintf(output_file, "bind X [ domodifier 3 ]\n");
  fprintf(output_file, "alias universaldelta [\n  if $editing [ alias s \"edit\" ] [ alias s \"game\" ]");
  fprintf(output_file, "  concatword delta _ $s _ $modifier\n  s\n]\n\n");
  fprintf(output_file, "bind MOUSE4 [ universaldelta  1 ]\n");
  fprintf(output_file, "bind MOUSE5 [ universaldelta -1 ]\n");
  fprintf(output_file, "alias delta_edit_0 [ editheight $flrceil $arg1 ]\n");
  fprintf(output_file, "alias delta_edit_1 [ if $flrceil [ vdelta $arg1 ] [ vdelta (- 0 $arg1) ] ]\n");
  fprintf(output_file, "alias delta_edit_2 [ edittex $flrceil $arg1 ]\n");
  fprintf(output_file, "alias delta_edit_3 [ edittex (+ $flrceil 1) $arg1 ]\n");
  fprintf(output_file, "alias delta_edit_4 [ equalize $flrceil ]\n");
  fprintf(output_file, "alias delta_edit_5 [ entproperty 0 $arg1 ]\n");
  fprintf(output_file, "\nalias delta_game_0 [ if (= $arg1 1) [ weapon 2 3 1 ] [ weapon 4 1 2 ] ]\n");
  fprintf(output_file, "\nalias zfov 120\n");
  fprintf(output_file, "alias delta_game_1 [\n");
  fprintf(output_file, "  alias zfov (+ $zfov (* $arg1 (div $zfov -5)))\n");
  fprintf(output_file, "  if (< $zfov 10)  [ alias zfov 10 ]\n");
  fprintf(output_file, "  if (> $zfov 120) [ alias zfov 120 ]\n");
  fprintf(output_file, "  fov $zfov\n]\n\n");
  
  char * myResolution = gtk_combo_box_get_active_text (GTK_COMBO_BOX(resolution));
  int x = 0;
  while ((* myResolution >= '0') && (* myResolution <= '9')) {
    x = (10 * x) + (* myResolution) - '0';
    myResolution++;
  }
  fprintf(output_file, "screen_width %d\n", x);
  x = 0;
  myResolution += sizeof(char) * 4;  // the format is "800 by 600", so there's 4 chars to jump
  while ((* myResolution >= '0') && (* myResolution <= '9')) {
    x = (10 * x) + (* myResolution) - '0';
    myResolution++;
  }
  fprintf(output_file, "screen_height %d\n", x);
  
  if (fclose(output_file) != 0) {
    printf("An error was encountered when I was trying to write the autoexec.cfg file\n");
    gtk_main_quit();
  }
  #ifdef DEBUG
  printf("Attempting to launch main Cube executable\n");
  #endif
  startCube();
  #ifdef DEBUG
  printf("Closing GTK\n");
  #endif
  gtk_main_quit();
}


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


void
on_resolution_box_changed              (GtkComboBox     *combobox,
                                        gpointer         user_data)
{
  GtkWidget *dialog1;
  dialog1 = create_dialog1 ();
  gtk_widget_show (dialog1);
}


void
on_dialog1_close                       (GtkDialog       *dialog,
                                        gpointer         user_data)
{
  GtkWidget *mydialog;
  mydialog = lookup_widget(GTK_WIDGET(dialog), "dialog1");
  gtk_widget_destroy (mydialog);
}


void
on_okbutton1_clicked                   (GtkButton       *button,
                                        gpointer         user_data)
{
  GtkWidget *mydialog;
  mydialog = lookup_widget(GTK_WIDGET(button), "dialog1");
  gtk_widget_destroy (mydialog);
}
