#include "tinysvg.h"
#include <stdio.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include <GL/GL.h>
#include <time.h>
#include "artgl.h"
#include <assert.h>
#include <stdlib.h>

#include <libart_lgpl/libart.h>

#include "svgsprite.h"
#include "level.h"


void do_fps(clock_t* begin, unsigned long* fc) {
  clock_t now = clock();
  double elapsed = (double)(now - *begin) / CLOCKS_PER_SEC;
  printf("%f FPS\n", *fc/elapsed);
  printf("%f msec per frame\n", 1000 * elapsed / *fc);
  *begin = now;
  *fc = 0;
} 

void initsdl() {

  if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    fprintf(stderr,
            "Couldn't initialize SDL: %s\n", SDL_GetError());
    exit(1);
  }

  atexit(SDL_Quit);

  if ( TTF_Init() < 0 ) {
    fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
    exit(1);
  }
  atexit(TTF_Quit);

}

SDL_Surface* initvideo(int w, int h, int fullscreen) {

  int flags = SDL_OPENGL;
  if (fullscreen) { flags = (flags | SDL_FULLSCREEN); }

  SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 5 );
  SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 0 );
  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1);
  SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4);

  const SDL_VideoInfo* vinfo = SDL_GetVideoInfo();
  assert(vinfo);
  SDL_PixelFormat* fmt = vinfo->vfmt;
  assert(fmt);
  int depth = fmt->BitsPerPixel;

  SDL_Surface* screen = SDL_SetVideoMode(w, h, depth, flags);
  
  if (screen == NULL) {
    fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
    exit(-1);
  }

  SDL_EnableKeyRepeat(0, 0);
  SDL_WM_SetCaption("LD48", 0);
  SDL_ShowCursor(SDL_DISABLE);
  SDL_WM_GrabInput(SDL_GRAB_ON);

  return screen;

}

void initgl(SDL_Surface* screen) {

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, 800, 600, 0.0, -1, 1);
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
    
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}

int main(int argc, char** argv) {

  srand((unsigned long)time(NULL));

  initsdl();
  int fullscreen = 0;
  SDL_Surface* screen = initvideo(800, 600, fullscreen);
  initgl(screen);

  ArtLibrary* lib = artlibrary_load();
  
  int done = 0;
  unsigned int level = 7;
  
  while (!done) {

    int inlevel = 1;
    Level* l = level_generate(lib, level);

    while (inlevel) {

      int rval;

      rval = level_handle_input(l);
      if (rval == LEVEL_RESULT_QUIT) { 
        inlevel = 0; 
        done = 1; 
        break;
      }

      rval = level_step_ai(l);
      switch (rval) {
      case LEVEL_RESULT_DIE:
        inlevel = 0;
        break;
      case LEVEL_RESULT_ADVANCE:
        inlevel = 0;
        ++level;
        break;
      default:
        break;
      }

      if (inlevel) {
        level_draw(lib, l);
      }

    }

    level_free(l);
  }

  artlibrary_free(lib);

  return 0;

}
