/**
* FX Precache
* @version: 12-26-2004
* @author Joseph 'Pcjoe' Florencio
*/

#include "fxsys.h"

// Constructor
CFXPrecache::CFXPrecache()
{	setType(FXPREC_NONE);
	m_pSoundSample = NULL;
}

// Constructor
CFXPrecache::CFXPrecache(std::string strPrecache)
{	CFXPrecache();
	precache(strPrecache);
}

/*
* Is Valid
*/
bool CFXPrecache::isValid( void )
{	if(getType()==FXPREC_NONE)
	{	return false;
	}
	return true;
}

/*
* Set type
*/
void CFXPrecache::setType( int iType )
{	m_iType = iType;
}

/*
* Get type
*/
int CFXPrecache::getType( void )
{	return m_iType;
}

/*
* Set WRL
*/
void CFXPrecache::setWRL( VRMLData VRMLData )
{	m_VRMLData=VRMLData;
	setType(FXPREC_WRL);
}

/*
* Get WRL
*/
VRMLData *CFXPrecache::getWRL( void )
{	if(getType()==FXPREC_WRL)
	{	return (&m_VRMLData);
	}
	return NULL;
}

/*
* Set SPR
*/
void CFXPrecache::setSPR( HSPRITE hsprSprite )
{	m_hsprSprite=hsprSprite;
	setType(FXPREC_SPR);
}

/*
* Get SPR
*/
HSPRITE *CFXPrecache::getSPR( void )
{	if(getType() == FXPREC_SPR)
	{	return (&m_hsprSprite);
	}
	return NULL;
}

/*
* Set Sample
*/
void CFXPrecache::setSample( FSOUND_SAMPLE *pSample )
{	m_pSoundSample=pSample;
	setType(FXPREC_SND);
}

/*
* Get Sample
*/
FSOUND_SAMPLE *CFXPrecache::getSample( void )
{	if(getType() == FXPREC_SND)
	{	return m_pSoundSample;
	}
	return NULL;
}

/*
* Precache
*/
bool CFXPrecache::precache( std::string strPrecache )
{	// Parse WRL
	if(strPrecache.find(".wrl") != -1)
	{	VRMLData VRMLParse;
		ParseVRML(strPrecache.c_str(), VRMLParse);

		// Valid Model
		if(VRMLParse.iNumberOfPolys != 0 && VRMLParse.iNumberOfVertices != 0)
		{	// Set WRL
			setWRL(VRMLParse);
			return true;
		}
		// Not valid, print error msg
		else
		{	string strPrint = "\"";
			strPrint += strPrecache + "\" is not a valid WRL file.\n";
			ConsolePrint(strPrint.c_str());
		}
	}
	// Parse SPR
	else if(strPrecache.find(".spr") != -1)
	{	HSPRITE hsprSprite = LoadSprite(strPrecache.c_str());

		// Valid Sprite
		if(hsprSprite > 0)
		{	// Add to list
			setSPR(hsprSprite);
			return true;
		}
		// Not valid, print error msg
		else
		{	string strPrint = "\"";
			strPrint += strPrecache + "\" is not a valid SPR file.\n";
			ConsolePrint(strPrint.c_str());
		}
	}
	// Parse Sound
	else if((strPrecache.find(".wav") != -1) || (strPrecache.find(".mp2") != -1) || (strPrecache.find(".mp3") != -1) || (strPrecache.find(".ogg") != -1) || (strPrecache.find(".raw") != -1))
	{	
		// Precache sound
		if(g_FMODWrapper.isLoaded())
		{	string strSoundPath = string(gEngfuncs.pfnGetGameDirectory()) + "/";
			strSoundPath += strPrecache;
			FSOUND_SAMPLE *pSample = g_FMODWrapper.FWRAPPER_Sample_Load(FSOUND_FREE,strSoundPath.c_str(), FSOUND_HW3D | FSOUND_LOOP_OFF, 0, 0);

			// Set sample
			if(pSample)
			{	setSample(pSample);
				return true;
			}
			// Not valid, print error msg
			else
			{	string strPrint = "\"";
				strPrint += strPrecache + "\" is not a valid sound file.\n";
				ConsolePrint(strPrint.c_str());
			}
		}
		else
		{	ConsolePrint("Can't precache sounds because FMOD did not load!\n");
		}	
	}
	// Unknown filetype, print error msg
	else
	{	string strPrint = "\"";
		strPrint += strPrecache + "\" is not a valid precache element.\n";
		ConsolePrint(strPrint.c_str());
	}
	setType(FXPREC_NONE);
	return false;
}