#include "main.h"
#include "data\indexes.h"
#include "log.h"
#include "common.h"
#include "display.h"
#include "sound.h"
#include "mouse.h"
#include "map.h"

int current_screen;
int frames;

int CORNER_X = 10;
int CORNER_Y = 10;

static volatile int ticks = 0;
static void ticker(void) {
	ticks++;
}
END_OF_FUNCTION (ticker)

int init_game() {
	/* opens log file and reads ini file */
	open_log("wou.log");
	set_config_file("WoU.ini");

	/* allegro inits */
	allegro_init();
	install_keyboard();
	install_mouse();

  /* timer */
  install_timer();
  LOCK_FUNCTION (ticker);
	LOCK_VARIABLE (ticks);
	install_int_ex (ticker, BPS_TO_TIMER (72));

	/* to close the window */
	LOCK_FUNCTION(close_button_handler);
	set_close_button_callback(close_button_handler);

	/* inits display and sound */
	if (display_init()<0) return -1;
	if (sound_init()<0) return -1;
	//if (map_load()<0) return -1;
}

void show_intro_screens() {
  int skip_intro;

  // should i skip intro?
  skip_intro = get_config_int("misc", "skipintro", FALSE);
	(skip_intro) ? current_screen = SCREEN_MENU : current_screen = SCREEN_INTRO1;

	/* opsisoft presents */
	if (current_screen == SCREEN_INTRO1 && !skip_intro) {
		intro_screen(SCREEN_INTRO1);
		current_screen++;
	}
	/* a ricardo santos production */
	else if (current_screen == SCREEN_INTRO2 && !skip_intro) {
		intro_screen(SCREEN_INTRO2);
		current_screen++;
	}
	/* main menu */
	else if (current_screen == SCREEN_MENU) {
		intro_screen(SCREEN_MENU);
		sound_midi_play(MIDI_MENU, TRUE);
		current_screen++;
	}
}

void enter_game() {
	display_drawHud();
	map_draw(CORNER_X, CORNER_Y, 0);
	fade_in((RGB*)tiles[PALETTE_GAME].dat, 3);
	mouse_setCursor(MOUSE_GREENARROW);
}

void exit_game() {
	//cleaning methods
	log_print("\n*** Shutting down");
	close_log();
	display_destroy();
	sound_destroy();
	allegro_exit();
}

void draw_all() {
	acquire_screen();

	/* Layer 1 */
	map_drawTiles(CORNER_X,CORNER_Y, 0);

	/* Layer 2 */
	map_drawFringe(CORNER_X,CORNER_Y, 0);
	/* Layer 3 */
	map_drawObjects(CORNER_X,CORNER_Y, 0);

	/* Player */
	//player_draw();

	/* Mete a area de 'jogo' no buffer */
	blit(gamearea,vbuffer,0,0,18,18,gamearea->w,gamearea->h);
	blit(vbuffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
	release_screen();
}

/*void do_input() {
	if (key[KEY_UP]) {
		key[KEY_UP] = 0;
		if (player_move(PLAYER_X, PLAYER_Y-1)) {
			CORNER_Y--;
			if (CORNER_Y<0) CORNER_Y = 549;
		}
	}
	else if (key[KEY_DOWN]) {
		key[KEY_DOWN] = 0;
		if (player_move(PLAYER_X, PLAYER_Y+1)) {
			CORNER_Y++;
			if (CORNER_Y>549) CORNER_Y = 0;
		}
	}
	else if (key[KEY_LEFT]) {
		key[KEY_LEFT] = 0;
		if (player.move(PLAYER_X-1, PLAYER_Y)) {
			CORNER_X--;
			if (CORNER_X<0) CORNER_X = 549;
		}
	}
	else if (key[KEY_RIGHT]) {
		key[KEY_RIGHT] = 0;
		if (player.move(PLAYER_X+1, PLAYER_Y)) {
	   	CORNER_X++;
	   	if (CORNER_X>549) CORNER_X = 0;
		}
	}
}*/

int main() {
  // could not initialize game
	if (init_game()<0) {
		exit_game();
		return 0;
	}

  // main loop
  frames = ticks;
	while (!key[KEY_ESC] && !CLOSE_BUTTON_PRESSED) {
		// show intro screens
		if (current_screen <= SCREEN_MENU) {
			show_intro_screens();
		}
		// main menu (SCREEN_MENU+1) -> the main menu after
		else if (current_screen == SCREEN_MENU+1 && key[KEY_ENTER]) {
			current_screen++;
			fade_out(3);
			sound_midi_stop();
			enter_game();
		}
		// in game screen
		else if(current_screen>SCREEN_MENU+1) {
			//do_input();
			draw_all();
		}
	}
	exit_game();
	return 0;
}
END_OF_MAIN();
