#include "imageloader.h"
#include "storage.h"
#include "dynamic.h"

#include <string>
#include <iostream>
#ifdef Revision
#undef Revision
#endif
#define Revision "me_imageloader.cpp $Revision: 1.5 $"

#ifdef HAVE_CONFIG_H
#include <conf_config.h>
#endif

static Storage<std::string, Image_Loader*> _loaders;
static std::string last_error;

bool imageloader_create ( std::string name, std::string driver, bool replace )
{
	if ( _loaders.exist ( name ) )
	{
		if ( replace )
		{
			Image_Loader *z;
			z=_loaders.get ( name );
			delete z;
			_loaders.remove ( name );
		}
		else
		{
			last_error = name + " already exist";
			return false;
		}
	}
	if ( !module_load ( driver ) )
	{
		last_error = dynamic_error();
		return false;
	}
	Dynamic_Module *mod;
	if ( ( mod = module_get ( driver ) ) == 0 )
	{
		last_error = dynamic_error();
		return false;
	}
	if ( !mod->_symbol_load ( "_get_imageloader" ) )
	{
		module_remove ( driver );
		last_error = dynamic_error();
		return false;
	}
	Image_Loader* ( *fun ) ( void );
	if ( ( fun = ( Image_Loader * ( * ) ( void ) ) ( *mod ) [ "_get_imageloader" ] ) == 0 )
	{
		module_remove ( driver );
		mod->_symbol_remove ( "_get_imageloader" );
		last_error = dynamic_error();
		return false;
	}
	Image_Loader *intf;
	intf=fun();
	last_error="";
	std::cerr<<"Loader "<<name<<" loaded from file "<<driver<<std::endl;
	return _loaders.insert ( name, intf );
}

bool imageloader_remove ( std::string name )
{
	last_error="";
	if ( !_loaders.exist ( name ) ) return true;
	Image_Loader *z;
	z=_loaders.get ( name );

	Dynamic_Module *mod;
	if ( ( mod = module_get ( z->_get_driver_name() ) ) == 0 )
	{
		last_error = dynamic_error();
	}
	else mod->_symbol_remove ( "_get_imageloader" );
	module_remove ( z->_get_driver_name() );
	delete z;
	return _loaders.remove ( name );
}
bool imageloader_loadimage ( std::string loader_name, std::string video_name, std::string image_name, std::string group_name, std::string surface_name, bool overwrite )
{
	std::cerr<<"loading file:"<<image_name<<std::endl;
	std::cerr<<"loader:"<<loader_name<<std::endl;
	Image_Loader *z;
	if ( !_loaders.exist ( loader_name ) )
	{
		last_error="No such loader \""+loader_name+"\"";
		
		return false;
	}	
	z=_loaders.get ( loader_name );
	return z->_loadimage ( video_name, image_name, group_name, surface_name, overwrite );
}

bool imageloader_loadimage_noext ( std::string video_name, std::string image_name, std::string group_name, std::string surface_name, bool overwrite )
{
	std::cerr<<__FUNCTION__<<std::endl;
	bool state=false;
	std::map<std::string, Image_Loader*>::iterator it;
	for(it=_loaders.get_map().begin();it!=_loaders.get_map().end();++it)
	{
		std::cerr<<"trying to load: "<<image_name+"."+it->first<<std::endl;
		state=it->second->_loadimage(video_name, image_name+"."+it->first, group_name, surface_name, overwrite);
		if(state)return state;
	}
	return false;
}


std::string imageloader_error( )
{
	return last_error;
}
std::string imageloader_version( )
{
	return VERSION;
}

void imageloader_seterror(std::string text)
{
	last_error=text;
}
