
#include "planner_headers.h"

/*
version.inc is generated by a pre-commit hook:

change the shebang to #!/bin/bash
and insert:

printf "updating src/version.inc\n"
printf "" > src/version.inc
printf "commit_timestamp = \"%(%Y-%m-%d %H:%M:%S)T\"\n" >> src/version.inc
printf "commit_tag = \"" >> src/version.inc
git describe --all --long | tr "\n" "\""  >> src/version.inc
printf "\n" >> src/version.inc
git add "src/version.inc"
*/

/*( public: )*/ string GestaltPlanner::get_version() {
#define commit_timestamp string timestamp
#define commit_tag ; string tag
#include "../../version.inc"
	;
#undef commit_timestamp
#undef commit_tag
	return string("version: ") + tag + "\n committed: " + timestamp;
}

/*( public: )*/ GestaltPlanner::GestaltPlanner(
	const string& command_log_file /*( = "last_run.log.cpp" )*/,
	bool show_logo /*( = true )*/
) :
	state{ make_shared<PlannerState>() } {

	state->log = Log(command_log_file);

	string msg;
	if (command_log_file == "") {
		msg = "GestaltPlanner gp;\n";
	}
	else {
		// we don't want to overwrite the log file we are currently running
		// so we append ".replay.cpp"
		msg = string("GestaltPlanner gp(\"") + command_log_file + ".replay.cpp\");\n";
	}
	msg += "if (gp.get_version() != R\"(" + get_version() + ")\") {\n"
		"\tstd::cout << \"warning: wrong version\\n\";\n}\n";

	auto guard = state->log.log(Log::Raw{ msg });

	if (show_logo) {
		cout << " \e[97m▄▄▄▄▄▄▄▄▄▄▄▄▄\e[0m" << "\n";
		cout << " \e[107;36m  ⌬ GΞSTΛLT  \e[0m" << "\n";
		cout << " \e[107;35m  │ Planner  \e[0m" << "\n";
		cout << " \e[107;35m  ╰──────>×  \e[0m" << "\n";
		cout << " \e[97m▀▀▀▀▀▀▀▀▀▀▀▀▀\e[0m" << "\n";
		cout << get_version() << "\n\n";
	}
	reset();
	//↯⍼↝➠➥⤞⤳⬳⮝
}

/*( public: )*/ GestaltPlanner::~GestaltPlanner() {
	auto guard = state->log.log("//~gp");

	while (!state->robots.empty()) {
		// remove removes an object and all its children, so we need a while loop
		remove(state->robots.begin()->first);
	}
	state->robotTemplates.clear();
	state->clearCache();
}

// bool operator==(const GestaltPlanner& lhs, const GestaltPlanner& rhs) {
// 	return *lhs.state == *rhs.state;
// }
