/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 *
 * Function draw screen between game (like lose, lose or print the level code)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

#include "stonage.h"

#include <SDL.h>
#if defined(HAVE_SDL_SDL_IMAGE_H)
#include <SDL/SDL_image.h>
#endif

#include "game.h"
#include "screen.h"
#include "level.h"
#include "level_io.h"
#include "level_code.h"
#include "image.h"
#include "music.h"

enum e_justify
{
  JUSTIFY_LEFT,
  JUSTIFY_CENTER,
  JUSTIFY_RIGHT
};

/**
 *
 *
 */
static int
Game_ScreenDrawFont(SDL_Surface *screen, SDL_Surface *font, int y, int justify, const char *msg)
{
  unsigned char c;
  int i,j,x;
  SDL_Rect rs,rd;
  static char special_characters[]=
   {"^$?!*.:, <>"};

  /* calc the len of the string (store in i) */
  for (i=0;msg[i]!=0 && msg[i]!='\n';i++)
    ;
  if (i==0)
    return 1;
  switch (justify)
   {
    case JUSTIFY_LEFT:
      x=16;
      break;
    case JUSTIFY_CENTER:
      x=(screen->w - (i*32))/2;
      break;
    case JUSTIFY_RIGHT:
      x=screen->w - (i*32);
      break;
    default:
      return 1;
   }

  i=0;
  c=msg[i++];
  while (c!=0 && c!='\n')
   {
     if (c>='A' && c<='Z')
       c-='A';
     else if (c>='0' && c<='9')
       c=c-'0'+26;
     else
      {
	for (j=0;j<sizeof(special_characters);j++)
	 {
	   if (special_characters[j]==c)
	    {
	      c=36+j;
	      break;
	    }
	 }
	if (j==sizeof(special_characters))
	 {
	   c=msg[i++];
	   continue;
	 }
      }

     rs.y=0; rs.w=rs.h=32;
     rs.x=1+c*32;

     rd.x=x; rd.y=y;
     rd.w=rd.h=32;

     SDL_BlitSurface(font,&rs,screen,&rd);

     x+=32;
     c=msg[i++];
   }
  return i;
}

/**
 * Draw some message when user play for the first time in the game.
 * @return 0 if we found an error
 */
static int 
Game_ScreenCommon(SDL_Surface *screen, const char *text)
{
  SDL_Surface *image, *font;
  SDL_Event event;
  int i,y,j,lines;

  image = SDL_CreateRGBSurface(SDL_HWSURFACE,screen->w,screen->h,
      screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
  SDL_FillRect(image,NULL,0);

  font = Image_Load("images/font_gamescreen.png",1);
  if (font==NULL)
   {
     fprintf(stderr,_("Can't load images/font_gamescreen.png\n"));
     return 0;
   }

  /* Center in Y */
  for (i=0,lines=0;text[i]!=0;i++)
   {
     if (text[i]=='\n')
       lines++;
   }
  y=16 + ((screen->h - lines * 40)/2);
  for (i=0;text[i]!=0;)
   {
     j=text[i++];
     if (j=='l')
       i+=Game_ScreenDrawFont(image,font,y,JUSTIFY_LEFT,text+i);
     else if (j=='c')
       i+=Game_ScreenDrawFont(image,font,y,JUSTIFY_CENTER,text+i);
     else if (j=='r')
       i+=Game_ScreenDrawFont(image,font,y,JUSTIFY_RIGHT,text+i);
     else
      {
	i--;
	i+=Game_ScreenDrawFont(image,font,y,JUSTIFY_CENTER,text+i);
      }
     y+=40;
   }

  Screen_Fade(screen,image);
  SDL_FreeSurface(image);
  SDL_FreeSurface(font);

  /* wait for key or button */
  while (1) 
   {
     SDL_WaitEvent(&event);
     if (event.type == SDL_QUIT) 
       Game_Quit();
     if (event.type == SDL_KEYUP)
       break;
   }

  return 1;
}


int 
Game_ScreenTimeout(SDL_Surface *screen)
{
  char text[]=
  N_("A STRONG BLOW AND\n"
     "ALL AROUND YOU FADES\n"
     "TO BLACK. LUCKY AS\n"
     "EVER, YOU FIND YOUR\n"
     "WAY BACK TO WHERE\n"
     "YOU STARTED !\n");
  return Game_ScreenCommon(screen,text);
}


int 
Game_ScreenLose(SDL_Surface *screen)
{
  char text[]=
     N_("SORRY, MY FRIEND,\n"
     "THERE ARE NO MORE\n"
     "CANDLES IN YOUR\n"
     "POCKET  !!!\n");
  return Game_ScreenCommon(screen,text);
}


int
Game_ScreenBegin(SDL_Surface *screen)
{
  char text[128];
#ifdef HAVE_SNPRINTF
  snprintf(text,128,_("YOU ARE ABOUT TO\n^ ENTER LEVEL %3.3d.\n\n\nPRESS ANY KEY\n^IF YOU THINK\n$ YOU ARE READY.*\n"),game.level);
#else
  sprintf(text,_("YOU ARE ABOUT TO\n^ ENTER LEVEL %3.3d.\n\n\nPRESS ANY KEY\n^IF YOU THINK\n$ YOU ARE READY.*\n"),game.level);
#endif
  return Game_ScreenCommon(screen,text);
}

int
Game_ScreenWin(SDL_Surface *screen)
{
  char text[256];
#define TEXT \
     "lYOUR SCORE:  %5.5d\n"\
     "rNEXT LEVEL: %3.3d\n"\
     "r%1.1d CANDLES\nc\n"
  const char *text_random[]={
     "THINKING OF NOTHING\n"
     "SPECIAL, SUDDENLY\n"
     "YOUR ONLY THOUGHT IS\n"
     "THAT SINGLE WORD:",
     "AS YOU STEP THRU THE\n"
     "EXIT, YOU FIND SOME\n"
     "LETTERS SCRIBBLED ON\n"
     "THE FLOOR. THESE ARE"
  };
#ifdef HAVE_SNPRINTF
  snprintf(text,256,TEXT"%s\nc%6s\n",game.score,game.level,game.candles,
      text_random[RANDOM()%2],level_encode(game.level));
#else
  sprintf(text,_(TEXT),game.score,game.level,level_encode(game.level));
#endif
  return Game_ScreenCommon(screen,text);
}


