#include "main.h"
#include "menu.h"
#include "game.h"

// current menu and option
int curr_menu = MENU_MAIN;
int curr_option = 0;

// menu images
BITMAP *menu_bg;

// background music
MIDI *menu_music;

// some texts
char const **menu_text;
char const *copyright = "Copyright (c) 2006 OpSiSoft Systems, Inc. and Ricardo Santos";
char const *menu_main_text[] = {
  "Journey Onward",
	"Create New Character",
	"Settings",
	"Credits",
	"Exit Game",
	NULL
};

int menu_init() {
  PALETTE pal;
  // load images
  if (!(menu_bg = load_bitmap("data/images/menu.pcx", pal))) {
    printf("Error loading menu background!\n");
    return -1;
  }
  // load music
  if (!(menu_music = load_midi("data/midi/menu.mid"))) {
    printf("Error loading menu music!\n");
    return -1;
  }
  // start playing menu music
  play_midi(menu_music, TRUE);
  // TEMPORARIO # TEMPORARIO # TEMPORARIO # TEMPORARIO
  enter_game();
  curr_screen = SCREEN_INGAME;
  stop_midi();
  return 0;
}

void menu_keypress(int key) {
  switch(curr_menu) {
    case MENU_MAIN: action_main(key); break;
    default: break;
  }
}

void render_menu(BITMAP *bmp) {
  int i, color;
  // background image
  blit(menu_bg, bmp, 0, 0, 0, 0, menu_bg->w, menu_bg->h);
  switch(curr_menu) {
    case MENU_MAIN: menu_text = menu_main_text; break;
    default: break;
  }
  for (i=0; menu_text[i]; i++) {
    if (curr_option == i) color = 360;
    else color = 180;
    textout_centre_ex(bmp, font, menu_text[i], SCREEN_W/2, (SCREEN_H/2)+i*25, color, -1);
  }
  // copyright and version
  textout_centre_ex(bmp, font, copyright, SCREEN_W/2, SCREEN_H-12, makecol(40, 40, 70), -1);
  textprintf_right_ex(bmp, font, 495, 173, makecol(35,35,70), -1,"WoU v%.2f", VERSION);
}

void action_main(int key) {
  // change option up and down
  if (key == KEY_DOWN)
    curr_option++;
  else if (key == KEY_UP)
    curr_option--;

  // out of bounds
  if (curr_option < 0) curr_option = 4;
  else if (curr_option > 4) curr_option = 0;

  switch(curr_option) {
    case 0: {
      if (key == KEY_ENTER) {
        enter_game();
        curr_screen = SCREEN_INGAME;
        stop_midi();
      }
      break;
    }
    case 1: {
      break;
    }
    case 2: {
      break;
    }
    case 3: {
      break;
    }
    case 4: {
      if (key == KEY_ENTER)
        quit = TRUE;
      break;
    }
  }
}
