/* I seem to need this for glGenBuffers as per
   http://www.gamedev.net/community/forums/topic.asp?topic_id=422358 */
#define GL_GLEXT_PROTOTYPES

#include "base.h"
#include "../../view/state0.h"
#include "../../util/check_error.h"
#include <GL/glut.h>
#include <string.h>
#include <stdlib.h>
#include "sqlca.h"
extern struct sqlca sqlca;

/*
 * A simple alias to make the code more readable.
 */
#define S state0

void
base (void)
{
  /* 
   * This implementation can be improved by mapping the video memory
   * directly rather than loading into system memory and then copying
   * into video memory.
   */

  /*
   * db2dclgn -d exp004 -t coordinates
   */
  EXEC SQL BEGIN DECLARE SECTION;
  EXEC SQL INCLUDE 'model/data/coordinates.h';
  sqlint32 rows;
  EXEC SQL END DECLARE SECTION;

  /* Determine how many nodes have coordinates assigned to them. */
  EXEC SQL SELECT COUNT (*) INTO:rows FROM coordinates;

  /* Free any existing coordinates and allocate memory to store the
     new values. */
  S.rows = rows;

  free (S.selection.set);
  S.selection.set = calloc (rows, sizeof (bool));

  free (S.gi_data);
  S.gi_data = calloc (rows, sizeof (char) * 20);

  free (S.base_vertices_data);
  S.base_vertices_data = calloc (rows, sizeof (float) * 2);

  free (S.base_colors_data);
  S.base_colors_data = calloc (rows, sizeof (float) * 4);

  EXEC SQL DECLARE c2 CURSOR FOR SELECT *FROM coordinates;

  EXEC SQL OPEN c2;

  /* 
   * Initialize the bounding box of the points.
   */
  S.bb.min_x = 0.0;
  S.bb.max_x = 0.0;
  S.bb.min_y = 0.0;
  S.bb.max_y = 0.0;

  EXEC SQL FETCH c2 INTO:coordinates;
  while (sqlca.sqlcode != 100)
    {
      int i = coordinates.coord_id - 1;

      char *gi_data_row = S.gi_data + (i * 20);
      strncpy (gi_data_row, coordinates.gi.data, sizeof (char) * 20);
      *(gi_data_row + coordinates.gi.length) = '\0';

      float *v = S.base_vertices_data + (i * 2);
      *v = coordinates.x;
      v++;
      *v = coordinates.y;

      v = S.base_vertices_data + (i * 2);
      if (*v < S.bb.min_x)
	S.bb.min_x = *v;
      if (*v > S.bb.max_x)
	S.bb.max_x = *v;

      v++;
      if (*v < S.bb.min_y)
	S.bb.min_y = *v;
      if (*v > S.bb.max_y)
	S.bb.max_y = *v;

      /*
       * Deselected by default.
       */
      S.selection.set[i] = false;

      float *c = S.base_colors_data + (i * 4);
      *c = DEFAULT_COLOR_R;
      c++;
      *c = DEFAULT_COLOR_G;
      c++;
      *c = DEFAULT_COLOR_B;
      c++;
      *c = DEFAULT_COLOR_A;

      EXEC SQL FETCH c2 INTO:coordinates;
    }

  EXEC SQL CLOSE c2;

  EXEC SQL COMMIT;

  /*
   * Find the largest axis and use it to setup the projection.  This
   * is done to preserve the aspect ratio.  The aspect ratio should be
   * preserved since relative distance is a meaningful indicator in
   * the map.
   */

  // Min of min x or min y.
  if (S.bb.min_x <= S.bb.min_y)
    S.ortho_min = S.bb.min_x;
  else
    S.ortho_min = S.bb.min_y;
  S.ortho_min--;

  // Max of max x or max y.
  if (S.bb.max_x >= S.bb.max_y)
    S.ortho_max = S.bb.max_x;
  else
    S.ortho_max = S.bb.max_y;
  S.ortho_max++;

  // Invert the y coordinate to match up with the LGL Java viewer.
  float *v = S.base_vertices_data;
  for (int i = 0; i < S.rows; i++)
    {
      v++;
      *v = S.ortho_max - *v;
      v++;
    }

  // Move the origin (0,0) to the center of the data.
  S.ortho_min = 0.0;
  S.ortho_max = 0.0;

  v = S.base_vertices_data;
  for (int i = 0; i < S.rows; i++)
    {
      *v = *v - (0.5 * (S.bb.max_x - S.bb.min_x));

      if (S.ortho_min > *v)
	S.ortho_min = *v;

      if (S.ortho_max < *v)
	S.ortho_max = *v;

      v++;

      *v = *v - (0.5 * (S.bb.max_y - S.bb.min_y));

      if (S.ortho_min > *v)
	S.ortho_min = *v;

      if (S.ortho_max < *v)
	S.ortho_max = *v;

      v++;
    }

  glGenBuffers (2, S.buffers);

  glBindBuffer (GL_ARRAY_BUFFER, S.buffers[BASE_VERTICES]);
  glVertexPointer (2, GL_FLOAT, 0, 0);
  glBufferData (GL_ARRAY_BUFFER,
		sizeof (S.base_vertices_data), S.base_vertices_data,
		GL_STATIC_DRAW);

  glBindBuffer (GL_ARRAY_BUFFER, S.buffers[BASE_COLORS]);
  glColorPointer (4, GL_FLOAT, 0, 0);
  glBufferData (GL_ARRAY_BUFFER,
		sizeof (S.base_colors_data), S.base_colors_data,
		GL_STATIC_DRAW);

  return;
}