package ch.zhaw.gartenverwaltung;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainFXMLController implements Initializable {
    /**
     * Caching the panes
     */
    private final Map<String, AnchorPane> panes = new HashMap<>();
    private static final Logger LOG = Logger.getLogger(MainFXMLController.class.getName());

    @FXML
    private Button home_button;

    @FXML
    private AnchorPane mainPane;

    @FXML
    private Button myPlants_button;

    @FXML
    private Button mySchedule_button;

    @FXML
    private Button plants_button;

    @FXML
    void goToHome(ActionEvent event) throws IOException {
        loadPane("Home.fxml");
        styleChangeButton(home_button);
    }

    @FXML
    void goToMyPlants(ActionEvent event) throws IOException {
        loadPane("MyPlants.fxml");
        styleChangeButton(myPlants_button);
    }

    @FXML
    void goToMySchedule(ActionEvent event) throws IOException {
        loadPane("MySchedule.fxml");
        styleChangeButton(mySchedule_button);
    }

    @FXML
    void goToPlants(ActionEvent event) throws IOException {
        loadPane("Plants.fxml");
        styleChangeButton(plants_button);
    }

    /**
     * Updates the mainPane with the selected fxml file.
     * set HGrow and VGrow to parent AnchorPane.
     * Sends MainController to other Controllers.
     * @param fxmlFile string of fxml file
     * @throws IOException exception when file does not exist
     */
    public void loadPane(String fxmlFile) throws IOException {

        AnchorPane anchorPane = panes.get(fxmlFile);
        if (anchorPane == null) {
            FXMLLoader loader = new FXMLLoader(Objects.requireNonNull(HelloApplication.class.getResource(fxmlFile)));
            anchorPane = loader.load();
            panes.put(fxmlFile, anchorPane);

            if(fxmlFile.equals("MyPlants.fxml")) {
                MyPlantsController myPlantsController = loader.getController();
                myPlantsController.getMainController(this);
            }
        }
        mainPane.getChildren().setAll(anchorPane);
        anchorPane.prefWidthProperty().bind(mainPane.widthProperty());
        anchorPane.prefHeightProperty().bind(mainPane.heightProperty());

    }

    private void styleChangeButton(Button button) {
        //ToDo changeStyle of the menu buttons
    }

    /**
     * loads the default FXML File
     * {@inheritDoc}
     */
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        try {
            loadPane("Home.fxml");
            styleChangeButton(home_button);
        } catch (IOException e) {
            LOG.log(Level.SEVERE, "Failed to load FXML-Pane!", e);
        }
    }
}

