//
// introscreen.cpp
//
// program introduction
//
//
// Author: Tomi Belan <tomi.belan@gmail.com>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <iostream> //@debug

#include "introscreen.h"
#include "mainmenu.h"
#include "music.h"

#define SEC(x) ((x)*FRAMES_PER_SECOND)

#define PERCENT_IN_INTERVAL(val,start,end) ((.0+(val)-(start))/(.0+(end)-(start)))
#define IS_IN_INTERVAL(val,start,end) ((val)>=(start)&&(val)<(end))

IntroScreen::IntroScreen()
{
  buf = createSurface(screen->w, screen->h);
  frameCounter = 0;
}

IntroScreen::~IntroScreen()
{
  if(buf)
    SDL_FreeSurface(buf);
}

void IntroScreen::event(SDL_Event *e)
{
  if(e->type == SDL_KEYDOWN || e->type == SDL_MOUSEBUTTONUP) {
    newProgramMode = new MainMenu();
  }
  if(e->type == SDL_QUIT)
    stopProgram();
}

void IntroScreen::update()
{
  if(frameCounter == SEC(0)) {
    SDL_FillRect(buf, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
    SDL_Surface *logo = imagePool["ad-victum4f.png"];
    SDL_Rect tmp; set_rect_pos(tmp, (screen->w-logo->w)/2, (screen->h-logo->h)/2);
    SDL_BlitSurface(logo, NULL, buf, &tmp);
  }
  if(IS_IN_INTERVAL(frameCounter,SEC(0),SEC(3))) {
    int alpha = (int) (255*PERCENT_IN_INTERVAL(frameCounter,SEC(0),SEC(3)));
    SDL_SetAlpha(buf, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
    SDL_BlitSurface(buf, NULL, screen, NULL);
    SDL_Flip(screen);
  }
  if(frameCounter == SEC(3)) {
    /*
    SDL_FillRect(buf, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
    SDL_Color white = {255, 255, 255}; std::string text = tr("PRESENTS");
    SDL_Surface *message = TTF_RenderText_Blended(papyrusFont, text.c_str(), white);
    SDL_Rect offset; set_rect(offset, (buf->w-message->w)/2, (buf->h-message->h)/2, 0, 0);
    SDL_BlitSurface(message, NULL, buf, &offset);
    */
    SDL_FillRect(buf, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
    SDL_Surface *presents = imagePool["presentsscreen1.png"];
    SDL_Rect tmp; set_rect_pos(tmp, (screen->w-presents->w)/2, (screen->h-presents->h)/2);
    SDL_BlitSurface(presents, NULL, buf, &tmp);
  }
  if(IS_IN_INTERVAL(frameCounter,SEC(3),SEC(6))) {
    int alpha = (int) (255*PERCENT_IN_INTERVAL(frameCounter,SEC(3),SEC(6)));
    SDL_SetAlpha(buf, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
    SDL_BlitSurface(buf, NULL, screen, NULL);
    SDL_Flip(screen);
  }
  if(frameCounter == SEC(6)) {
    SDL_Rect tmp = {0, 0, 0, 0};
    set_rect_pos(tmp, (screen->w-imagePool["logo.png"]->w)/2, MainMenu::getLogoYOffset());
    SDL_BlitSurface(imagePool[MENU_BACKGROUND], NULL, buf, NULL);
    SDL_BlitSurface(imagePool["logo.png"], NULL, buf, &tmp);
    /*
    SDL_Color white = {255, 255, 255}; std::string text = tr("PRESENTS");
    SDL_Surface *message = TTF_RenderText_Blended(papyrusFont, text.c_str(), white);
    SDL_Rect offset; set_rect(offset, (buf->w-message->w)/2, (buf->h-message->h)/2, 0, 0);
    SDL_BlitSurface(message, NULL, buf, &offset);
    */
  }
  if(IS_IN_INTERVAL(frameCounter,SEC(6),SEC(9))) {
    int alpha = (int) (255*PERCENT_IN_INTERVAL(frameCounter,SEC(6),SEC(25)));
    if(alpha > 64) alpha = 255;
    SDL_SetAlpha(buf, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
    SDL_BlitSurface(buf, NULL, screen, NULL);
    SDL_Flip(screen);
  }
  if(frameCounter == SEC(9)) {
    SDL_SetAlpha(buf, SDL_SRCALPHA | SDL_RLEACCEL, SDL_ALPHA_OPAQUE);
    SDL_BlitSurface(buf, NULL, screen, NULL);
    SDL_Flip(screen);
    
    SDL_Rect offset;
    SDL_BlitSurface(imagePool[MENU_BACKGROUND], NULL, buf, NULL);
    set_rect(offset, (screen->w-imagePool["logo.png"]->w)/2, MainMenu::getLogoYOffset(), 0, 0);
    SDL_BlitSurface(imagePool["logo.png"], NULL, buf, &offset);
    
    SDL_SetAlpha(buf, SDL_SRCALPHA | SDL_RLEACCEL, SDL_ALPHA_OPAQUE);
    SDL_BlitSurface(buf, NULL, screen, NULL);
    SDL_Flip(screen);
  }
  /*
  if(IS_IN_INTERVAL(frameCounter,SEC(9),SEC(12))) {
    int alpha = (int) (255*PERCENT_IN_INTERVAL(frameCounter,SEC(9),SEC(30)));
    if(alpha > 128) alpha = 255;
    SDL_SetAlpha(buf, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
    SDL_BlitSurface(buf, NULL, screen, NULL);
    SDL_Flip(screen);
  }
  */
  if(frameCounter == SEC(10)) {
    newProgramMode = new MainMenu();
  }
  
  frameCounter++;
}

void IntroScreen::cleanUp()
{
}


