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

package xmlpackage;


import CommonFunctions.debugPrinter;
import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import structures.Constants;
import applications.CarbonControl;
import database.Context;
import mdot.Main;
import structures.ChargesEntry;
/**
 *
 * @author student
 */
public class ChargesReader {

    public static void ExtractFromFile() {
        try {
            File xmlFile = new File(Main.wallet);
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(xmlFile);
            ExtractChargesData(doc);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

     private static void ExtractChargesData(Document doc) {
        /*
        <charges>
        <charge>
        <date>11/13/2009</date>
        <type>Bridge</type>
        <latitude>42.39713</latitude>
        <longitude>-85.29481</longitude>
        <price>3.50</price>
        </charge>
        </charges>*/

        try {

            //get the root element -- charges
            Element rootElement = doc.getDocumentElement();

            //get the list of all trip element
            NodeList chargesList = rootElement.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGE);

            if (chargesList != null && chargesList.getLength() > 0) {
                for (int i = 0; i < chargesList.getLength(); i++) {


                    ChargesEntry charges = new ChargesEntry();

                    //get the trip element
                    Node fstCharge = chargesList.item(i);

                    //if trip is an element
                    if (fstCharge.getNodeType() == Node.ELEMENT_NODE) {

                        Element chargeElmnt = (Element) fstCharge;

                        NodeList dateElementList = chargeElmnt.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_DATE);
                        Element dateElement = (Element) dateElementList.item(0);
                        NodeList dateChildren = dateElement.getChildNodes();
                        debugPrinter.printLine("Date: " + ((Node) dateChildren.item(0)).getNodeValue());
                        charges.setDate(((Node) dateChildren.item(0)).getNodeValue());
                        
                        NodeList typeElementList = chargeElmnt.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_TYPE);
                        Element typeElement = (Element) typeElementList.item(0);
                        NodeList typeChildren = typeElement.getChildNodes();
                        debugPrinter.printLine("Type: " + ((Node) typeChildren.item(0)).getNodeValue());
                        charges.setType(((Node) typeChildren.item(0)).getNodeValue());

                        NodeList latElementList = chargeElmnt.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_LAT);
                        Element latElement = (Element) latElementList.item(0);
                        NodeList latChildren = latElement.getChildNodes();
                        debugPrinter.printLine("Lat: " + ((Node) latChildren.item(0)).getNodeValue());
                        charges.setLat(Double.parseDouble(((Node)latChildren.item(0)).getNodeValue()));

                        NodeList lonElementList = chargeElmnt.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_LON);
                        Element lonElement = (Element) lonElementList.item(0);
                        NodeList lonChildren = lonElement.getChildNodes();
                        debugPrinter.printLine("Lon: " + ((Node) lonChildren.item(0)).getNodeValue());
                        charges.setLon(Double.parseDouble(((Node) lonChildren.item(0)).getNodeValue()));


                        NodeList priceElementList = chargeElmnt.getElementsByTagName(Constants.XML_CHARGEPOINTS_CHARGEPOINT_PRICE);
                        Element priceElement = (Element) priceElementList.item(0);
                        NodeList priceChildren = priceElement.getChildNodes();
                        debugPrinter.printLine("Price: " + ((Node) priceChildren.item(0)).getNodeValue());
                        charges.setPrice(((Node) priceChildren.item(0)).getNodeValue());


                    }

                    Context.chargedVector.add(charges);
                }
            }

            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
