package seng302.Mock;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import seng302.GPSCoordinate;
import seng302.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by jjg64 on 19/04/17.
 */
public class RegattaXMLReader extends XMLReader {
    private int regattaID;
    private String regattaName;
    private int raceID = 0;
    private String courseName;
    private double centralLatitude;
    private double centralLongitude;
    private double centralAltitude;
    private float utcOffset;
    private float magneticVariation;

    /**
     * Constructor for Regatta XML
     *
     * @param filePath path of the file
     * @throws IOException                  error
     * @throws SAXException                 error
     * @throws ParserConfigurationException error
     */
    public RegattaXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException {
        this(filePath, true);
    }

    /**
     * Constructor for Regatta XML
     *
     * @param filePath file path to read
     * @param read     whether or not to read and store the files straight away.
     * @throws IOException                  error
     * @throws SAXException                 error
     * @throws ParserConfigurationException error
     */
    private RegattaXMLReader(String filePath, boolean read) throws IOException, SAXException, ParserConfigurationException {
        super(filePath);
        if (read) {
            read();
        }
    }

    public RegattaXMLReader(InputStream xmlString) throws IOException, SAXException, ParserConfigurationException {
        super(xmlString);
        read();
    }

    /**
     * Read the XML
     */
    private void read() {
        NodeList attributeConfig = doc.getElementsByTagName("RegattaConfig");
        Element attributes = (Element) attributeConfig.item(0);
        makeRegatta(attributes);
    }

    private void makeRegatta(Element attributes) {
        this.regattaID = Integer.parseInt(getTextValueOfNode(attributes, "RegattaID"));
        this.regattaName = getTextValueOfNode(attributes, "RegattaName");
        this.courseName = getTextValueOfNode(attributes, "CourseName");
        this.centralLatitude = Double.parseDouble(getTextValueOfNode(attributes, "CentralLatitude"));
        this.centralLongitude = Double.parseDouble(getTextValueOfNode(attributes, "CentralLongitude"));
        this.centralAltitude = Double.parseDouble(getTextValueOfNode(attributes, "CentralAltitude"));
        this.utcOffset = Float.parseFloat(getTextValueOfNode(attributes, "UtcOffset"));
        this.magneticVariation = Float.parseFloat(getTextValueOfNode(attributes, "MagneticVariation"));
    }

    public int getRegattaID() {
        return regattaID;
    }

    public void setRegattaID(int ID) {
        this.regattaID = ID;
    }

    public String getRegattaName() {
        return regattaName;
    }

    public void setRegattaName(String regattaName) {
        this.regattaName = regattaName;
    }

    public int getRaceID() {
        return raceID;
    }

    public void setRaceID(int raceID) {
        this.raceID = raceID;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public double getCentralLatitude() {
        return centralLatitude;
    }

    public void setCentralLatitude(double centralLatitude) {
        this.centralLatitude = centralLatitude;
    }

    public double getCentralLongitude() {
        return centralLongitude;
    }

    public void setCentralLongitude(double centralLongitude) {
        this.centralLongitude = centralLongitude;
    }

    public double getCentralAltitude() {
        return centralAltitude;
    }

    public void setCentralAltitude(double centralAltitude) {
        this.centralAltitude = centralAltitude;
    }

    public float getUtcOffset() {
        return utcOffset;
    }

    public void setUtcOffset(float utcOffset) {
        this.utcOffset = utcOffset;
    }

    public float getMagneticVariation() {
        return magneticVariation;
    }

    public void setMagneticVariation(float magneticVariation) {
        this.magneticVariation = magneticVariation;
    }

    public GPSCoordinate getGPSCoordinate() {
        return new GPSCoordinate(centralLatitude, centralLongitude);
    }
}
