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

using namespace std;

// Nacteni souboru a vytahnuti dat
void parseFile(string filename, DataFile_t &osmDataHolder, vector<int> &Ways_v, string OUTfile) {

    osmDataHolder.clear();

    string buf;
    string line;
    ifstream in(filename.c_str(), ifstream::in);
	ofstream out(OUTfile.c_str());

    // Nacteni celeho souboru
    while(getline(in, line)) {
        buf += (line + '\n');
    }
    in.close();
    
	size_t RctnglPos = 0;
	string minlat, minlon, maxlat, maxlon;
	//Ziskani oblasti mapy
        if((RctnglPos = buf.find("<bounds minlat=\"", RctnglPos)) != string::npos) {
			minlat = buf.substr(RctnglPos+16,buf.find("\"", RctnglPos+17) - RctnglPos-16).c_str();
		}
		if((RctnglPos = buf.find("minlon=\"", RctnglPos)) != string::npos) {
			minlon = buf.substr(RctnglPos+8,buf.find("\"", RctnglPos+9) - RctnglPos-8).c_str();
		}
		if((RctnglPos = buf.find("maxlat=\"", RctnglPos)) != string::npos) {
			maxlat = buf.substr(RctnglPos+8,buf.find("\"", RctnglPos+9) - RctnglPos-8).c_str();
		}
		if((RctnglPos = buf.find("maxlon=\"", RctnglPos)) != string::npos) {
			maxlon = buf.substr(RctnglPos+8,buf.find("\"", RctnglPos+9) - RctnglPos-8).c_str();
		}
			out<< "Ctverec v mape, ze ktereho byly vycteny data" <<endl;
			out<< "Bod A: " << minlat << ", " << minlon <<endl;
			out<< "Bod B: " << maxlat << ", " << maxlon <<endl;

// Nalezeni vsech dat
    size_t wayPos = 0;
    while(true) {
		
        // Ziskani way ID
        if((wayPos = buf.find("<way id=\"", wayPos)) != string::npos) {
            long wayId = atol(buf.substr(wayPos+9, buf.find("\"", wayPos+10) - wayPos-9).c_str());
			out << endl <<wayId <<endl;
            wayPos += 9;

            // Ziskani vsech nd ref pro way ID
            size_t ndRefPos = 0;
            string ndRefBuf = buf.substr(wayPos, buf.find("</way>", wayPos) - wayPos);

            // Data jedne way ID
            NODES_t wayData;

            while(true) {
                if((ndRefPos = ndRefBuf.find("<nd ref=\"", ndRefPos)) != string::npos) {
                    long ndRef = atol(ndRefBuf.substr(ndRefPos+9, ndRefBuf.find("\"", ndRefPos+10) - ndRefPos-9).c_str());
					out << "\t" << ndRef << ": ";
                    ndRefPos += 1;

                    // Ziskani souradnic pro nd ref
                    size_t nodePos = 0;
                    stringstream nodeData;
                    double lat = 0.0, lon = 0.0;
                    nodeData << "<node id=\"" << ndRef << "\"";
                    if((nodePos = buf.find(nodeData.str())) != string::npos) {
                        string nodeBuf = buf.substr(nodePos, buf.find("/>", nodePos) - nodePos);

                        // Lat, Lon pozice
                        size_t nodePos = 0;
                        if((nodePos = nodeBuf.find("lat=\"", nodePos)) != string::npos) {
                            lat = atof(nodeBuf.substr(nodePos+5, nodeBuf.find("\"", nodePos+6) - nodePos-5).c_str());
                        }
                        if((nodePos = nodeBuf.find("lon=\"", nodePos)) != string::npos) {
                            lon = atof(nodeBuf.substr(nodePos+5, nodeBuf.find("\"", nodePos+6) - nodePos-5).c_str());
                        }
                    }
					out.precision(6);
					out.setf(ios::fixed);
					out << "\t" << " [" << lat << ", " << lon << "]" << endl;

                    NODE_t node;
                    node.lat = lat;
                    node.lon = lon;
                    node.nodeId = ndRef;
                    wayData.push_back(node);
                }
                else {
                    break;
                }
            }
			Ways_v.push_back(wayId);
            osmDataHolder[wayId] = wayData;
			
        }

        else {
            break;
        }
    }

	cout << "\n=== Data nactena ===\n" << endl;
    return;
} 