#include "errorHandling.h"

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


void ErrorHandler::handleError(int errNo)
{
	int index = 0; 
	
	index = getLookUpIndex(errNo);

	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 | " << errNo << " | \"" << errorLookup[index].errMessage << "\""<<std::endl;
	exit(1); 
}

void ErrorHandler::addNewError(const int& no, const std::string& message)
{
	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) >= 0) // Check if Error No already exists
	{
		std::cout << "Error | Intern | Error No: "<< no << " Exists ! PLease choose a different number !" << std::endl; 
		exit(1); 
	}

	errorLookup[emptyIndex].errNo = no; 
	errorLookup[emptyIndex].errMessage = message; 
	emptyIndex++; 
}

// 
// Privat memeber functions 
//

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