#include <chrono>
#include <iostream>
#include <filesystem>
#include "topology/geometryData.h"
#include "misc/Timer.hpp"
#include "easylogging++/easylogging++.h"

#include "config.h"

INITIALIZE_EASYLOGGINGPP

int main(int argc, char* argv[])
{
	std::string testdir = TEST_DIR;
	std::string test01_dir = testdir + "/geometryData/tests/test01";
	std::string wdir = testdir + "/geometryData/tests/current";

	//TO DO replace deleting and copyin with misc functions
	std::error_code ec;
	if (std::filesystem::exists(wdir, ec))
	{
		std::filesystem::remove_all(wdir, ec);
		std::cout << "could not delete directory " << ec.value() << std::endl;

	}

	if (!std::filesystem::create_directories(wdir, ec))
	{
		std::cout << "could not create directory " << ec.value() << std::endl;
		return 1;
	}
	std::filesystem::copy(test01_dir, wdir, std::filesystem::copy_options::recursive);



	try
	{
		Timer t;
		t.setMessage("Testing geometryDataTest.");

		t.start(" Parsing json.");
		std::ifstream ifs(wdir + "/geometry/raw/RawGeometryData.json");
		json jf = json::parse(ifs);
		t.stop();

		GeometryData gd(jf, wdir);

		t.start("Constructing geometryData.json.");
		json result_json =  gd.getJson();
		t.stop();

		std::ifstream if_res(wdir + "/result.json");
		json asset_json = json::parse(if_res);

		json diff_json = json::diff(result_json, asset_json);

		HANDLE  hConsole;
		hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

		FlushConsoleInputBuffer(hConsole);

		if (diff_json.empty())
		{
			SetConsoleTextAttribute(hConsole, 2);
			std::cout<< "TEST01 passed" << std::endl;
		}
		else
		{
			SetConsoleTextAttribute(hConsole, 12);
			std::cout << "TEST01 failed"  << std::endl;
			std::cout << "json diff :: " << diff_json.dump() << std::endl;
			std::cout << std::endl;

			std::cout << result_json.dump() << std::endl;
		}
		SetConsoleTextAttribute(hConsole, 15);

	}
	catch (std::exception &e)
	{
		std::cout << "Exception  " << e.what() << std::endl;
		return 1;
	}

	return 0;
}