#include <easy2d/easy2d.h>
#include <filesystem>

// Pushbox 游戏核心头文件
#include "core/data.h"
#include "core/storage.h"
#include "scenes/start_scene.h"

// Nintendo Switch 平台支持
#ifdef __SWITCH__
#include <switch.h>
#endif

// Switch 上的存储路径
#ifdef __SWITCH__
static const char *SWITCH_STORAGE_PATH = "sdmc:/switch/pushbox";
#endif

static std::filesystem::path getExecutableDir(int argc, char **argv) {
// Nintendo Switch 上使用 SD 卡路径
#ifdef __SWITCH__
  // 创建目录（如果不存在）
  std::filesystem::create_directories(SWITCH_STORAGE_PATH);
  return SWITCH_STORAGE_PATH;
#else
  if (argc <= 0 || argv == nullptr || argv[0] == nullptr) {
    return std::filesystem::current_path();
  }

  std::error_code ec;
  auto exePath = std::filesystem::absolute(argv[0], ec);
  if (ec) {
    return std::filesystem::current_path();
  }
  return exePath.parent_path();
#endif
}

static float parseAutoQuitSeconds(int argc, char **argv) {
  for (int i = 1; i < argc; i++) {
    if (!argv[i]) {
      continue;
    }
    const std::string arg = argv[i];
    const std::string prefix = "--autoquit=";
    if (arg.rfind(prefix, 0) == 0) {
      try {
        return std::stof(arg.substr(prefix.size()));
      } catch (...) {
        return 0.0f;
      }
    }
  }
  return 0.0f;
}

int main(int argc, char **argv) {
  easy2d::Logger::init();
  easy2d::Logger::setLevel(easy2d::LogLevel::Info);

  auto &app = easy2d::Application::instance();

  easy2d::AppConfig config;
  config.title = "推箱子";
  config.width = 640;
  config.height = 480;
  config.vsync = true;
  config.fpsLimit = 0;

  if (!app.init(config)) {
    easy2d::Logger::shutdown();
    return -1;
  }

  const auto exeDir = getExecutableDir(argc, argv);
  auto &resources = app.resources();
  resources.addSearchPath(exeDir.string());
  resources.addSearchPath((exeDir / "assets").string());
  resources.addSearchPath((exeDir.parent_path() / "assets").string());
  resources.addSearchPath((exeDir.parent_path() / "src").string());
  resources.addSearchPath("assets");
  resources.addSearchPath("src");

  pushbox::initStorage(exeDir);
  pushbox::g_CurrentLevel = pushbox::loadCurrentLevel(1);
  if (pushbox::g_CurrentLevel > MAX_LEVEL) {
    pushbox::g_CurrentLevel = 1;
  }
  pushbox::g_SoundOpen = pushbox::loadSoundOpen(true);

  // 进入开始场景（主界面）
  app.enterScene(easy2d::makePtr<pushbox::StartScene>());

  const float autoQuitSeconds = parseAutoQuitSeconds(argc, argv);
  if (autoQuitSeconds > 0.0f) {
    app.timers().addTimer(autoQuitSeconds, [&app]() { app.quit(); });
  }
  app.run();

  app.shutdown();
  easy2d::Logger::shutdown();
  return 0;
}
