package atmsdriver.model;

import java.util.ArrayList;
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
 * Stations in the highways network.
 *
 * An FEPLine contains static line meta data and a list of Stations. A
 * single FEPLine contains multiple Stations.
 *
 * @author John A. Torres
 * @version 09/10/2017
 */
final public class FEPLine
{
    /* Static FEPLine meta data */
    final public int lineNum;
    final public List<Station> stations;
    final private int count;

    /**
     * Constructs an FEPLine from given line number, list of stations, and count.
     * 
     * @param lineNum
     * @param stations
     * @param count 
     */
    FEPLine(int lineNum, ArrayList<Station> stations, int count)
    {
        this.lineNum = lineNum;
        this.stations = stations;
        this.count = count;
    }
    
    /**
     * Returns the FEPLine meta data in string format
     * @return FEPLine metadata
     */
    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();
    }
    
    /**
     * Returns the FEPLine data in XMLFormat
     * 
     * @param currElem The current XML <Line> element
     */
    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 stationsElement = theDoc.createElement(XML_TAGS.STATIONS.tag);
        lineElement.appendChild(stationsElement);
        for (Station station : stations)
        {
            station.toXML(stationsElement);
        }
    }
    
    /**
     * XML Tags used in toXML()
     */
    private static enum XML_TAGS
    {

        LINE("Line"),
        LINE_NUM("Line_Num"),
        STATIONS("Stations"),
        COUNT("Count");

        String tag;

        private XML_TAGS(String n)
        {
            tag = n;
        }
    }
}
