///////////////////////////////////////////////////////////////////////////////
// MW2MechImporter.h
// Copyright Bjoern Ganster
// Developed since 2009
///////////////////////////////////////////////////////////////////////////////

#ifndef MW2MechImporter__h
#define MW2MechImporter__h

#include "Texture.h"
#include "TextureCompiler.h"
#include "MechWarriorIIPRJ.h"

// Import MW2 files for use in internal engine
size_t importMW2Files(const PathChar* source, int cdType);

///////////////////////////////////////////////////////////////////////////////
// Help structs

struct MW2TextureTableEntry {
   int BMID;   // ID assigned in BMID tag in BWD
   int BMPJ; // Number of file according to BMPJ tag in BWD
   Texture* texture;
   ManagedTexture* manText;
};

///////////////////////////////////////////////////////////////////////////////
// Class for importing MW2 mechs

class MW2MechImporter FINAL: public VirtualClass {
public:
	// Constructor
	MW2MechImporter(int cdType);

	// Destructor
	~MW2MechImporter() OVERRIDE;

	// Open MW2.PRJ
	bool setPRJ(const PathChar* PRJFN);

	// Import
	int import();

	// Get number of imported mechs
	size_t getImportedMechCount() const;

private:
	MechWarriorIIPRJ prj;
	int mechsImported;
	Palette palette;
	int mCdType;
	std::vector <MW2TextureTableEntry*> textures;

	// Copy data from mw2-mechs.xml into a new .mvmk file
	void createMVMKFile (XMLTreeConstIterator& importXmlIter, 
		const PathChar* outputPath, const PathChar* outputFN);

	// Copy MW2 mech config file (*.MEK)
	void addMW2Stats (XMLTreeConstIterator& MW2MechTreeIter, 
		const PathChar* outputPath);

	// Copy single mech's limbs from MW2.PRJ
	bool copyMechComponents (XMLTreeConstIterator& MW2MechTreeIter, 
		const PathChar* targetPath, const char* mechName);

	// Load mech limb from MemoryBlock
	Mesh* loadLimb(const MemoryBlock* block, TextureCompiler& tc, const BGCPString& limbName);

	// Build texture table
	bool buildTextureTable (const char* BWDFN);
	bool buildTextureTable (DWORD fileNum);

	// Retrieve texture, loading it when necessary
	ManagedTexture* getTexture(TextureCompiler& tc, int textureID);

	// Set palette
	void setPalette (MemoryBlock* mb);
};

#endif
