// Project Lab - NHTV Igad

#include "UnrealProject.h"

#if PLATFORM_SPECIFIC_WIN == 0

#include "HeatMapMetrics.h"

#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>


using namespace std;

float HeatMapMetrics::s_timeMap[3][512][512];

void HeatMapMetrics::ResetMap()
{
	memset(&s_timeMap, 0, sizeof(s_timeMap));
}

void HeatMapMetrics::AddTime(FVector2D position, float time, size_t team)
{
	position *= 512.0f;
	s_timeMap[team][(int)position.Y][(int)position.X] += time;
}

void HeatMapMetrics::ExportToFile()
{
	char* envPath;
	size_t envPathSize;
	getenv_s(&envPathSize, NULL, 0, "APPDATA");
	if (envPathSize == 0)
	{
		TERROR("APPDATA does not exist.");
		return;
	}

	envPath = (char*)malloc(envPathSize * sizeof(char));
	if (!envPath)
	{
		TERROR("Failed to allocate memory.");
		return;
	}

	getenv_s(&envPathSize, envPath, envPathSize, "APPDATA");


	string path = envPath;

	free(envPath);

	path.append("\\Haxis\\HaxisHeatmap");
	path.append(m_GetCurrentDateTime());
	path.append(".HME");
	fstream fs;
	fs.open(path.c_str(), fstream::out | fstream::binary);
	if (!fs.is_open())
	{
		TERROR("failed to export metrics");
		return;
	}

	fs.write((char*)&s_timeMap[0][0][0], sizeof(s_timeMap));

	fs.close();
}

void HeatMapMetrics::m_RetrieveCurrentTM(std::tm& a_TM)
{
	time_t currentTime = time(0);
	localtime_s(&a_TM, &currentTime);
}
string HeatMapMetrics::m_GetCurrentTime()
{
	tm tstruct;
	char buffer[80];
	m_RetrieveCurrentTM(tstruct);
	strftime(buffer, sizeof(buffer), "(%H:%M:%S) ", &tstruct);

	return buffer;
}
string HeatMapMetrics::m_GetCurrentDateTime()
{
	tm tstruct;
	char buffer[80];
	m_RetrieveCurrentTM(tstruct);
	strftime(buffer, sizeof(buffer), "%Y-%m-%d_%H-%M-%S", &tstruct);

	return buffer;
}

#endif