#include "raine.h"
#include "display.h"
#include "dejap.h"
#include "games/games.h"
#include "config.h"
#include "hiscore.h"
#include "games/default.h"
#include "video/res.h"
#include "control.h"
#include "dsw.h"
#include "sound/sasound.h"
#include "emumain.h"
#include "ingame.h" // print_ingame

int wants_switch_res = 0;
static int WantScreen;
static int WantQuit;
static int WantPlay;

void load_message(char *msg) {
  // Does nothing for now
}

static int load_game_proc(int msg, void *d, int c)
{
  switch(msg){
  case MSG_IDLE:

    LoadDefault();

    load_error = 0;
    load_debug = malloc(0x10000);

    sprintf(load_debug,"Load Game\n---------\n\n");

    current_game = game_list[raine_cfg.req_game_index];

    // I have to change the depth BEFORE loading.
    // Probably because of the set_color_mapper in the loading function

    if(wants_switch_res) // && switch_res(current_game->video_info))){
      switch_res(current_game->video_info);
    else {
      print_debug("no resolution switching wanted\n");
      update_stretch();
    }

    /*

    count roms

    */

    load_game_rom_info();
    load_message("Loading Game Files...");

    if (!(load_error & LOAD_FATAL_ERROR)) {
      current_game->load_game();

      init_inputs();
      init_dsw();
      init_romsw();
      init_sound_list();
    }

    return D_EXIT;
    break;
  default:
    return D_O_K;
    break;
  }
}

static void do_load_game(void)
{

   /*

   satisfy the request

   */

   raine_cfg.req_load_game = 0;

   /*

   close down the current game (if there is one)

   */

   if(current_game){

      save_game_config();
      hs_close();

      if (current_game->clear_game)
	current_game->clear_game();

      ClearDefault();

      current_game = NULL;

   }


   load_game_proc(MSG_IDLE,NULL,0); // load without gui

   // Check if the load was successful, if ok, load game config, if bad clear the mess

   switch(load_error&3){
   case LOAD_WARNING:			// WARNING - IT MIGHT RUN OK

     strcat(load_debug,"\n\nThe game might not run correctly.");
     fprintf(stderr,"%s\n",load_debug);

   case 0x00:				// SUCCESS
     memcpy(&prev_display_cfg, &display_cfg, sizeof(DISPLAY_CFG));

     if (bestbpp > 0 && bestbpp <=32) {
       WantScreen = 1;
       display_cfg.bpp = bestbpp;
       bestbpp = 0;
     }

     if (bestw && besth) {
       //display_cfg.screen_type = bestmode;
       display_cfg.screen_x = bestw;
       display_cfg.screen_y = besth;
       WantScreen = 1;
       bestw = besth = 0;
     }

     load_game_config();

     reset_game_hardware();

     if (raine_cfg.save_game_screen_settings)
       WantScreen=1;
     break;
   case LOAD_FATAL_ERROR:			// FATAL ERROR - REMOVE GAME
   case LOAD_FATAL_ERROR|LOAD_WARNING:

     fprintf(stderr,"%s\n",load_debug);

     if (current_game->clear_game)
       current_game->clear_game();

     ClearDefault();

     current_game = NULL;

     break;
   }

   free(load_debug);
}

static void setup_font()
{
  if (RaineData) {
/*     gui_main_font     = RaineData[font_gui_main].dat; */
/*     gui_fixed_font    = RaineData[font_gui_fixed].dat; */
    ingame_font       = RaineData[font_8x8_ingame].dat;
    font	      = RaineData[font_gui_main].dat;

  } else {
    ingame_font = calloc(1,1792);
  }
}

int StartGUI(void)
{
   char str[256];

#ifdef RAINE_DEBUG
   print_debug("StartGUI(): START\n");
#endif

   sprintf(str, "%sconfig/language/%s", dir_cfg.share_path, dir_cfg.language_file);
   raine_push_config_state();
   raine_set_config_file(str);

   init_display();

   setup_font(); // Usefull even without gui for the messages on screen

/*    SpriteGun1	     = RaineData[Gun1].dat; */
/*    SpriteGun2	     = RaineData[Gun2].dat; */

   WantScreen=0;
   WantQuit=0;
   WantPlay=0;

#ifdef RAINE_DEBUG
   print_debug("StartGUI(): prepare international\n");
#endif

   // These 2 functions are in allegro, to install japenese text.
   // Actually it creates problems with accents...
   // register_uformat(U_SJIS, sjis_getc, sjis_getx, sjis_setc, sjis_width, sjis_cwidth, sjis_isok, 2);
   // set_uformat(U_SJIS);
   while(!WantQuit){		// ----- Main Loop ------

     /*

     check for load game request (from the command line)

     */

     if(raine_cfg.req_load_game)

       do_load_game();

     WantQuit=1;

     if(current_game)

       WantPlay = 1;

     else

       WantPlay = 0;

     if(WantScreen){		// Are we here for a screenchange?
       WantScreen=0;
       ScreenChange();
     }

     clear_raine_screen();

     if(WantPlay){		// Are we able to and wanting to play?
       WantPlay = 0;
       if(run_game_emulation()){ // In no gui mode, tab will reactivate the gui (req. by bubbles)
	 raine_cfg.no_gui = 0;
	 WantQuit = 0;
       }
     }

   }			// ----- Main Loop -----

   /*

   close down the current game (if there is one)

   */

   if(current_game){
     save_game_config();
     hs_close();

     if (current_game->clear_game)
       current_game->clear_game();

     ClearDefault();

     current_game = NULL;
   }

   raine_pop_config_state();

   return 0;
}

void setup_gui()
{
}

// release gui : the opposite function (see above)

void release_gui()
{
}

int raine_file_select(char *message, char *path, char *ext) {
  print_ingame(60,"raine_file_select not implemented yet");
  return 0;
}

void *old_draw = NULL;

void switch_mixer() {
  print_ingame(60,"no mixer yet");
}

void load_progress(void)
{
  /* Updates a progress bar while the game is loading. It will probably be much different
     in sdl... */
}

int goto_debuger = 0; // don't know if I'll use this one...
