/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlpackage;


import applications.Trip;
import java.io.FileWriter;
import java.util.Enumeration;
import java.util.Vector;
import mdot.Main;
import org.jdom.*;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author student
 */
public class CarbonEmissionsXMLWriter {

 public static void writeToFile(Vector<Trip> trips)
    {

        Element tripsElement = new Element("trips");
        Document myDocument = new Document(tripsElement);

        //Vector<sign> vsign = signVector.getSignVect();
        //Enumeration<sign> en = vsign.elements();

        Enumeration en = trips.elements();

        while(en.hasMoreElements())
        {

            Element trip = new Element("trip");

            Trip tripData = (Trip)en.nextElement();

            Element date = new Element("date");
                date.addContent(tripData.getDate());

            Element fuel = new Element("fuel");
                
                if(tripData.getGallonsused() == Double.NaN || tripData.getGallonsused() == Double.NEGATIVE_INFINITY || tripData.getGallonsused() == Double.POSITIVE_INFINITY)
                    fuel.addContent("N/A");
                else
                    fuel.addContent(String.valueOf(tripData.getGallonsused()));

            Element miles = new Element("miles");
                miles.addContent(String.valueOf(tripData.getMilesTraveled()));


            Element carbon = new Element("carbon");

              if(tripData.getCarbonemission() == Double.NaN || tripData.getCarbonemission() == Double.NEGATIVE_INFINITY || tripData.getCarbonemission() == Double.POSITIVE_INFINITY)
                carbon.addContent("N/A");
              else
                carbon.addContent(String.valueOf(tripData.getCarbonemission()));
            

            trip.addContent(date);
            trip.addContent(fuel);
            trip.addContent(miles);
            trip.addContent(carbon);
           
            tripsElement.addContent(trip);
        }

       try {

            FileWriter writer = new FileWriter(Main.carbon);

            org.jdom.output.Format f = Format.getPrettyFormat();
            XMLOutputter outputter = new XMLOutputter(f);
            outputter.output(myDocument, writer);

            writer.close();
            } catch (java.io.IOException e) {
            e.printStackTrace();
        }

    }

}