#pragma once    

#include "Task.h"
#include "Text.h"
#include "json.hpp"

class CompilationTask : public Task {
	String test_name;
	String makefile_text;
public:
	void setTestName(String* test_name_in) {
		test_name = String(test_name_in->getCharArray());
	}

	void setMakefileText(String* makefile_text_in) {
		makefile_text = String(makefile_text_in->getCharArray(), '|');
	}

	virtual void print() const {
		printf("id=%ld; maxtime=%d; test_name=%s\n", id, maxtime, test_name.getCharArray());
		printf("makefile_text=%s\n", makefile_text.getCharArray());
	}

	CompilationTask(Text* lines, int offset) : Task(lines, offset) {
		setTestName(lines->get(offset + 2));
		setMakefileText(lines->get(offset + 3));
		setState(Waiting);
		kernels = 1;
	}

	CompilationTask(const nlohmann::json& data) : Task(data) {
		test_name = Utils::getValue<string>(data, "test_name");
		makefile_text = Utils::getValue<string>(data, "makefile_text");

		setState(Waiting);
		kernels = 1;
	}

	virtual void prepareWorkspace() {
		String makeFilePath = String(id) + "/Makefile";
		File makeFileFile = File(makeFilePath, this->makefile_text);
		String tests = userWorkspace + "/projects";
		String testPath = tests + "/" + test_name;
		Utils::CopyDirectory_L(testPath, workspace);
	}
	virtual String getLaunchScriptText() {
		String modules = userWorkspace + "/modules";
		String starterCall = modules + "/starter";
		String launcherCall = modules + "/launcher";
		return String::DQuotes(starterCall) + " " +
			String::DQuotes(launcherCall) + " " +
			String(maxtime) + " " +
			String::DQuotes("") + " " +
			"make -j -f Makefile";
	}
	virtual void analyseResults() {
	    Task::analyseResults();
		String binary = workspace + "/0";
		state = Utils::Exists(binary) ? Done : DoneWithErrors;
	}
};