#include "readConfig.h"

static const int CHARS_PER_LINE = 512;

static int screen_width = 0;
static int screen_height = 0;

void read_config (GtkWidget * window1)
{
  GtkWidget *name, *team, *music, *sound, *gamma, *stats, *flightsim, *resolution;
  FILE * input_stream;
  char * currentLine = calloc(CHARS_PER_LINE + 1, sizeof(char));
  
  if ((input_stream = fopen("autoexec.cfg", "r")) == NULL)
  {
    fprintf(stderr, "Unable to open autoexec.cfg file\n");
    return ;
  }
  
  while (readLine(currentLine, input_stream)) {
    char * setting = calloc(CHARS_PER_LINE + 1, sizeof(char));
    char * setting_pos = setting;
    int x, y;
    x = 0;
    if (strncmp("name", currentLine, 4) == 0) {
      
      while (* (currentLine + (x * sizeof(char))) != '"')
        x++;
      x++;  // skip the '"' as well
      y = x + 1;
      while (* (currentLine + (y * sizeof(char))) != '"')
        y++;
      for ( ; x < y ; x++) {
        * setting_pos = * (currentLine + x);
        setting_pos++;
      }
      //now set name to "setting" value
      name = lookup_widget(GTK_WIDGET(window1), "name_textbox");
      gtk_entry_set_text(GTK_ENTRY(name), setting);
      
    } else if (strncmp("team", currentLine, 4) == 0) {
    
      while (* (currentLine + (x * sizeof(char))) != '"')
        x++;
      x++;  // skip the '"' as well
      y = x + 1;
      while (* (currentLine + (y * sizeof(char))) != '"')
        y++;
      for ( ; x < y ; x++) {
        * setting_pos = * (currentLine + x);
        setting_pos++;
      }
      team = lookup_widget(GTK_WIDGET(window1), "team_textbox");
      gtk_entry_set_text(GTK_ENTRY(team), setting);
    
    } else if (strncmp("invmouse", currentLine, 8) == 0) {
      
      flightsim = lookup_widget(GTK_WIDGET(window1), "flightsim_mode");
      if (* (currentLine + (9*sizeof(char))) == '1')
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(flightsim), TRUE);
      else
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(flightsim), FALSE);

    } else if (strncmp("musicvol", currentLine, 8) == 0) {
      
//       printf("%s\n", currentLine);
      music = lookup_widget(GTK_WIDGET(window1), "music_spin");
      strcpy(setting, (currentLine + (9 * sizeof(char))));
      // Sometimes I hate C. If someone knows of a C method similar to Java's 
      // Integer.parseInt(String ) let me know
      while ((* setting_pos <= '9') && (* setting_pos >= '0')) {
//         printf("At the start of this interation: x = %d, current value = %d\n", x, * setting_pos - '0');
        x = (10 * x) + (* setting_pos) - '0';
        setting_pos++;
      }
//       printf("At the end of the loop: x = %d\n", x);
      if (x > 255) {
        x = 255;
        fprintf(stderr, "Music volume cannot exceed 255\n");
      }
      // because of the while () statement earlier we're guaranteed there won't be any negatives
      // This is a check that sound/music volume did indeed get set. x can be set to zero, so we can't trust it.
      if (setting == setting_pos) {
        fprintf(stderr, "Illegal music volume. Please correct. Setting music volume to 150.\n");
        x = 150;
      }
      gdouble value = 0.0 + x;
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(music), value);
      
    } else if (strncmp("soundvol", currentLine, 8) == 0) {

//       printf("%s\n", currentLine);
      sound = lookup_widget(GTK_WIDGET(window1), "sound_spin");
      strcpy(setting, (currentLine + (9 * sizeof(char))));
      while ((* setting_pos <= '9') && (* setting_pos >= '0')) {
        x = (10 * x) + (* setting_pos) - '0';
        setting_pos++;
      }
      if (x > 255) {
        x = 255;
        fprintf(stderr, "Sound volume cannot exceed 255\n");
      }
      if (setting == setting_pos) {
        fprintf(stderr, "Illegal sound volume. Please correct. Setting sound volume to 150.\n");
        x = 150;
      }
      gdouble value = 0.0 + x;
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(sound), value);

    } else if (strncmp("gamma", currentLine, 5) == 0) {
    
      gamma = lookup_widget(GTK_WIDGET(window1), "gamma_spin");
      strcpy(setting, (currentLine + (6 * sizeof(char))));
      while ((* setting_pos <= '9') && (* setting_pos >= '0')) {
        x = (10 * x) + (* setting_pos) - '0';
        setting_pos++;
      }
      if (x > 100) {
        x = 100;
        fprintf(stderr, "Gamma cannot exceed 100\n");
      }
      if (setting == setting_pos) {
        fprintf(stderr, "Illegal gamma level. Please correct. Setting gamma to 100.\n");
        x = 100;
      }
      gdouble value = 0.0 + x;
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(gamma), value);

    } else if (strncmp("hidestats", currentLine, 9) == 0) {
    
      // I know these seem backwards -- see an original file to understand
      stats = lookup_widget(GTK_WIDGET(window1), "show_statistics");
      if (* (currentLine + (10*sizeof(char))) == 1)
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(stats), FALSE);
      else
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(stats), TRUE);
    
    } else if (strncmp("screen_height", currentLine, 13) == 0) {
      
      strcpy(setting, (currentLine + (14 * sizeof(char))));
      while ((* setting_pos <= '9') && (* setting_pos >= '0')) {
        x = (10 * x) + (* setting_pos) - '0';
        setting_pos++;
      }
      screen_height = x;
//       printf("Height has been set as %d\n", x);
    
    } else if (strncmp("screen_width", currentLine, 11) == 0) {
      
      strcpy(setting, (currentLine + (13 * sizeof(char))));
      while ((* setting_pos <= '9') && (* setting_pos >= '0')) {
//         printf("x = %d\n", * setting_pos -'0');
        x = (10 * x) + (* setting_pos) - '0';
        setting_pos++;
      }
      screen_width = x;
//       printf("Width has been set as %d\n", x);
    
    }
    
    // else - do nothing

    #ifdef DEBUG
    printf("%s\n", currentLine);
    #endif
    free(currentLine);
    currentLine = calloc(CHARS_PER_LINE + 1, sizeof(char));
