package seng302;


import javafx.application.Application;
import javafx.stage.Stage;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import seng302.DataInput.PolarParser;
import seng302.DataInput.XMLReader;
import seng302.Model.Event;
import seng302.Model.Polars;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class App extends Application {

    /**
     * Entry point for running the programme
     *
     * @param args for starting the programme
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        try {
            Polars boatPolars = PolarParser.parse("polars/acc_polars.csv");

            String regattaXML = readFile("mockXML/regattaTest.xml", StandardCharsets.UTF_8);
            String raceXML = readFile("mockXML/raceTest.xml", StandardCharsets.UTF_8);
            String boatXML = readFile("mockXML/boatTest.xml", StandardCharsets.UTF_8);

            Event raceEvent = new Event(raceXML, regattaXML, boatXML, boatPolars);
            raceEvent.start();

        } catch (Exception e) {
            //Catch all exceptions, print, and exit.
            e.printStackTrace();
            System.exit(1);
        }
    }

    private String readFile(String path, Charset encoding) throws IOException, ParserConfigurationException, SAXException, TransformerException {

        InputSource fXmlFile = new InputSource(getClass().getClassLoader().getResourceAsStream(path));

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        return XMLReader.getContents(doc);

    }

}
