#include "main.h"
#include "log.h"
#include "menu.h"
#include "sql.h"
#include "game.h"
#include "console.h"

BITMAP *vbuffer;
int curr_screen = SCREEN_MENU;

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

volatile int quit = FALSE;
void close_button_handler(void) {
	quit = TRUE;
}

int display_init() {
	int gfx_mode, depth;

  // display init
	depth = desktop_color_depth();
	if (depth == 0) depth = 32;
	set_color_depth(depth);

	// set the screen
	if (get_config_int("misc", "fullscreen", 0)) gfx_mode = GFX_AUTODETECT;
	else gfx_mode = GFX_AUTODETECT_WINDOWED;
  if (set_gfx_mode(gfx_mode, 640, 480, 0, 0) != 0) {
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to set any graphic mode\n%s", allegro_error);
		return -1;
	}

  // sound init
	if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, "")<0) {
		allegro_message("Error initialising sound\n%s\n", allegro_error);
		log_println("Unable to initialize sound card!\n%s", allegro_error);
		return -1;
	}
	return 0;
}

int init_main() {
  /* allegro inits */
	allegro_init();
	install_keyboard();

  /* misc inits */
	three_finger_flag = 0;
	set_window_title("World of Ultima");
	open_log("wou.log");
	set_config_file("wou.ini");

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

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

	/* display init */
	if (display_init()<0) return -1;
	set_display_switch_mode(SWITCH_BACKGROUND);
	vbuffer = create_bitmap(SCREEN_W, SCREEN_H);

  /* sound init */
	install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL);

	/* sql init */
	//if (sql_loaditems()<0) return -1;
	return 0;
}

int load_data() {
  DATAFILE *fonts = load_datafile("data/fonts.dat");

	if (!fonts) {
    printf("Could not load font datafile!\n");
		return -1;
	}
  font = fonts[3].dat;
  // load menu data
  if (menu_init()<0) return -1;
  return 0;
}

void run_logic() {
  /* check the keys pressed */
  while (keypressed()) {
    int k = readkey();
    int scancode = k >> 8;
    // check for changes in menus
    if (curr_screen == SCREEN_MENU) {
      menu_keypress(scancode);
    }
    else if (curr_screen == SCREEN_INGAME) {
      console_keypress(k);
    }
    if (scancode == KEY_ESC) {
      quit = TRUE;
    }
  }
  client_sendall(&client);
}

void run_render() {
  if (curr_screen == SCREEN_MENU)
    render_menu(vbuffer);
  else if (curr_screen == SCREEN_INGAME) {
    render_playarea(vbuffer);
    render_console(vbuffer);
  }
  blit(vbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void cleanup() {
}

int main() {
  int draw;

  /* initializations */
  printf("initializing...\n");
  if (init_main()<0) return 1;

  /* data loading */
  printf("loading data...\n");
  if (load_data()<0) {
    allegro_message ("Some data could not be found. Please check your installation.");
    cleanup();
    return 1;
  }

  /* main loop while not quit */
  printf("entering the main loop...\n");
  while (!quit) {
		while (ticks > 0) {
			run_logic();
			draw = TRUE;
			ticks--;
		}
    if (draw) {
      run_render();
      draw = FALSE;
    }
    while(ticks == 0){ Sleep(1); }
  }

  log_println("leaving program...");
  cleanup();
  return 0;
}
END_OF_MAIN();
