#ifndef DEXIBUILD_HPP
#define DEXIBUILD_HPP

#pragma warning (disable : 4786)
#pragma warning (push, 3)
#include "tinyxml_pull/tinyxml.h"
#include <map>
#pragma warning (pop)

#define USING_DESTINY3D
#define DESTINY3D_STATIC_LINK

#include "Destiny3D.hpp"
#include "deFileSystem.hpp"
#include "deFile.hpp"
#include "deWorld.hpp"
#include "deScene.hpp"
#include "deString.hpp"

typedef void (* dexiDocHandler)(TiXmlElement* pRoot, deString2 inputFileName);

class IDexiBuilder
{
protected:
	virtual ~IDexiBuilder() {}
public:
	virtual void RegisterDexiHandler(const char* targetName, dexiDocHandler h) = 0;
};

class DexiBuilder : public IDexiBuilder
{
public:
	DexiBuilder();
	~DexiBuilder();
	void RegisterDexiHandler(const char* targetName, dexiDocHandler h);

	void loadFile(deString2 filename);
	void loadBatch(deString2 batchDir, bool doSubDirs);
	void handleDoc(TiXmlDocument* pDoc, deString2 inputFileName);
	
private:
	typedef std::map<deString2, dexiDocHandler>handlermap;
	handlermap m_Handlers;
};

enum LogLevel
{
	Log_Info,
	Log_Message,
	Log_Warning,
	Log_Error,
};
void LogExportMsg(int level, const char* format, ...);
class dexiException
{
private:
	void operator=(const dexiException& ) { }
public:
	const deString2 what;
	dexiException() {}
	dexiException(const deString & msg) : what(msg.const_str()) {}
	dexiException(const char * msg) : what(msg) {}
	~dexiException() {}
};

struct dexiVertexSet
{
public:
	deTArray<deVec3d> unique_positions;
	deTArray<u32> pos_bindings;
	deTArray<deVertex> vert_positions;
	deTArray<deVertex> normals;
	deTArray<deColor> colors;
	deTArray< deTArray<deTexCoord3> > texcoords;
};

struct dexiTriMeshSub
{
public:
	struct triface_t
	{
		u32 v0, v1, v2;
	};
	deString2 surface_name;
	deTArray<triface_t> faces;
};

struct dexiTriMesh
{
public:
	dexiTriMesh() : instance_template(NULL), instance_owner(deFALSE) {}
	deString2 vertexpool;
	deTArray<dexiTriMeshSub> submeshes;
	IdeMesh* instance_template;
	deBoolean instance_owner;
};

struct dexiScene
{
public:
	deTArray<IdeSceneStaticMesh*> instance_meshes;
	deTArray<IdeMesh*> fixed_meshes;
	deTArray<deTransformInfo> fixed_transforms;
};

#endif