/*

  I'm 14 and this is cool as SHIT
         by suicide cult

  code & design: Happy Pony Land
          music: modus0perandi

  Presented at Edison 2019

 */

#include "demo.h"


// Global framecounter
int frame = 0;

poly_t * first_to_render = NULL;
poly_t * last_to_render = NULL;
trans_t * current_trans = NULL;

int display_timecode = 0;
int have_music = 1;
int start_fullscreen = 1;
int full_render = 0;



int main(int argc, char ** argv)
{
  for (int i = 1; i < argc; i++)
  {
    if (argv[i][0] == 'f')
      start_fullscreen = true;
    
    if (argv[i][0] == 'r')
      full_render = true;
  }
            
  alloc_all();

  gfx_init();

  set_scene_name("EMPTY");

  if (full_render)
  {
    char file[200];

    while (current_scene < ch_end)
    {
      render();

      snprintf(file, sizeof file, "/tmp/test-out/shit-%06d.png", frame);
      if (al_save_bitmap(file, al_get_backbuffer(realdisp)))
        DEBUG_PRINTF("Wrote %s\n", file);
      else
        DEBUG_PRINTF("Couldn't write %s\n", file);

      animate();
      frame++;
    }

    goto death;
  }

  
  
  ALLEGRO_EVENT ev;

  while (current_scene != ch_end)
  {
    if (al_is_event_queue_empty(event_queue))
    {
      render();
    }

    al_wait_for_event(event_queue, &ev);
    
    if (system_event(&ev))
    {
      continue;
    }
    else if (ev.type == ALLEGRO_EVENT_TIMER)
    {
      animate();
      frame++;
    }
    else if (ev.type == ALLEGRO_EVENT_KEY_CHAR)
    {
      switch (ev.keyboard.keycode)
      {
        case ALLEGRO_KEY_ESCAPE:
        case ALLEGRO_KEY_Q:
          goto death;

        case ALLEGRO_KEY_T:
          display_timecode = !display_timecode;
          break;

        case ALLEGRO_KEY_N:
          {
            int old = current_scene;

            while (old == current_scene)
            {
              animate();
              frame++;
            }
            
            al_seek_audio_stream_secs(music, (frame / (float)FPS));
          }
          break;

        case ALLEGRO_KEY_S:
          {
            char timecode[50];
            get_timecode(timecode, sizeof(timecode));
            printf("MARKED: %s\n", timecode);
          }
          break;
      }
    }
  }

 death:
  gfx_shutdown();
}



void animate()
{
  while (run_trans(current_trans) > 0)
  {
    current_scene++;
    current_trans = init_scene(current_scene);
    
    if (current_scene >= ch_end)
    {
      DEBUG_PRINTF("%s", "demo end\n");
      return;
    }
    
    char timecode[50];
    get_timecode(timecode, sizeof(timecode));
    DEBUG_PRINTF("starting chapter %d \"%s\" at %s\n", current_scene, scene_name, timecode);
  }
  
  return;
}



int system_event(ALLEGRO_EVENT * ev)
{
  if (ev->type == ALLEGRO_EVENT_TIMER)
  {
    if (ev->any.source == (void*)fpstimer)
    {
      fps_avg = (fps_avg + fps_frames) / 2;
      fps_frames = 0;
      
      return true;
    } 
  }
  else if (ev->type == ALLEGRO_EVENT_DISPLAY_RESIZE)
  {
    if (ev->display.width >= WIN_MIN_W || ev->display.height >= WIN_MIN_H)
    {
      al_resize_display(realdisp,
                        (ev->display.width  >= WIN_MIN_W ? ev->display.width  : WIN_MIN_W),
                        (ev->display.height >= WIN_MIN_H ? ev->display.height : WIN_MIN_H));
    }

    al_acknowledge_resize(realdisp);

    configure_canvas();
    reload_fonts();
    
    return true;
  }
  else if (ev->type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  {
    gfx_shutdown();
  }
  else if ((ev->type == ALLEGRO_EVENT_KEY_CHAR && ev->keyboard.keycode == ALLEGRO_KEY_F) ||
           (ev->keyboard.modifiers & ALLEGRO_KEYMOD_ALT && ev->keyboard.keycode == ALLEGRO_KEY_ENTER))
  {
    if (al_get_display_flags(realdisp) & ALLEGRO_FULLSCREEN_WINDOW)
      al_toggle_display_flag(realdisp, ALLEGRO_FULLSCREEN_WINDOW, false);
    else
      al_toggle_display_flag(realdisp, ALLEGRO_FULLSCREEN_WINDOW, true);

    configure_canvas();
    reload_fonts();
    
    return true;
  }

  return false;
}



void get_timecode(char * dest, size_t len)
{
  int min, sec, subframe;
  
  min = frame / (FPS * 60);
  sec = (frame / FPS) % 60;
  subframe = frame % FPS;
  
  snprintf(dest, len, "%04d %02d:%02d:%02d", frame, min, sec, subframe);

  return;
}
