#include "cKeyboard.hpp"

using InputEngine::cKeyboard;

cKeyboard::cKeyboard( const cString& label )
	: m_label(label)
{}

cKeyboard::cKeyboard( const cKeyboard& copy )
	:m_label(copy.getLabel())
{}

/*virtual*/ cKeyboard::~cKeyboard()
{
	DeleteAllKeys();
}

///Functions
/*virtual*/ void cKeyboard::KeyboardCheck( const SDL_Event& event )
{
	SDL_Keycode key = event.key.keysym.sym;

	std::vector<InputEngine::cKey*>::iterator it;

	for ( it = m_keys.begin() ; it < m_keys.end(); it++ ) {
		if (key == (*it)->getKey()) {
			switch( event.type )
			{
			case SDL_KEYUP:
				(*it)->KeyUP();
				break;
			case SDL_KEYDOWN:
				(*it)->KeyDown();
				break;
			}
			//break out of the for loop
			break;
		}
	}
}

void cKeyboard::AddKey( InputEngine::cKey* key )
{
	m_keys.push_back(key);
}

void cKeyboard::DeleteKey( const SDL_Keycode& key )
{
	std::vector<InputEngine::cKey*>::iterator it;

	for ( it = m_keys.begin() ; it < m_keys.end(); it++ ) {
		if (key == (*it)->getKey()) {
			InputEngine::cKey* temp = (*it);
			delete temp;
			temp = nullptr;
			m_keys.erase(it);
			//break out of the for loop
			break;
		}
	}
}

void cKeyboard::DeleteAllKeys()
{
	m_keys.clear();
}

///Sets
const cString& cKeyboard::getLabel() const
{
	return m_label;
}