package be.iminds.ilabt.jfed.experimenter_gui.tutorials;

import be.iminds.ilabt.jfed.ui.javafx.util.StageUtils;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * User: twalcari
 * Date: 3/16/2016
 * Time: 14:54
 */
public class TutorialsView extends ScrollPane implements DialogActionsProvider {

    private final VBox container;

    private final Tutorials tutorials;
    private final TutorialViewsFactory tutorialViewsFactory;

    //dialog fields
    private final Scene scene = new Scene(this, 700, 600);
    private final Stage stage;

    public TutorialsView(Tutorials tutorials, TutorialViewsFactory tutorialViewsFactory) {
        this.tutorials = tutorials;
        this.tutorialViewsFactory = tutorialViewsFactory;

        this.container = new VBox(5);
        setContent(container);
        container.prefWidthProperty().bind(widthProperty().subtract(4));


        for (Tutorial tutorial : tutorials.getTutorials()) {
            final TutorialDescriptionView tutorialDescriptionView =
                    tutorialViewsFactory.createTutorialDescriptionView(tutorial, this);
            container.getChildren().add(tutorialDescriptionView);

            tutorialDescriptionView.prefWidthProperty().bind(container.widthProperty());
        }

        this.stage = new Stage();
        stage.setMinWidth(405);
        stage.setMinHeight(635);
        stage.setScene(scene);
        StageUtils.setJFedStageIcons(stage);
        stage.setTitle("Tutorials");
    }

    public void showDialog() {
        if (tutorials.getTutorials().size() > 1)
            stage.show();
        else if (tutorials.getTutorials().size() == 1)
            tutorialViewsFactory.createTutorialDetailView(tutorials.getTutorials().get(0)).showDialog(null);
        else
            new Alert(Alert.AlertType.WARNING, "The tutorials file has no entries.").show();
    }

    public void hideDialog() {
        stage.hide();
    }

}
