package ch.zhaw.gartenverwaltung;


import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.File;

/**
 * Controller class for the Tutorial.fxml file
 */
public class TutorialController {

    @FXML
    public Button previousPageButton;
    @FXML
    public Button nextPageButton;
    @FXML
    public StackPane tourPages;
    public ImageView imgAddNewPlant;
    public ImageView imgTaskList;
    public ImageView imgSelectDate;
    public ImageView imgAddTaskButton;
    public ImageView imgDetailDeleteButtons;

    private int page = 0;

    @FXML
    public void initialize() {
        switchViews();
        setButtonAbilities();

        setImageView(imgAddNewPlant, "add-new-plant.png");
        setImageView(imgSelectDate, "select-sow-harvest.png");
        setImageView(imgDetailDeleteButtons, "details-delete.png");
        setImageView(imgTaskList, "schedule.png");
        setImageView(imgAddTaskButton, "add-task.png");
    }

    /**
     * update the given image view with screenshot or placeholder image.
     * @param imageView the image view to update
     * @param fileName the file name of the source
     */
    private void setImageView(ImageView imageView, String fileName) {
        File file = new File(String.valueOf(PlantsController.class.getResource(fileName)));
        Image image;
        if (file.exists()) {
            image = new Image(String.valueOf(PlantsController.class.getResource(fileName)));
        } else {
            image = new Image(String.valueOf(PlantsController.class.getResource("placeholder.png")));
        }
        imageView.setImage(image);
    }

    public void viewNextPage() {
        page++;
        switchViews();
        setButtonAbilities();
    }
    public void viewPreviousPage() {
        page--;
        switchViews();
        setButtonAbilities();
    }

    /**
     * disable next or close button according to the location of button
     */
    private void setButtonAbilities() {
        previousPageButton.setDisable(page <= 0);
        nextPageButton.setDisable(page >= tourPages.getChildren().size() - 1);
    }

    /**
     * switch to next view
     */
    private void switchViews() {
        tourPages.getChildren().forEach(node -> node.setOpacity(0));
        tourPages.getChildren().get(page).setOpacity(1);
    }

    /**
     * close Tutorial
     */
    public void closeTutorial() {
        Stage root = (Stage) tourPages.getScene().getWindow();
        root.close();
    }
}
