#include <string>
#include <vector>
#include <Widgets/Main.h>
#include "../../Source/Assets/Load.h"
#include "../../Source/Graphics/Map.h"
#include "Object.h"
#include <Buildings.h>
#include "Instantiation.h"
#include <iosfwd>
#include "../Mechanics/Map.h"
using namespace std;

/*
assets::Object* findObject(Type type, std::string ID)
{
	switch(type)
	{
	case Obstacle:
		return assets::Obstacle::list[ID];
		break;
	case TownBuilding:
		return assets::TownBuilding::list[ID];
		break;
	case MilitaryBuilding:
		return assets::MilitaryBuilding::list[ID];
		break;
	case Den:
		return assets::Den::list[ID];
		break;
	default:
		return NULL;
		break;
	}
}*/

int Application(std::vector<std::string> &arguments) 
{ 
	gge::widgets::InitializeApplication("Drawing", "Drawing", 600, 600);
	gge::os::window::Destroyed.RegisterLambda([]{
		gge::Main.Exit(0);
	});




	gge::resource::File mapFile;

	assets::loadAll();
	assets::resolve();

	mapFile.AddGameLoaders();
	mapFile.LoadFile("map.gor");
	mapFile.Prepare();

	/*gge::resource::DataArray &dataArray = mapFile.Root().Get<gge::resource::Folder>(1);*/
	/*gge::resource::DataArray &objects = mapFile.Root().Get<gge::resource::DataArray>(3);*/


	assets::loadAll();
	assets::resolve();

	graphics::Map::initialize(gge::Main.BoundingBox.GetSize());
	assets::Map mapProvider;
	mapProvider.load(mapFile.Root());

	createMap(mapProvider);

	ofstream file("pm.txt");

	file << mechanics::Map::current().getPointInfoMap();

	int index = 0;
/*
	for(int i = 0; i < objects.GetCount(); i += 3)
	{
		std::string ID = objects.getString(index++);
		Type type = (Type)objects.getInt(index++);
		gge::utils::Point location = objects.getPoint(index++);
		
		map.getObjectVector().push_back(findObject(type, ID));
	}*/

	
	float SCROLL_AMOUNT = 500.f;

	gge::input::keyboard::Events.RegisterLambda([&](gge::input::keyboard::Event event) -> bool
	{
		return false; 
	});

	gge::utils::Point2D actualScroll(0,0);

	unsigned last=gge::Main.CurrentTime;
	
	gge::Main.BeforeGameLoopEvent.RegisterLambda([&]{
		if(gge::input::keyboard::PressedKeys[gge::input::keyboard::KeyCodes::Left]) {
			actualScroll.x-=SCROLL_AMOUNT*(gge::Main.CurrentTime-last)/1000.f;
		}

		else if(gge::input::keyboard::PressedKeys[gge::input::keyboard::KeyCodes::Right]) {
			actualScroll.x+=SCROLL_AMOUNT*(gge::Main.CurrentTime-last)/1000.f;
		}

		else if(gge::input::keyboard::PressedKeys[gge::input::keyboard::KeyCodes::Up]) {
			actualScroll.y-=SCROLL_AMOUNT*(gge::Main.CurrentTime-last)/1000.f;
		}

		else if(gge::input::keyboard::PressedKeys[gge::input::keyboard::KeyCodes::Down]) {
			actualScroll.y+=SCROLL_AMOUNT*(gge::Main.CurrentTime-last)/1000.f;
		}


		graphics::Map::current().scrollTo(actualScroll);
		graphics::Map::current().drawObjects();
		graphics::Map::current().drawTiles();

		last=gge::Main.CurrentTime;
	});


	gge::Main.Add(graphics::Map::current().getBaseLayer());
	gge::Main.Run();
	return EXIT_SUCCESS;
}
