#include "errorHandling.h"

ErrorHandler::ErrorHandler(): emptyIndex(0)
{
}


void ErrorHandler::handleError(int errNo, std::string source)
{
	int index = 0; 
	
	index = getLookUpIndex(errNo, source);

	if(index < 0)
	{
		std::cout << "The given error number does not exist\n" << "Tipp: use \"addNewError(Integer, String)\""<< std::endl;
		exit(1); 
	}
	
	std::cout <<  ">> Error << | " << errorLookup[index].errSource  << " | " << errNo << " | \"" << errorLookup[index].errMessage << "\""<<std::endl;

	if(errorLookup[index].errExit)
	{
		exit(1); 
	}
}

void ErrorHandler::addNewError(const int& no, const std::string& source, const std::string& message, const unsigned char kill)
{
	if(emptyIndex >= MAX_NUMBER_OF_ERRORS) // Check if list is full
	{
		std::cout << "Error | Ultimate | List is full" << std::endl; 
		exit(1); 
	}
	
	if(getLookUpIndex(no, source) >= 0) // Check if Error No already exists
	{
		std::cout << "Error | Intern | Error No: "<< no << " From Source: "<< source <<" Exists ! PLease choose a different number !" << std::endl; 
		exit(1); 
	}

	errorLookup[emptyIndex].errNo = no; 
	errorLookup[emptyIndex].errSource = source; 
	errorLookup[emptyIndex].errMessage = message; 
	errorLookup[emptyIndex].errExit = kill; 
	emptyIndex++; 
}

// 
// Privat memeber functions 

int ErrorHandler::getLookUpIndex(int errNo, std::string source)
{
	int i = 0; 
	for(i = 0; i < emptyIndex; i++)
	{
		if(errNo == errorLookup[i].errNo)
		{
			if(source == errorLookup[i].errSource)
			{
				return i;
			}
		}
	}
	return -1; 
}
