#include "cEventControl.hpp"

/*** Custom Header Files ***/
#include "../InputEngine/cInput.hpp"
#include "../MathEngine/iVector/iVector2.hpp"
#include "../GUIEngine/cGUI.hpp"

using EventEngine::cEventControl;
using MathEngine::iVector2;

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

//Private
cEventControl::cEventControl()
{}

cEventControl::~cEventControl()
{
	CleanUp();
	for (unsigned int i = 0; i < m_children.size(); ++i) {
		delete m_children[i];
		m_children[i] = nullptr;
	}
	m_children.clear();
}

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

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

//Functions
const bool cEventControl::Initialize() const
{
	return true;
}

const bool cEventControl::Setup()
{
	return true;
}

void cEventControl::CleanUp()
{
}

/* Checks for Events */
void cEventControl::CheckEvents()
{
	SDL_Event event;
	while ( SDL_PollEvent(&event)) {
		std::vector<EventEngine::cEvent*>::iterator it;

		for (it = m_children.begin(); it < m_children.end(); it++) {
			(*it)->OnEvent(event);
		}
// 		iVector2 location = { 0, 0 };
// 		switch (event.type)
// 		{
// 		case SDL_KEYUP:
// 		case SDL_KEYDOWN:
// 		case SDL_JOYAXISMOTION:
// 		case SDL_JOYBALLMOTION:
// 		case SDL_JOYHATMOTION:
// 		case SDL_JOYBUTTONDOWN:
// 		case SDL_JOYBUTTONUP:
// 			InputEngine::cInput::Inst().CheckInputs(event);
// 			break;
// 		case SDL_MOUSEBUTTONDOWN:
// 			
// 			SDL_GetMouseState(&location.x, &location.y);
// 			GUIEngine::cGUI::Inst().Event(location, GUIEngine::cGUI::eGUIEventType::MouseButton1);
// 			break;
// 		case SDL_MOUSEBUTTONUP:
// 			break;
// 		}
	}
}

void cEventControl::AddEvent(EventEngine::cEvent* event)
{
	m_children.push_back(event);
}

const bool cEventControl::RemoveEvent(EventEngine::cEvent* event)
{
	bool rtn = false;
	std::vector<EventEngine::cEvent*>::iterator it;

	for (it = m_children.begin(); it < m_children.end(); it++) {
		if (event == (*it)) {
			m_children.erase(it);
			rtn = true;
			//break out of the for loop
			break;
		}
	}
	return rtn;
}

std::vector<EventEngine::cEvent*>& cEventControl::GetEvents()
{
	return m_children;
}


///Sets

///Gets
/* Gets return true if Event was initialized */
const bool cEventControl::IsInit() const
{
	return true;
}