/**
 * Copyright (c) 2024 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#include "game/game.h"
#include "game/time.h"
#include "input.h"
#include "display/display.h"
#include "asset/asset.h"
#include "asset/assetmap.h"
#include "ui/textbox.h"
#include "ui/testmenu.h"
#include "ui/mainmenu.h"

#include "game/state/mainmenu.h"
#include "game/state/mapchange.h"
#include "game/state/overworld.h"

game_t GAME;

void gameInit() {
  memset(&GAME, 0, sizeof(game_t));

  timeInit();
  inputInit();
  displayInit();
  assetInit();

  textboxInit();
  testMenuInit();
  mainMenuInit();

  GAME.mapNext = MAP_LIST_TEST;
  GAME.state = GAME_STATE_INITIAL;
}

gameupdateresult_t gameUpdate(const float_t delta) {
  timeUpdate(delta);
  inputUpdate();

  switch(GAME.state) {
    case GAME_STATE_INITIAL:
      GAME.state = GAME_STATE_MAIN_MENU;
      break;

    case GAME_STATE_OVERWORLD:
      gameStateOverworldUpdate();
      break;
    
    case GAME_STATE_PAUSED:
      if(inputWasPressed(INPUT_BIND_PAUSE)) GAME.state = GAME_STATE_OVERWORLD;
      break;

    case GAME_STATE_MAP_CHANGE:
      gameStateMapChangeUpdate();
      break;

    case GAME_STATE_MAIN_MENU:
      gameStateMainMenuUpdate();
      break;
  }

  // Perform render.
  displayUpdate();

  if(GAME.shouldExit) return GAME_UPDATE_RESULT_EXIT;
  return GAME_UPDATE_RESULT_CONTINUE;
}

void gameDispose() {
  assetDispose();
  displayDispose();
}