package atmsdriver.model;

import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;



/** An FEPLine is a simulated line of communication from the FEP to 
 *  LoopDetectorStations in the traffic network.
 * 
 *  An FEPLine contains static meta data and a list of LoopDetectorStations.
 *  A single FEPLine contains multiple LoopDetectorStations.
 *
 * @author John A. Torres
 * @version 09/10/2017
 */ 
public class FEPLine {
    /* Static FEPLine meta data */
    final private int lineNum;
    final private List<Station> stations;
    final private int count;
    // NOT SURE IF NEXT IS FINAL
    final private int schedule;
    final private int lineInfo;
    final private long systemKey;
    private long globalSeq;
    private long scheduleSeq;
    
    public FEPLine(int lineNum, List<Station> stations, int count,
            int schedule, int lineInfo, long systemKey, long globalSeq,
            long scheduleSeq)
    {
        this.lineNum = lineNum;
        this.stations = stations;
        this.count = count;
        this.schedule = schedule;
        this.lineInfo = lineInfo;
        this.systemKey = systemKey;
        this.globalSeq = globalSeq;
        this.scheduleSeq = scheduleSeq;
    }
    
    public int getLineNumber()
    {
        return this.lineNum;
    }
    
    public List<Station> getStations()
    {
        return this.stations;
    }
    
    // NEED TO CHECK NUMBERS? DO WE EVEN NEED THIS?
    public void updateSequences()
    {        
        this.scheduleSeq += 1;
        this.globalSeq += 51;
    }
    
    public String getLineMeta()
    {
        StringBuilder build = new StringBuilder();
        build.append(Integer.toString(this.lineNum));
        build.append(" ");
        build.append(Integer.toString(this.count));
        build.append(" ");
        build.append(Integer.toString(this.stations.size()));
        build.append("\n");
        for(Station station : stations)
        {
            build.append(station.getStationMeta());
        }
        return build.toString();
    }
    
    public void toXML(Element currElem)
    {
        Document theDoc = currElem.getOwnerDocument();
        
        Element lineElement = theDoc.createElement(XML_TAGS.LINE.tag);
        currElem.appendChild(lineElement);
        
        Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag);
        lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum)));
        lineElement.appendChild(lineNumElement);
        
        Element countElement = theDoc.createElement(XML_TAGS.COUNT.tag);
        countElement.appendChild(theDoc.createTextNode(String.valueOf(this.count)));
        lineElement.appendChild(countElement);
        
        Element scheduleElement = theDoc.createElement(XML_TAGS.SCHEDULE.tag);
        scheduleElement.appendChild(theDoc.createTextNode(String.valueOf(this.schedule)));
        lineElement.appendChild(scheduleElement);
        
        Element lineInfoElement = theDoc.createElement(XML_TAGS.LINE_INFO.tag);
        lineInfoElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineInfo)));
        lineElement.appendChild(lineInfoElement);
        
        Element systemKeyElement = theDoc.createElement(XML_TAGS.SYSTEM_KEY.tag);
        systemKeyElement.appendChild(theDoc.createTextNode(String.valueOf(this.systemKey)));
        lineElement.appendChild(systemKeyElement);
        
        Element globalSeqElement = theDoc.createElement(XML_TAGS.GLOBAL_SEQ.tag);
        globalSeqElement.appendChild(theDoc.createTextNode(String.valueOf(this.globalSeq)));
        lineElement.appendChild(globalSeqElement);
        
        Element scheduleSeqElement = theDoc.createElement(XML_TAGS.SCHEDULE_SEQ.tag);
        scheduleSeqElement.appendChild(theDoc.createTextNode(String.valueOf(this.scheduleSeq)));
        lineElement.appendChild(scheduleSeqElement);
        
        Element stationsElement = theDoc.createElement(XML_TAGS.STATIONS.tag);
        lineElement.appendChild(stationsElement);
        for(Station station : stations)
        {
            station.toXML(stationsElement);
        }
    }
    
    private static enum XML_TAGS
    {
        LINE("Line"),
        LINE_NUM("Line_Num"),
        STATIONS("Stations"),
        COUNT("Count"),
        SCHEDULE("Schedule"),
        LINE_INFO("Line_Info"),
        SYSTEM_KEY("System_Key"),
        GLOBAL_SEQ("Global_Seq"),
        SCHEDULE_SEQ("Schedule_Seq");
        
        String tag;
        
        private XML_TAGS(String n)
        {
            tag = n;
        }
    }
}
