#include <iostream>
#include "EditorApp.hpp"
#include "systems/SceneSerializer.hpp"

int main(int argc, char *argv[])
{
	try {
		EditorApp app;

		// Parse command line arguments
		bool gameMode = false;
		bool debugBuoyancy = false;
		Ogre::String sceneFile;
		for (int i = 1; i < argc; i++) {
			Ogre::String arg = argv[i];
			if (arg == "--game") {
				gameMode = true;
			} else if (arg == "--debug-buoyancy") {
				debugBuoyancy = true;
			} else if (arg.length() > 0 && arg[0] != '-') {
				sceneFile = arg;
			}
		}

		if (gameMode) {
			app.setGameMode(EditorApp::GameMode::Game);
		}

		if (debugBuoyancy) {
			app.setDebugBuoyancy(true);
		}

		app.initApp();

		// Auto-load scene if provided as argument (editor mode only)
		if (!sceneFile.empty() &&
		    app.getGameMode() == EditorApp::GameMode::Editor) {
			std::cout << "Auto-loading scene: " << sceneFile
				  << std::endl;
			SceneSerializer serializer(*app.getWorld(),
						   app.getSceneManager());
			if (!serializer.loadFromFile(sceneFile, nullptr)) {
				std::cerr << "Failed to load scene: "
					  << serializer.getLastError()
					  << std::endl;
			}
		}

		app.getRoot()->startRendering();
		app.closeApp();
	} catch (const std::exception &e) {
		std::cerr << "Error: " << e.what() << std::endl;
		return 1;
	}

	return 0;
}
