package ch.zhaw.gartenverwaltung;

import ch.zhaw.gartenverwaltung.bootstrap.AppLoader;
import ch.zhaw.gartenverwaltung.backgroundtasks.BackgroundTasks;
import ch.zhaw.gartenverwaltung.io.CropList;
import ch.zhaw.gartenverwaltung.io.PlantList;
import ch.zhaw.gartenverwaltung.io.TaskList;
import ch.zhaw.gartenverwaltung.models.Garden;
import ch.zhaw.gartenverwaltung.types.Crop;
import javafx.application.Application;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.Timer;

/**
 * Main class of the Application
 */
public class Main extends Application {
    Timer backGroundTaskTimer = new Timer();
    BackgroundTasks backgroundTasks;

    /**
     * Method which is automatically called if Application is starting. It loads the scenes to stage and shows the stage.
     * It creates a new Instance of BackgroundTasks and schedules them with a Timer instance to execute them every minute.
     * @param stage Stage to show
     * @throws IOException If loading Scenes can not access the fxml Resource File
     */
    @Override
    public void start(Stage stage) throws IOException {
        AppLoader appLoader = new AppLoader();

        appLoader.loadSceneToStage("MainFXML.fxml", stage);

        stage.setTitle("Gartenverwaltung");
        stage.show();

        backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(CropList) appLoader.getAppDependency(CropList.class), (PlantList) appLoader.getAppDependency(PlantList.class));
        backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 60000);

    }

    /**
     * Method which is automatically called when application is stopped.
     * It cancels the timer to not execute the background tasks anymore.
     */
    @Override
    public void stop(){
        backGroundTaskTimer.cancel();
    }

    /**
     * The Main method launches the application
     * @param args There are no arguments needed.
     */
    public static void main(String[] args) {
        launch();
    }
}