//     printf("%s\n", setting);
    free(setting);
  }
  fclose(input_stream);
  free(currentLine);
  
  // Finally, let's set the screen resolution the user choose in their config
  // file.  If they didn't set it, pick a default.
  if ((screen_width == 0) || (screen_height == 0)) {
    screen_width = 1024;
    screen_height = 768;
  }
  
  gint res_index = -1;
  resolution = lookup_widget(GTK_WIDGET(window1), "resolution_box");
  switch (screen_height)
  {
    case 600: res_index = 0;
      break;
    case 768:
      if (screen_width == 1024)
        res_index = 1;
      else if (screen_width == 1280)
        res_index = 3;
      break;
    case 864: res_index = 2;
      break;
    case 960: res_index = 4;
      break;
    case 1024: res_index = 5;
      break;
    case 1200:
      if (screen_width == 1600)
        res_index = 6;
      else if (screen_width == 1920)
        res_index = 11;
      break;
    case 1344: res_index = 7;
      break;
    case 1440:
      if (screen_width == 1800)
        res_index = 8;
      else if (screen_width == 1920)
        res_index = 12;
      break;
    case 1392: res_index = 9;
      break;
    case 1080: res_index = 10;
      break;
    case 1536: res_index = 13;
      break;
    default: res_index = 1;
  }
  gtk_combo_box_set_active (GTK_COMBO_BOX(resolution), res_index);
}
