/*
 * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#include <ox/logconn/def.hpp>
#include <ox/logconn/logconn.hpp>

#ifndef OLYMPIC_PROJECT_NAME
#define OLYMPIC_PROJECT_NAME "OlympicProject"
#endif

#ifndef OLYMPIC_APP_ID
#define OLYMPIC_APP_ID "AppID"
#endif

#ifndef OLYMPIC_APP_NAME
#define OLYMPIC_APP_NAME "App"
#endif

#ifndef OLYMPIC_APP_VERSION
#define OLYMPIC_APP_VERSION "dev build"
#endif

#ifndef OLYMPIC_GUI_APP
#define OLYMPIC_GUI_APP 0
#endif

#ifndef OLYMPIC_PROJECT_NAMESPACE
#define OLYMPIC_PROJECT_NAMESPACE project
#endif

#ifndef OLYMPIC_PROJECT_DATADIR
#define OLYMPIC_PROJECT_DATADIR ".keel"
#endif

#ifndef OLYMPIC_LOAD_KEEL_MODULES
#define OLYMPIC_LOAD_KEEL_MODULES 1
#endif

#ifndef OLYMPIC_LOAD_STUDIO_MODULES
#define OLYMPIC_LOAD_STUDIO_MODULES 0
#endif

namespace olympic {
ox::StringLiteral appVersion{OLYMPIC_APP_VERSION};
ox::StringLiteral appId{OLYMPIC_APP_ID};
ox::StringLiteral projectName{OLYMPIC_PROJECT_NAME};
}

ox::Error run(
		ox::StringView project,
		ox::StringView appName,
		ox::StringView projectDataDir,
		ox::SpanView<ox::CString> argv) noexcept;

namespace OLYMPIC_PROJECT_NAMESPACE {
void registerKeelModules() noexcept;
void registerStudioModules() noexcept;
}

#if defined(_WIN32) && OLYMPIC_GUI_APP
int WinMain() {
	auto const argc = __argc;
	auto const argv = const_cast<char const**>(__argv);
#else
int main(int const argc, char const **argv) {
#endif
	OX_INIT_DEBUG_LOGGER(loggerConn, OLYMPIC_PROJECT_NAME " " OLYMPIC_APP_NAME)
#if OLYMPIC_LOAD_KEEL_MODULES
	OLYMPIC_PROJECT_NAMESPACE::registerKeelModules();
#endif
#if OLYMPIC_LOAD_STUDIO_MODULES
	OLYMPIC_PROJECT_NAMESPACE::registerStudioModules();
#endif
	auto const err = run(
			OLYMPIC_PROJECT_NAME,
			OLYMPIC_APP_NAME,
			OLYMPIC_PROJECT_DATADIR,
			{argv, static_cast<size_t>(argc)});
	oxAssert(err, "Something went wrong...");
	if (ox::defines::Debug && err) {
		oxErrf("Failure: {}\n", toStr(err));
	}
	return static_cast<int>(err);
}
