#include "ZLogger.h"
#include <iostream>

ZLogger::ZLogger(int level) {
	memset(logTimes, 0, sizeof(long) * 100);
	memset(logCount, 0, sizeof(int) * 100);
	startTime = clock();
	logTime = clock();
	calcTime = 0;
	settingLevel = level;
	output("Start", 0, 0);
}

void ZLogger::finish() {
	output("Finished", 0, 0);
}

void ZLogger::warning(const char* text) {
	output("Warning", 1, 0);
}

void ZLogger::error(const char* text) {
	errorCode = 1;
	output("Error", 2, 0);
}

int ZLogger::infomation(const char* text, bool important) {
	if (errorCode) return errorCode;
	if (important) calcTime += (double)clock() - logTime;
	output(text, important ? -1 : -2, (double)clock() - logTime);
	logTime = clock();
	return 0;
}

int ZLogger::infomation(const char* text, bool important, int type) {
	if (errorCode) return errorCode;
	if (important) calcTime += (double)logTimes[type] / logCount[type];
	output(text, important ? -1 : -2, (double)logTimes[type] / logCount[type]);
	logTime = clock();
	return 0;
}

int ZLogger::step(int type) {
	if (errorCode) return errorCode;
	logTimes[type] += clock() - logTime;
	logCount[type]++;
	logTime = clock();
	//printf(".");
	return 0;
}

void ZLogger::restart(const char* text) {
	output(text, 0, calcTime);
	calcTime = 0;
}

void ZLogger::output(const char* text, int level, double time) {
	if (level < settingLevel) return;
	if (level == -2) printf("\x1b[34m");
	if (level == -1) printf("\x1b[36m");
	if (level == 1) printf("\x1b[33m");
	if (level == 2) printf("\x1b[31m");
	if (time > 0) {
		if (level == -1) {
			printf("\n%8ld ms |%8.2f ms |%8.2f ms | %s", clock() - startTime, calcTime, time, text);
		}
		else {
			printf("\n%8ld ms |            |%8.2f ms | %s", clock() - startTime, time, text);
		}
	}
	else {
		printf("\n%8ld ms |            |            | %s", clock() - startTime, text);
	}
	if(level != 0) printf("\x1b[39m");
}