/*
	This method loads all the sprite types found in the provided sprite type list file
	into the game state manager, including their images.
*/
bool PoseurSpriteTypesImporter::loadSpriteTypes(Game *game, wstring spriteTypesListFileName)
{
	int slashIndex = spriteTypesListFileName.rfind('/');
	dir = string(spriteTypesListFileName.begin(), spriteTypesListFileName.end()).substr(0, slashIndex+1);
	const char *spriteTypesListFile = newCharArrayFromWstring(spriteTypesListFileName);
	bool success = loadSpriteTypesListInfo(spriteTypesListFile);
	if (!success) return false;
	for (unsigned int i = 0; i < spriteTypes.size(); i++)
	{
		success = loadSpriteTypeInfo(spriteTypes[i]);
		if (!success) return false;
	}

	TextureManager *tm = game->getGraphics()->getWorldTextureManager();
	WStringTable *wStringTable = tm->getWStringTable();

	// NOW LET'S USE ALL THE INFO WE'VE LOADED 
	// LET'S START BY LOADING THE TEXTURES INTO THE WORLD TEXTURE MANAGER
	for (unsigned int i = 0; i < spriteTypes.size(); i++)
	{
		string spriteType = spriteTypes[i];
		unsigned int offset = wStringTable->getNumWStringsInTable();
		map<int, string> images = spriteTypesImages[spriteType];
		for (int j = 0; j < images.size(); j++)
		{
			string imageToLoad = images[j];
			wstring wImageToLoad(imageToLoad.begin(), imageToLoad.end());
			tm->loadTexture(wImageToLoad);
		}

		AnimatedSpriteType *ast = new AnimatedSpriteType();
		unsigned int spriteTypeId = game->getGSM()->getSpriteManager()->addSpriteType(ast);
		ast->setSpriteTypeID(spriteTypeId);
		Dimensions dim = spriteTypesDimensions[spriteType];
		ast->setTextureSize(dim.width, dim.height);
		
		map<string, vector<Pose>> animations = spriteTypesAnimationsLists[spriteType];
		map<string, vector<Pose>>::iterator it = animations.begin();
		while (it != animations.end())
		{
			string key = it->first;
			wstring wKey(key.begin(), key.end());
			ast->addAnimationSequence(wKey);
			vector<Pose> poseList = it->second;
			vector<Pose>::iterator poseIt = poseList.begin();
			while (poseIt != poseList.end())
			{
				Pose pose = *poseIt;
				ast->addAnimationFrame(wKey, pose.imageId + offset - 1, pose.duration);
				poseIt++;
			}				
			it++;
		}
	}


	return true;
}
bool TMXMapImporter::loadMapInfo()
{
	// LOAD THE XML DOC
	const char *charPath = newCharArrayFromWstring(mapLevelFilePath);
	TiXmlDocument doc(charPath);
	delete charPath;
	bool loadOkay = doc.LoadFile();
	if (loadOkay)
	{
		TiXmlElement *pElem = doc.FirstChildElement();
		string eName;
		if (pElem)
		{
			eName = pElem->Value();
			if (strcmp(eName.c_str(), MAP_ELEMENT.c_str()) == 0)
			{
				// WHAT'S THE TYPE OF MAP?
				const char* orientation = pElem->Attribute(ORIENTATION_ATT.c_str());
				if (strcmp(orientation, ORTHOGONAL_VAL.c_str()) == 0)
				{
					mapType = MapType::ORTHOGONAL_MAP;
					return loadOrthographicMap(pElem);
				}
				else
				{
					mapType = MapType::ISOMETRIC_MAP;
					return loadIsometricMap(pElem);
				}
			}
			else
			{
				return false;
			}
		}
	}
	else
	{
		return false;
	}
	return true;
}