#include "Buildings.h"
#include "../Utils.h"
#include <sstream>

namespace assets
{
	std::map<std::string, MilitaryBuilding*> MilitaryBuilding::list;
	std::map<std::string, TownBuilding*> TownBuilding::list;
	std::map<std::string, Den*> Den::list;

	void TownBuilding::load(gge::resource::DataArray &dataArray)
	{
		// load inherited data members and return current index
		int index = Object::load(dataArray);

		HP				   = dataArray.getFloat(index++);
		armor			   = dataArray.getFloat(index++);
		odds			   = dataArray.getFloat(index++);
		spacingRequirement = dataArray.getInt(index++);
	}

	void MilitaryBuilding::load(gge::resource::DataArray &dataArray)
	{
		// load inherited data members and return current index
		int index = Object::load(dataArray);

		type			 = Type(dataArray.getInt(index++));
		HP				 = dataArray.getFloat(index++);
		armor			 = dataArray.getFloat(index++);
		constructionCost = dataArray.getFloat(index++);
		constructionTime = dataArray.getFloat(index++);

		// get all points into string stream
		std::stringstream ss(dataArray.getString(index++));
		gge::utils::Point temp;

		// loop until end of ss
		while(!ss.eof())
		{
			// read ss to a gge::utils::Point object
			ss >> temp;

			if(ss.fail())
			{
				break;
			}

			// push read point into vector
			usablePoints.push_back(temp);
		}

		capacity	= dataArray.getInt(index++);
		storageType = Item::Type(dataArray.getInt(index++));
		maxLvl		= dataArray.getInt(index++);
		XPPerHit	= dataArray.getInt(index++);
	}

	void Den::load(gge::resource::Folder &folder)
	{
		// a den is contained in a folder
		// first element of folder is a DataArray and contains den info
		// the other elements of folder are DataArrays
		// containing the monster info(SpawnInfo) that can spawn from the den
		gge::resource::DataArray &dataArray = folder.Get<gge::resource::DataArray>(0);

		// load inherited data members and return current index
		int index = Object::load(dataArray);

		HP				= dataArray.getFloat(index++);
		armor			= dataArray.getFloat(index++);
		initialAttackCD = dataArray.getFloat(index++);
		regularAttackCD = dataArray.getFloat(index++);



		int numOfMonsters = folder.GetCount();

		// since SpawnInfo DataArrays starts from second index of the folder(0 is being first)
		// the counter starts from 1
		// this loop reads all monsten info into our SpawnInfo vector
		for(int i = 1; i < numOfMonsters; ++i)
		{
			int spawnInfoIndex = 0;
			// get DataArray containing SpawnInfo
			dataArray = folder.Get<gge::resource::DataArray>(i);
			SpawnInfo temp;

			temp.ID		= dataArray.getString(spawnInfoIndex++);
			temp.spawnCount	= dataArray.getInt(spawnInfoIndex++);
			temp.initialSpawnCD = dataArray.getFloat(spawnInfoIndex++);
			temp.regularSpawnCD = dataArray.getFloat(spawnInfoIndex++);
			temp.monsterLevel	= dataArray.getInt(spawnInfoIndex++);
			temp.maxMonster		= dataArray.getInt(spawnInfoIndex++);

			wildlings.push_back(temp);
		}
	}
}

