package controllers;

import controllers.BaseController;
import controllers.Controller;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;

import javafx.stage.Stage;
import model.*;
import utils.DataManagerSerialiser;

import java.io.IOException;
import java.io.InputStream;


/**
 * Created by samschofield on 7/03/17.
 */
public class Main extends Application {

    private Stage primaryStage;
    private Parent base;
    private BaseController baseController;
    private Session session;
    private Scene scene = null;

    public void start(Stage stage) throws Exception {
        this.primaryStage = stage;
        replaceSceneContent(SceneCode.MAIN);

        //load up base pane
        FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("scenes/base.fxml"));
        Parent root = loader.load();
        base = root;
        baseController = (BaseController) loader.getController();
        baseController.setParent(this);
        this.session = Session.session;
        try{
            DataManagerSerialiser dataManagerSerialiser = new DataManagerSerialiser();
            DataManager dataManager = dataManagerSerialiser.load();
            this.session.setDataManager(dataManager);
        } catch (Exception e){
            //this exception is for when the files is formatted wrong.
            this.session.setDataManager(new DataManager());
            Ride ride = new Ride("Nissan March", "Baby Blue", "EPU001", 2004, 5);

            Stop stop1 = new Stop("1 Avonhead Road");
            Stop stop2 = new Stop("100 Yaldhurst Road");
            Stop stop3 = new Stop("120 Maidstone Road");
            Stop stop4 = new Stop("1 University Drive");
            Stop stop5 = new Stop("1 Homestead Lane");

            ObservableList<Stop> routeList1 = FXCollections.observableArrayList();
            routeList1.add(stop1);
            routeList1.add(stop4);
            Route route1 = new Route("Home to University", routeList1);

            ObservableList<Stop> routeList2 = FXCollections.observableArrayList();
            routeList2.addAll(stop2, stop3, stop5);
            Route route2 = new Route("Friends Route", routeList2);

            boolean[] days = {false, true, true, false, true, false , false};

            TripStop tripStop1 = new TripStop("1 Avonhead Road", "9:00");
            TripStop tripStop4 = new TripStop("1 University Drive", "9:15");
            ObservableList<TripStop> tripStops = FXCollections.observableArrayList();
            tripStops.addAll(tripStop1, tripStop4);

            Trip trip = new Trip("Home to Uni", tripStops, "University", ride, days, true, "30-12-2017");

            this.session.getDataManager().addRides(ride);

            this.session.getDataManager().addStop(stop1);
            this.session.getDataManager().addStop(stop2);
            this.session.getDataManager().addStop(stop3);
            this.session.getDataManager().addStop(stop4);
            this.session.getDataManager().addStop(stop5);

            this.session.getDataManager().addRoute(route1);
            this.session.getDataManager().addRoute(route2);

            this.session.getDataManager().addTrip(trip);
        }

        //set up stage
        stage.setTitle("UC RSS");
        stage.setMinHeight(400);
        stage.setMinWidth(600);
        stage.show();

        stage.setOnCloseRequest(e -> {
            DataManagerSerialiser dataManagerSerialiser = new DataManagerSerialiser();
            dataManagerSerialiser.serialise(session.getDataManager());
            Platform.exit();
        });

    }

    /**
     * Replace Scene Content with fxml file code from oracle.
     * @param fxml
     * @return
     * @throws Exception
     */
    public Controller replaceSceneContent(SceneCode fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = getClass().getClassLoader().getResourceAsStream(fxml.getPath());
        Parent page;
        try {
            page = (Parent) loader.load(in);
        } finally {
            in.close();
        }
        if (fxml.isLoadMenu()){
            if (scene == null){
                scene = new Scene(base, SceneCode.prefWidth, SceneCode.prefHeight);
            }else{
                scene.setRoot(base);
            }
            baseController.setContent(page);
        }else {
            if (scene == null) {
                scene = new Scene(page, SceneCode.prefWidth, SceneCode.prefHeight);
            }else{
                scene.setRoot(page);
            }
        }
        primaryStage.setScene(scene);
        Controller controller = loader.getController();
        if (controller != null) {
            controller.setParent(this);
            controller.runLater();
        }
        return controller;
    }

    public Session getSession() {
        return session;
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
