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

#include <iostream> // @debug

#include <SDL.h>

#include "main.h"
#include "music.h"
#include "keyboard.h"
#include "mainmenu.h"
#include "mainmenubutton.h"
#include "gamehandler.h"
#include "helpscreen.h"
#include "credits.h"

const SDL_Rect MainMenu::firstButton = {0, 0, 200, 43};
const SDL_Rect MainMenu::focusModifier = {0, 559, 0, 0};
const int MainMenu::numButtons = 4;
const int MainMenu::buttonTypes[] = {2, 8, 9, 11};

MainMenu::MainMenu()
{
  waitingToReleaseReturn = false;
  focusedButton = -1;
  
  for(int i = 0; i < numButtons; i++)
    buttons.push_back(new MainMenuButton(this, i));
  
  menuImage = createSurface(screen->w, screen->h);
}

MainMenu::~MainMenu()
{
  for(int i = 0; i < numButtons; i++)
    delete buttons[i];
  
  SDL_FreeSurface(menuImage);
}

void MainMenu::activate()
{
  redraw();
  Music::play(1);
}

void MainMenu::cleanUp()
{
  SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0));
}

void MainMenu::redraw()
{
  SDL_Rect offset;
  
  SDL_Surface *logo = imagePool["logo.png"];
  SDL_Surface *menu = imagePool["menu.png"];
  SDL_BlitSurface(imagePool[MENU_BACKGROUND], NULL, menuImage, NULL);
  set_rect_pos(offset, (screen->w-logo->w)/2, getLogoYOffset());
  SDL_BlitSurface(logo, NULL, menuImage, &offset);
  set_rect_pos(offset, (screen->w-menu->w)/2, getAbsoluteMenuYOffset());
  SDL_BlitSurface(menu, NULL, menuImage, &offset);
  
  for(int i = 0; i < numButtons; i++)
    buttons[i]->redraw(menuImage);
  
  set_rect(offset, 0, 0, 0, 0);
  SDL_BlitSurface(menuImage, NULL, screen, &offset);
  SDL_Flip(screen);
}

void MainMenu::up()
{
  focusedButton--;
  if(focusedButton < 0)
    focusedButton = numButtons - 1;
  redraw();
}

void MainMenu::down()
{
  focusedButton++;
  if(focusedButton >= numButtons)
    focusedButton = 0;
  redraw();
}

void MainMenu::select()
{
  if(focusedButton == -1) {
    focusedButton = 0;
    redraw();
    return;
  }
  if(focusedButton == 0) {
    if(Keyboard::isPressed(SDLK_RETURN) || Keyboard::isPressed(SDLK_SPACE))
      waitingToReleaseReturn = true;
    else
      newProgramMode = new GameHandler();
  }
  if(focusedButton == 1)
    newProgramMode = new HelpScreen();
  if(focusedButton == 2)
    newProgramMode = new Credits();
  if(focusedButton == 3)
    stopProgram();
}

void MainMenu::event(SDL_Event *e)
{
  if(e->type == SDL_KEYDOWN) {
    if(e->key.keysym.sym == SDLK_ESCAPE || e->key.keysym.sym == SDLK_q)
      stopProgram();
    if(e->key.keysym.sym == SDLK_UP)
      up();
    if(e->key.keysym.sym == SDLK_DOWN)
      down();
    if(e->key.keysym.sym == SDLK_RETURN || e->key.keysym.sym == SDLK_SPACE)
      select();
  }
  if(e->type == SDL_KEYUP) {
    if(e->key.keysym.sym == SDLK_RETURN || e->key.keysym.sym == SDLK_SPACE)
      if(waitingToReleaseReturn)
        newProgramMode = new GameHandler();
  }
  if(e->type == SDL_MOUSEBUTTONUP) {
    if(e->button.button == SDL_BUTTON_WHEELUP)
      up();
    if(e->button.button == SDL_BUTTON_WHEELDOWN)
      down();
    if(e->button.button == SDL_BUTTON_LEFT)
      select();
  }
  if(e->type == SDL_MOUSEMOTION) {
    focusedButton = -1;
    for(int i = 0; i < numButtons; i++) {
      SDL_Rect pos = buttons[i]->getPosition();
      if(e->motion.x >= pos.x && e->motion.y >= pos.y &&
         e->motion.x < (pos.x+pos.w) && e->motion.y < (pos.y+pos.h)) {
        focusedButton = i;
      }
    }
  }
}

void MainMenu::update()
{
  for(int i = 0; i < numButtons; i++)
    buttons[i]->update();
  redraw();
}


