#include "graphicMapper.h"

GraphicMapper *GraphicMapper::instance = NULL;

GraphicMapper::GraphicMapper(void)
{
}

bool GraphicMapper::generateBaselineData( const ID baselineId, const string &planetType, BaselineData &dstData )
{
	

	calSpace::GraphicGeneratorContainer::iterator it;
	for ( it= this->graphicGeneratorDatabase.begin(); it != this->graphicGeneratorDatabase.end(); ++it )
	{
		if ( it->name == planetType )
		{
			//skip rest of for cycle, we found what we wanted
			break;
		}
	}

	if ( it != this->graphicGeneratorDatabase.end() )
	{
		/*
		srand( baselineId ); //warning this has a serious side effects!
		dstData.baselineType =  (BaselineType) getRandItemFromContainer< unsigned int, vector< unsigned int >>( it->baselineTypes );
		dstData.cloudName = getRandItemFromContainer< string, vector<string> >(  it->cloudNames );
		dstData.materialName = getRandItemFromContainer< string, vector<string> >( it->materialNames );
		dstData.skyboxName = getRandItemFromContainer< string, vector<string> >( it->skyboxNames );
		dstData.surfaceTexName = getRandItemFromContainer< string, vector<string> >( it->innerSurfaceTexNames );
		dstData.terrainScript = getRandItemFromContainer< string, vector<string> >( it->terrainScripts );
		*/

		dstData.baselineType	=	(BaselineType) it->baselineTypes.at( baselineId % it->baselineTypes.size());
		dstData.cloudName		=	it->cloudNames.at(  baselineId % it->cloudNames.size() );
		dstData.materialName	=	it->materialNames.at( baselineId % it->materialNames.size() ); 
		dstData.skyboxName		=	it->skyboxNames.at( baselineId % it->skyboxNames.size() );
		dstData.surfaceTexName	=	it->innerSurfaceTexNames.at( baselineId % it->innerSurfaceTexNames.size() );
		dstData.terrainScript	=	it->terrainScripts.at( baselineId % it->terrainScripts.size() );
		return true;
	}

	return false;
}

bool GraphicMapper::generateOuterSurfaceTexure( const ID planetId, const string &planetType, string &dstTexName )
{
	srand(  planetId ); //warning this has a serious side effects!
	rand(); rand(); rand(); rand(); rand(); rand(); //dont use first values...they dont look random 


	calSpace::GraphicGeneratorContainer::iterator it;
	for ( it= this->graphicGeneratorDatabase.begin(); it != this->graphicGeneratorDatabase.end(); ++it )
	{
		if ( it->name == planetType )
		{
			//skip rest of for cycle, we found what we wanted
			break;
		}
	}

	if ( it != this->graphicGeneratorDatabase.end() )
	{
		dstTexName = getRandItemFromContainer< string, vector<string> >( it->outerSurfaceTexNames );
		
		return true;
	}

	return false;
}

GraphicMapper* GraphicMapper::getInstance(void)
{
	if ( instance == NULL )
	{
		instance = new GraphicMapper;
	}

	return instance;
}
