/**
* FX Parse
* @version: 12-28-2004
* @author Joseph 'Pcjoe' Florencio
*/

#ifndef FXPARSE_H
#define FXPARSE_H

// script folders/names
#define FX_SCRIPT_MAIN_DIR	"fxscript"
#define FX_SCRIPT_INDEX		"index.xml"

struct precache_list_s
{	std::vector<std::string> vecList;
};

class CParseMain;
extern CParseMain g_ParseMain;


/*
* Base Parse Class
*/
class CParseBase
{	

public:
	/*
	* Print Error Parse
	* @param bool ok: If false, print error message and return true
	* @param std::string strFile: File we cant parse
	*/
	bool errorParse( bool ok, std::string strFile );

	/*
	* Get Base Dir
	*/
	std::string getBaseDir( void );

	/************************
	 * XML Interface
	 ************************/
	/*
	* Get Attribute
	*/
	std::string getAttribute( TiXmlNode* pNode, std::string strAttributeName );
		
protected:

	/*
	* Add interface
	*/
	void addInterface( void );

	/*
	* Pop interface (remove latest interface)
	*/
	void popInterface( void );

	/*
	* Clear interface(s)
	*/
	void clearInterfaces( void );

	/*
	* Get Current Interface
	*/
	CXML_Interface *getInterface( void );

	std::vector<CXML_Interface> m_XMLInterface;
	std::vector<CXML_Interface>::iterator m_iterXMLInterface;
};


/*
* Generic Parse Class
*/
class CParseGeneric : public CParseBase
{
public:
	// Constructor
	CParseGeneric( );
	
	/*
	* Reset
	*/
	virtual void Reset( void );


	/************************
	 * File Iterator
	 ************************/
public:
	/*
	* Clear File List
	*/
	void clearFileList( void );

	/*
	* Set File List
	*/
	void setFileList( std::vector<std::string> fileList );

protected:
	
	/*
	* Get Current File Iterator
	*/
	std::vector<std::string>::iterator getFileIter( void ) { return m_fileListIter; }

	/*
	* Update File Iterator
	*/
	void updateFileIter( std::vector<std::string>::iterator iterFile ) { m_fileListIter = iterFile; }

	
	std::vector<std::string> m_fileList;
	std::vector<std::string>::iterator m_fileListIter;


	/************************
	 * Element Management
	 ************************/
public:
	/*
	* Print
	*/
	void Print( void );

public:
	/*
	* Find Script Element
	*/
	CFXStorage *findScriptElement( std::string strFind );

	/*
	* Add System
	*/
	void addSystem( std::string strName, CFXStorage fxStorage );

protected:
	// List of elements
	std::map<std::string,CFXStorage> m_Elements;


	/************************
	 * Generic Load System
	 ************************/
public:
	/*
	* Manage Files
	*/
	virtual bool manageFiles( std::string strSearch );
	
protected:
	/*
	* Can Derive
	*/
	virtual bool canDerive( void ) { return true; }

	/*
	* Load Derived System
	*/
	virtual CFXStorage *loadDerive( TiXmlNode* pSystem, std::string strDerive );
	
	/*
	* Load Element
	*/
	virtual CFXStorage loadElement( TiXmlNode* pSystem, CFXStorage *pDerive );

	/*
	* Setup Derive
	*/
	virtual CFXStorage *setupDerive( TiXmlNode* pSystem );

	/*
	* Setup System
	*/
	virtual void setupSystem( TiXmlNode* pSystem, std::string strName );

	/*
	* Iterate Systems
	*/
	virtual bool iterateSystems( TiXmlNode* pIndexNode, std::string strSearch );
};

/*
* Parse FX
*/
class CParseFX : public CParseGeneric
{
};

/*
* Main Parse Compilation
*/
class CParseMain : public CParseBase
{
public:
	/*
	* Initialize
	*/
	void Initialize( void );

	/*
	* Reset
	*/
	void Reset( void );

	/*
	* Get Precache
	*/
	CFXPrecache *getPrecache( std::string strPrecache );

	/*
	* get Parse FX
	*/
	CParseFX *getParseFX( void ) { return &m_ParseFX; }

private:
	/*
	* Get Path
	*/
	std::string getPath( TiXmlNode* pPath );

	/*
	* Parse XML File(s)
	*/
	void parseXML( void );

	/*
	* Parse XML Index Fle
	*/
	bool parseIndex( TiXmlNode* pIndexNode );

	
	/************************
	 * Precache
	 ************************/

	/*
	* Manage Precache
	*/
	bool managePrecache( std::vector<std::string> &precacheList );

	// Precache list
	std::map<std::string,CFXPrecache> m_Precache;

	/************************
	 * Index List
	 ************************/

	/*
	* Get Index List
	*/
	std::vector<std::string> getIndexList( std::string strIndex );

	/*
	* Add Index List
	*/
	void addIndexList( std::string strPath, precache_list_s list );

	// List of parse files
	std::map<std::string,precache_list_s> m_IndexList;

	// Parse FX
	CParseFX m_ParseFX;
};




#endif