#include "Manager.h"
#include "Event.h"
#include "GameObject.h"
#include "GameObjectList.h"
#include <string>

Manager::Manager(){
	this->_event[0]="step";
	this->_event[1]="keyboard";
	this->_event[2]="mouse";
	this->_event[3]="collision";
	//this->_event[4]="coin";
}
Manager::~Manager(){}
  
/// Send event to all interested objects
void Manager::onEvent(Event *p_event){
	for(int i=0;i<MAX_EVENTS;i++){
		if(p_event->getType().compare(this->_event[i])==0){
			for(int j=0;j<MAX_GAME_OBJECTS;j++){
				if(obj_list[i].getAt(j)!=NULL){
					this->obj_list[i].getAt(j)->eventHandler(p_event);
				}
			}
		}
	}
}

/// Indicate interest in event.
/// Return 0 if ok, else -1.
int Manager::registerInterest(GameObject *p_go, string event_name){
	for(int i=0;i<MAX_EVENTS;i++){
		if(event_name==this->_event[i]){
			this->obj_list[i].insert(p_go);
			return i;
		}
	}
	//if event isnt defined, return error
	return -1;
}

/// Indicate no longer interest in event
/// Return 0 if ok, else -1.
int Manager::unregisterInterest(GameObject *p_go, string event_name){
	for(int i=0;i<MAX_EVENTS;i++){
		if(event_name.compare(this->_event[i])==0){
			this->obj_list[i].remove(p_go);
		}
	}
	//if event isnt defined, return error
	return -1;
}

/// Startup the Manager.
/// Return 0 if ok, else negative number
int Manager::startUp(){
	return 0;
}

/// Shutdown the Manager.
void Manager::shutDown(){

}

int Manager::addEvent(string event_name){
	for(int i=0;i<MAX_EVENTS;i++){
		if(_event[i]==""){
			this->_event[i]=event_name;
			return i;
		}
	}
	return -1;
}