#include "Map.h"
#include "../Basic.h"
#include <sstream>
#include <string>
#include <Resource/DataArray.h>

namespace assets
{
	void Map::load(gge::resource::Folder &folder)
	{
		int index = 0;

		gge::resource::DataArray &info = folder.Get<gge::resource::Folder>(0).Get<gge::resource::DataArray>(0);
		startingPopulation=info.getInt(3);
		startingGold=info.getInt(6);
		startingHour=info.getInt(7);
		populationLimit=info.getInt(5);

		// create tile map
		gge::resource::DataArray &dataArray = folder.Get<gge::resource::DataArray>(1);
		gge::utils::Size size = dataArray.getSize(index++);
		tileMap.Resize(size);

		for(int y = 0; y < size.Height; ++y)
		{
			std::stringstream ss1;
			ss1.str(dataArray.getString(index++));

			for(int x = 0; x < size.Width; ++x)
			{
				std::string variant, ID;
				std::stringstream ss2;
				int tileIndex;

				// get ID
				std::getline(ss1, ID, '|');

				// get variant
				std::getline(ss1, variant, ',');

				ss2.str(variant);

				// parse variant into integer
				ss2 >> tileIndex;

				// pick appropriate tile with given index
				tileMap(x,y) = &TileInfo::list[ID]->tiles[tileIndex];
			}
		}

		// get all objects into objects vector
		ObjectInfo temp;
		index = 0;
		dataArray = folder.Get<gge::resource::DataArray>(3);
		for(int i = 0; i < dataArray.GetCount(); i += 3)
		{

			temp.ID		  = dataArray.getString(index++);
			temp.type	  = (ObjectType)dataArray.getInt(index++);
			temp.location = dataArray.getPoint(index++);

			objects.push_back(temp);
		}

		// push all events into event vector
		Event temp1;
		index = 0;
		dataArray = folder.Get<gge::resource::DataArray>(4);
		for(int i = 0; i < dataArray.GetCount(); i += 5)
		{
			temp1.title     = dataArray.getString(index++);
			temp1.type	   = (EventType)dataArray.getInt(index++);
			temp1.condition = dataArray.getString(index++);
			temp1.imageID   = dataArray.getString(index++);
			temp1.text	   = dataArray.getString(index++);

			events.push_back(temp1);
		}
	}
}