/*
 * Authors : Kerem Yollu & Edwin Koch
 * Date : 07.03.2021
 * Version	: 0.1
 * License	: MIT-0
 *
 * Description : An easy way to intereact with argv for passing commands. 
 *
 * TODO : Inplement singleton pattern 
 * TODO : Write description
 * TODO : Comment the code wiht odxygen
 * TODO : write a standart string Output function and implment it (will come with the BSL) 08.08.21
 */

#include "commandManager.h"


CommandManager::CommandManager():emptyIndex(0)
{
}

void CommandManager::addNewCommand(	const std::string& commmand, 
									const std::string& description, 
									commanCallback_t callBack)
{
	if(emptyIndex >= MAX_MUNBER_OF_COMMANDS) // Check if the command list is full
	{
		std::cout << "Error | Intern | Command list is full!" << std::endl;
		exit(1);
	}
	if(getLookUpIndex(commmand) >= 0) // Chek for command duplicats
	{
		std::cout << "Error | Intern | Command already exitst!" << std::endl;
		exit(1);
	}
	
	
	commandLookup[emptyIndex].commmand = commmand; 
	commandLookup[emptyIndex].description = description; 
	commandLookup[emptyIndex].callBack = callBack; 
	emptyIndex++;
}

void CommandManager::operator()(const std::string cmdName)
{
	int index = 0; 

	if(cmdName == "help" || cmdName == "Help")
	{
		printHelp(); 
		exit(1);
	}
	
	if(cmdName == "-h" || cmdName == "-H")
	{
		printCommads(); 
		exit(1);
	}
	
	index = getLookUpIndex(cmdName); 

	if(index < 0) 
	{
		std::cout << "Error | Intern | Invalid Command!" << std::endl;
		exit(1);
	}
	
	commandLookup[index].callBack(); 
	exit(1);
}


// 
// Privat memeber functions 
//


int CommandManager::getLookUpIndex(const std::string& cmd)
{
	int index = 0; 
	for (index = 0; index < emptyIndex; index++) 
	{
		if(commandLookup[index].commmand == cmd)
		{
			return index; 
		}
	}
	return -1; 
}

void CommandManager::printHelp()
{
	std::cout << "Function : printHelp is under construction" << std::endl; 
}

void CommandManager::printCommads()
{
	std::cout << "Function : printCommads is under construction" << std::endl; 
}

