/* 
 * File:   HighwaysParser.cpp
 * Author: jtorres
 * 
 * Created on October 28, 2017, 7:23 PM
 */

#include "HighwaysParser.h"
     /* 43                   // "number of lines"
     * 32 0 13              // "line id" "count num" "number of stations"
     * 1210831 1 5 S 0.9 8  // "station id" "drop num" "route num"...
     *                      //      ..."direction" "postmile" "number of loops"
     * 1210832  0.0 0       // "loop id" "occ" "vol"
     * 1210833  0.0 0       // ..
     * 1210834  0.0 0       // ..
     * 1210835  0.0 0       // ..
     * 1210836  0.0 0       // ..
     * 1210837  0.0 0       // ..
     * 1210838  0.0 0       // ..
     * 1210839  0.0 0       // ..
      * */
HighwaysParser::HighwaysParser(char * hwyData) {
    parseLines(hwyData);
}

HighwaysParser::~HighwaysParser() {
}

void HighwaysParser::parseLines(char * hwyData)
{
    string highwaysData = hwyData;
    istringstream highwaysStream(highwaysData);
    string currLine;
    getline(highwaysStream, currLine);
    int numLines;
    sscanf(currLine.c_str(), "%d", &numLines);
    int lineNum = 0;
    long lds = 0;
    long ldsIndex = 0;
    short count = 0;
    int schedule = 0;
    int lineInfo = 0;
    long systemKey = 0;
    long globalSeq = 0;
    long schedleSeq = 0;
    short dropNum = 0;
    int routeNum = 0;
    float postmile = 0;
    long loopID = 0;
    float occ = 0;
    int vol = 0;

    for(int lineIndex = 0; lineIndex < numLines; lineIndex++)
    {
        FEP_LINE * newLine = new FEP_LINE;
        
        getline(highwaysStream, currLine);
        int numStations = 0;
        sscanf(currLine.c_str(), "%d %hd %d", &lineNum, &count, &numStations);
        newLine->lineNum = lineNum;
        newLine->count = count;
        newLine->schedule = schedule;
        newLine->lineInfo = lineInfo;
        newLine->systemKey = systemKey;
        newLine->globalSeq = globalSeq;
        newLine->schedleSeq = schedleSeq;
        
        for(int stationIndex = 0; stationIndex < numStations; stationIndex++)
        {
            STATION * newStation = new STATION;
    
            int numLoops = 0;
            getline(highwaysStream, currLine);
            char direction;
            sscanf(currLine.c_str(), "%ld %hd %d %c %f %d", &lds, &dropNum, &routeNum, &direction,
                    &postmile, &numLoops);
            newLine->lds.push_back(lds);
            newLine->ldsIndex.push_back(ldsIndex++);
            newStation->lds = lds;
            newStation->line_num = lineNum;
            newStation->drop = dropNum;
            newStation->pos = 0;
            newStation->MlTotVol = 0;
            newStation->OppTotVol = 0;
            
            for(int loopIndex = 0; loopIndex < numLoops; loopIndex++)
            {
                LOOP * newLoop = new LOOP;
                
                getline(highwaysStream, currLine);
                newLoop->loop_loc = (char *) malloc(25 * sizeof(char));
                sscanf(currLine.c_str(), "%ld %f %d %s", &loopID, &occ, &vol, newLoop->loop_loc);
                newLoop->loopID = loopID;
                newLoop->occ = occ;
                newLoop->vol = vol;
                newLoop->spd = 0;
                newStation->loops.push_back(newLoop);
            }
            newStation->length = newStation->loops.size() * 2 + CONTROL_DATA_LEN;
            newStation->dataPack = DataPacker::packData(newStation);
            
            this->stations.push_back(newStation);
        }
        this->lines.push_back(newLine);
    }
}