#include <SDL.h>
#include <allegro.h>
#include "raine.h"
#include "blit.h"
#include "palette.h"
#include "compat2.h"
#include "video/res.h"

int disp_screen_x;
int disp_screen_y;

SDL_Surface *get_sdl_surface(BITMAP *b) {
  return (SDL_Surface*)b->extra;
}

static SDL_Surface *new_set_gfx_mode() {
  UINT32 videoflags;
  SDL_Surface *s;

  videoflags = SDL_HWSURFACE | SDL_HWPALETTE;
  // videoflags = SDL_SWSURFACE|SDL_RESIZABLE|SDL_ASYNCBLIT|SDL_ANYFORMAT;

  printf("old res %dx%d\n",display_cfg.screen_x,display_cfg.screen_y);
  // for now just force 800x600 to test scaling
  display_cfg.screen_x = 800; display_cfg.screen_y = 600;
  printf("new res %dx%d\n",display_cfg.screen_x,display_cfg.screen_y);
  // display_cfg.screen_x = 800; display_cfg.screen_y = 600;
  if ( (s=SDL_SetVideoMode(display_cfg.screen_x, display_cfg.screen_y,
			16, videoflags)) == NULL ) {
    fprintf(stderr,"could not setup %dx%d %d bpp\n",display_cfg.screen_x,display_cfg.screen_y,display_cfg.bpp);
    exit(1);
  }
  display_cfg.screen_x = s->w; display_cfg.screen_y = s->h;
  disp_screen_y = display_cfg.screen_y;
  disp_screen_x = display_cfg.screen_x;
  printf("mode %dx%d %dbpp\n",display_cfg.screen_x, display_cfg.screen_y,display_cfg.bpp);
  sdl_screen = s;
  return s;
}

void init_display() {
  new_set_gfx_mode();
  screen = surface_to_bmp(sdl_screen);
}

void ScreenChange(void)
{
  //show_mouse(NULL);

  SDL_Surface *s;
   if((s=new_set_gfx_mode())==NULL){	// Didn't work:
      memcpy(&display_cfg, &prev_display_cfg, sizeof(DISPLAY_CFG));
      s = new_set_gfx_mode();	// Revert to old mode
   }
   else{
     ReClipScreen();
      if (prev_display_cfg.bpp != display_cfg.bpp) {
	if (current_colour_mapper){
	  set_colour_mapper(current_colour_mapper);
	  ResetPalette();
	}
      }
   }
   if (screen) free(screen);
   screen = surface_to_bmp(s);
   //show_mouse(screen);
}

void set_default_video_mode() {
  display_cfg.screen_x = 640;
  display_cfg.screen_y = 480;
  display_cfg.bpp = 8;
}

void setup_gfx_modes() {
  set_default_video_mode();
}

void clear_bitmap(BITMAP *screen) {
  int len = screen->w*screen->h*bytes_per_pixel(screen);
  memset(screen->dat,0,len);
}

void clear_raine_screen() {
  clear_bitmap(screen);
}

// Sadly, I have to make this because I can't switch res as soon as I want.
// I have to indicate to the game being loaded the mode it will work in
// but wait for the gui to be redrawn before switching the res for real...
// Note also that the default values are 0 (no change)
int bestw,besth,bestbpp;

// Chooses a resolution with the variables bestw, besth and bestbpp
int switch_res(const VIDEO_INFO *vid)
{
   int ta;
   int w,h,i,mindiff,minindice,diff;

   get_best_resolution(vid,&w,&h);
   print_debug("trying to find a resolution close to %dx%d\n",w,h);
   if (vid->flags & VIDEO_NEEDS_16BPP) {
     if (display_cfg.bpp < 15)
       bestbpp = 16;
     else bestbpp = 0;
   } else if (vid->flags & VIDEO_NEEDS_8BPP)
     bestbpp = 8;
   else
     bestbpp = display_cfg.bpp;

   if (!sdl_screen->flags&SDL_FULLSCREEN) {
     bestw = w; besth = h; // EVERY mode is available for a window
   }

   if (display_cfg.screen_x != bestw || display_cfg.screen_y != besth
       || display_cfg.bpp != (UINT32)bestbpp)
     return 1;
   bestw = besth = bestbpp = 0; // No change !!!
   return 0;
}

void build_mode_list() {
}
