#include "cInput.hpp"

/*** Custom Header Files ***/
#include "../UtilityEngine/cUtility.hpp"

using InputEngine::cInput;
using UtilityEngine::cUtility;

/*static*/ cInput* cInput::sp_inst = nullptr;

//Private
cInput::cInput()
{}

cInput::~cInput()
{
	CleanUp();
}

//Public
/*static*/ cInput& cInput::Inst()
{
	if (sp_inst == nullptr)
	    sp_inst = new cInput();
	return *sp_inst;
}

/*static*/ void cInput::Delete()
{
	delete sp_inst;
	sp_inst = nullptr;
}

//Functions
const bool cInput::Initialize() const
{
	bool rtn = IsInit();

	if (rtn == false) {
		if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0) {
			cUtility::Inst().Message("Input initialized.");
			rtn = true;
		} else
			cUtility::Inst().Message("Could not initialize Input.hpp SDL_InitSubSystem(SDL_INIT_JOYSTICK):", __AT__, cUtility::eTypeSDL::SDL);
	}

	return rtn;
}
		
const bool cInput::Setup()
{
	return true;
}

void cInput::CleanUp()
{
	DeleteAllInputs();
}

void cInput::CheckInputs( const SDL_Event& event )
{
	m_event = event;
	switch(m_event.type)
	{
	case SDL_KEYUP:
	case SDL_KEYDOWN:
		CheckKeyboards();
		break;
	case SDL_JOYAXISMOTION:
	case SDL_JOYBALLMOTION:
	case SDL_JOYHATMOTION:
	case SDL_JOYBUTTONDOWN:
	case SDL_JOYBUTTONUP:
		CheckJoysticks();
		break;
	}
}

void cInput::CheckKeyboards()
{
	std::vector<InputEngine::cKeyboard*>::iterator it;

	for ( it = m_keyboards.begin() ; it < m_keyboards.end(); it++ ) {
		InputEngine::cKeyboard* temp = (*it);
		temp->KeyboardCheck(m_event);
	}
}

void cInput::CheckJoysticks()
{

}

/* Adds a keyboard to cInput internal list to be called when CheckInputs is called */
void cInput::AddKeyboard( InputEngine::cKeyboard* keyboard )
{
	m_keyboards.push_back(keyboard);
}

/* Deletes a keyboard in cInput internal list */
void cInput::DeletesKeyboard( const cString& label )
{
	std::vector<InputEngine::cKeyboard*>::iterator it;

	for ( it = m_keyboards.begin() ; it < m_keyboards.end(); it++ ) {
		if (label == (*it)->getLabel()) {
			InputEngine::cKeyboard* temp = (*it);
			delete temp;
			temp = nullptr;
			m_keyboards.erase(it);
			//break out of the for loop
			break;
		}
	}
}

/* Deletes all keyboards in cInput internal list */
void cInput::DeleteAllKeyboards()
{
	m_keyboards.clear();
}

/* Removes the keyboard from cInput internal list */
void cInput::RemoveKeyboard( const cString& label )
{
	std::vector<InputEngine::cKeyboard*>::iterator it;

	for ( it = m_keyboards.begin() ; it < m_keyboards.end(); it++ ) {
		if (label == (*it)->getLabel()) {
			m_keyboards.erase(it);
			//break out of the for loop
			break;
		}
	}
}

/* Removes all keyboards from cInput internal list. */
void cInput::RemoveAllKeyboards()
{
	m_keyboards.erase(m_keyboards.begin(), m_keyboards.end());
}

void cInput::DeleteAllInputs()
{
	DeleteAllKeyboards();
}

void cInput::RemoveAllInputs()
{
	RemoveAllKeyboards();
}

///Sets
		
///Gets
/* Gets return true if Input was initialized */
const bool cInput::IsInit() const
{
	bool rtn = false;
	if (SDL_WasInit(SDL_INIT_JOYSTICK) != 0) {
		cUtility::Inst().Message("Input is initialized.");
		rtn = true;
	} else
		cUtility::Inst().Message("Input is not initialized.");
	return rtn;
}

