#include "core.h"
#include "log.h"
#include "entitymanager.h"

static EntityManager s_entityManager;
EntityManager* g_entityManager = &s_entityManager;

void EntityManager::Init()
{
	Logger::Msg("Initializing entity manager ...");
	RegisterEntities();
}

void EntityManager::Shutdown()
{

}

void EntityManager::RegisterEntities()
{
	SDL_assert(EntityRegistrator::ms_entityRegistrators);

	for (EntityRegistrator* entityReg = EntityRegistrator::ms_entityRegistrators;
		entityReg != nullptr;
		entityReg = entityReg->m_nextEntityRegistrator)
	{
		m_entityRegistrationInfo.push_back(entityReg->m_entityRegInfo);
	}

	Logger::Msg("total registered %i entities", m_entityRegistrationInfo.size());
}

void EntityManager::Update(float dt)
{
	//for (std::vector<IEntityBase*>::iterator it = m_entities.begin();
	//	it != m_entities.end();
	//	++it)
	//{
	//	IEntityBase* entity = (*it);
	//	if (entity) {
	//		entity->Update(dt);
	//	}
	//}
}

IEntityBase* EntityManager::CreateEntity(const char* classname)
{
	for (std::vector<EntityRegistrationInfo>::iterator it = m_entityRegistrationInfo.begin();
		it != m_entityRegistrationInfo.end();
		++it)
	{
		EntityRegistrationInfo& info = (*it);
		if (strcmp(info.m_entityName, classname) == 0) {
			IEntityBase* entity = info.m_createEntityFunc();
			entity->SetClassname(info.m_entityName);
			//m_entities.push_back(entity);
			return entity;
		}
	}

	Core::Error("EntityManager::CreateEntity: can't create entity with unknown '%s' class name!", classname);

	return nullptr;
}
