#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include "ParseOSMfromFILE.h"
#include "OSMfunctions.h"

using namespace std;

// Main structure
DataFile_t osmDataHolder;
vector<int> Ways;

int main(int argc, char *argv[]) {
	
	cout.precision(6);
	cout.setf(ios::fixed);

	// Soubor s daty
	string filename = "map.osm";

	//Vystupni soubor
	string OUTFile = "ParseOSM.txt";

	//Vystupni soubor RNDF
	string RNDFoutFILE = "OutRNDF.txt";

	/**************************************************************************/

	// Nacteni dat do struktury
	parseFile(filename, osmDataHolder, Ways, OUTFile);
	

	/**************************************************************************/

	//Dekodovani RNDF
	ConvertToRNDF(osmDataHolder, Ways, RNDFoutFILE);
	
	/**************************************************************************/

	// Prvni pokusna funkce na ziskani nodu podle wayId
	NODES_t nodes = getNodesByWayId(osmDataHolder, 7726746);

	// Vypsani vysledku
	cout << "Ziskani nodu z way ID 7726746: " << endl;
	for(NODES_t::iterator it = nodes.begin(); it != nodes.end(); it++) {
		cout << "Node: " << it->nodeId << " "
			<< "Lat: " << it->lat << " Lon: " << it->lon << endl;
	}

	cout << endl;

	/**************************************************************************/

	// Druha pokusna funkce na ziskani souradnice bodu podle ID nodu
	double lat, lon;
	getNodePosition(osmDataHolder, 54747976, lat, lon);

	// Vypsani vysledku
	cout << "Ziskani pozice nodu 54747976: " << endl;
	cout << "Lat: " << lat << endl;
	cout << "Lon: " << lon << endl;

	cout <<  endl;

	/**************************************************************************/

	// Treti pokusna funkce na ziskani way ID podle nodu, vraci vice way ID a jejich pozici
	DataPos_t ways_pos;
	vector<int> positions;
	getWayIdByNodeId(osmDataHolder, 617014629, ways_pos);

	// Vypsani vysledku
	int pos = 0;
	cout << "Ziskani vsech way ID podle nodu 617014629: " << endl;
	for(DataPos_t::iterator it = ways_pos.begin(); it != ways_pos.end(); it++) {
		cout << "Node ID: 617014629 nalezeno ve way ID: "
			<< it->first << " na pozici: " << it->second << endl;
	}
	cout << endl;

	/**************************************************************************/

	//Ziskani pozice way
	cout<<"Way cislo: 31783865"<<endl;
	cout<<"Pozice v souboru: "<<getPositionWay(Ways, 31783865)<<endl<<endl;

	/**************************************************************************/

	//Vzdalenost bodu
	cout<<"Bod A: 49.204717, 16.60914"<<endl;
	cout<<"Bod B: 49.206834, 16.612128"<<endl;
	cout<<"Vzdalenost Bodu A od Bodu B: "<<getDistance(49.204717, 49.206834, 16.60914, 16.612128)<<" metru"<<endl<<endl;

	/**************************************************************************/

	//Ziskani nodu	
	cout<<"Nalezeni nodu za pomoci Lat a Lon: "<<endl;
	cout<<"Lat: 49.205968"<<endl;
	cout<<"Lon: 16.612849"<<endl;
	cout<<"ID nodu: "<<getNodeByLL(osmDataHolder, 49.205968, 16.612849, ways_pos)<<endl;
	for (DataPos_t::iterator it = ways_pos.begin(); it != ways_pos.end(); it++) {
		cout<<"Obsazen ve Ways: "<<it->first<<endl;
		cout<<"Na pozici: "<<it->second<<endl<<endl;
	}

	//Ziskani nejblizsiho bodu z "aktualni" pozice
	double nearLat, nearLon;
	long nearNode;

	double dst = getNearestPoint(osmDataHolder, 49.205380, 16.611240, nearLat, nearLon, nearNode, ways_pos);
	cout<<"Nalezeni nejblizsiho bodu od souradnic: 49.205380, 16.611240"<<endl;
	cout<<"Lat: "<<nearLat<<endl;
	cout<<"Lon: "<<nearLon<<endl;
	cout<<"Vzdalenost od techto bodu: "<<dst<<" metru"<<endl;
	cout<<"ID nalezeneho nodu: "<<nearNode<<endl;
	for (DataPos_t::iterator it = ways_pos.begin(); it != ways_pos.end(); it++) {
		cout<<"Obsazen ve Ways: "<<it->first<<endl;
		cout<<"Na pozici: "<<it->second<<endl;
	} 



	cout<<endl;
	return EXIT_SUCCESS;
} 