#include "scene/whitebox_scene.h"
#include "core/game_config.h"

#include <cstdio>

#ifndef __SWITCH__
#define SDL_MAIN_HANDLED
#else
#include <switch.h>
#endif

#include <frostbite2D/core/application.h>
#include <frostbite2D/scene/scene_manager.h>

#ifdef __SWITCH__
namespace {
void showSwitchInitFailure() {
  consoleInit(nullptr);

  PadState pad;
  padInitializeDefault(&pad);

  std::puts("NS Unknown Game");
  std::puts("");
  std::puts("Switch init failed.");
  std::puts("Check nxlink/Ryujinx logs for the failed init stage.");
  std::puts("");
  std::puts("Press + to exit.");

  while (appletMainLoop()) {
    padUpdate(&pad);
    if (padGetButtonsDown(&pad) & HidNpadButton_Plus) {
      break;
    }
    consoleUpdate(nullptr);
  }

  consoleExit(nullptr);
}
} // namespace
#endif

int main(int argc, char** argv) {
  (void)argc;
  (void)argv;

#ifdef __SWITCH__
  padConfigureInput(1, HidNpadStyleSet_NpadStandard);
#endif

  auto config = frostbite2D::AppConfig::createDefault();
  config.appName = "NS Unknown Game";
#ifdef __SWITCH__
  config.windowConfig.title = "NS Unknown Game";
  config.windowConfig.width = ns_game::config::kVirtualWidth;
  config.windowConfig.height = ns_game::config::kVirtualHeight;
  config.windowConfig.resizable = false;
#else
  config.windowConfig.title = "NS Unknown Game - Whitebox";
  config.windowConfig.width = 1280;
  config.windowConfig.height = 720;
  config.windowConfig.resizable = true;
#endif
  config.windowConfig.vsync = true;
  config.useVirtualResolution = true;
  config.virtualWidth = ns_game::config::kVirtualWidth;
  config.virtualHeight = ns_game::config::kVirtualHeight;
  config.resolutionMode = frostbite2D::ResolutionScaleMode::Fit;
  config.renderStyleProfile = frostbite2D::RenderStyleProfileId::PixelArt2D;
  config.maxFps = 60;

  auto& app = frostbite2D::Application::get();
  if (!app.init(config)) {
#ifdef __SWITCH__
    std::puts("NS Unknown Game Switch init failed.");
    showSwitchInitFailure();
#else
    std::puts("NS Unknown Game init failed.");
#endif
    return 1;
  }

  app.run([]() {
    frostbite2D::SceneManager::get().ReplaceScene(
        frostbite2D::MakePtr<ns_game::WhiteboxScene>());
  });

  app.shutdown();
  return 0;
}
