/*********************************************************************//**
*	Textures pool
*	Implementace podpurneho nastroje knihovny textur,
*	umoznuje shlukovat texury do skupin.
*
*	author: Michal Jirous
*	date: 20.03.2009
*	file: textureslib_pool.cpp
**********************************************************************/

#include "textureslib.h"
using namespace std;
void TexturePool::Pool_checkTexturesUsage()
{
	if( m_TexturesSharedPool.empty() )
		return;
	for( map<string,TexturePooled>::iterator iter = m_TexturesSharedPool.begin(); iter != m_TexturesSharedPool.end();  )
		if( iter->second.reference_counter == 0 )
		{
			iter->second.unconnect();
			iter = m_TexturesSharedPool.erase( iter );
		}
		else
			++iter;
}




TexturePooled *TexturePool::Pool_getTexture( std::string key )
{
	if( m_TexturesSharedPool.empty() )
		return NULL;
	map<string,TexturePooled>::iterator iter = m_TexturesSharedPool.find( key );
	if( iter != m_TexturesSharedPool.end() )
		return &iter->second;
	
	//TODO
	for( size_t i = 0; i != key.length(); ++i )
	{
		char &c = key[i];
		if( isalpha(c) )
			c = tolower( c );
	}

	iter = m_TexturesSharedPool.find( key );
	if( iter != m_TexturesSharedPool.end() )
		return &iter->second;

	return NULL;

}


TexturePooled *TexturePool::Pool_applyTexture( std::string key)				/*!< @brief Nacteni textury a prirazenych animaci, pokud je potreba. */
{
	TexturePooled* pTmp = Pool_getTexture( key );
	if( !pTmp )
	{
		pair<std::map<std::string,TexturePooled>::iterator,bool> ret = m_TexturesSharedPool.insert( make_pair( key,TexturePooled() ) );
		if( !ret.second )
			return NULL;

		ret.first->second.m_pTextureElement = textureLibrary.applyTexture( key );
		pTmp = &ret.first->second;
		
	}
	pTmp->reference_counter++;
	return pTmp;
}

//TexturePooled *Pool_applyTexture( TextureElement *element );			/*!< @brief Nacteni textury a prirazenych animaci, pokud je potreba. */
//TexturePooled *Pool_applyTexture( SDL_RWops *rw, int type = PICTURE_DEFAULT);	/*!< @brief Nacteni textury, pokud je potreba. */
//TexturePooled *Pool_applyTexture( TextureElement *current, SDL_RWops *rw);		/*!< @brief Nacteni textury, pokud je potreba. */

void TexturePool::Pool_animNextAll()
{
	for( map<string,TexturePooled>::iterator iter = m_TexturesSharedPool.begin(); iter != m_TexturesSharedPool.end(); ++iter )
		iter->second.animTexture();

}
void TexturePool::Pool_switchNextAll()
{
	for( map<string,TexturePooled>::iterator iter = m_TexturesSharedPool.begin(); iter != m_TexturesSharedPool.end(); ++iter )
		iter->second.switchTexture();
}

void TexturePool::Pool_destroy()
{
	if( m_TexturesSharedPool.empty() )
		return;
	for( map<string,TexturePooled>::iterator iter = m_TexturesSharedPool.begin(); iter != m_TexturesSharedPool.end(); ++iter )
		{
			iter->second.unconnect();
			//iter = m_TexturesSharedPool.erase( iter );
		}
	m_TexturesSharedPool.clear();
}





void TexturePooled::bind()
{
	if( m_pTextureElement )
		texBind( m_pTextureElement->texture_id );
}
void TexturePooled::switchTexture()
{
	if( m_pTextureElement )
		m_pTextureElement = m_pTextureElement->switcher;
}
void TexturePooled::animTexture()
{
	if( m_pTextureElement )
		m_pTextureElement = m_pTextureElement->animNext;
}
void TexturePooled::unload()
{
	reference_counter -= min( reference_counter, 1u );
}

void TexturePooled::unconnect()
{
	textureLibrary.unloadTexture( m_pTextureElement );
}
