/**
 * Copyright © CENRA (2016)
 *
 * remi.clement@espaces-naturels.fr
 * 
 * This software is a multi-platform and portable application based on 
 * the scientifical toolbox to monitor wetlands. With this toolbox it’s 
 * possible to calculate indicators «RhoMéO» to monitor quality, 
 * fonctionalities and urban and agricultural pressures of wetlands. 
 * Indicators are calculated using naturalist data entered and/or imported,
 * and geographic data downloaded from a geographic server.
 * 
 * This software is governed by the CeCILL license under French law and
 * abiding by the rules of distribution of free software.  You can  use, 
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info". 
 * 
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability. 
 * 
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or 
 * data to be ensured and,  more generally, to use and operate it in the 
 * same conditions as regards security. 
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
*/
package fr.cenra.rhomeo.fx;

import fr.cenra.rhomeo.Rhomeo;
import fr.cenra.rhomeo.api.InternationalResource;
import fr.cenra.rhomeo.core.Session;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.geotoolkit.gui.javafx.util.ProgressMonitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import org.geotoolkit.gui.javafx.util.TaskManager;

/**
 * Main application panel.
 *
 * @author Cédric Briançon (Geomatys)
 */
@Component
public class FXMainPane extends BorderPane implements InternationalResource {

    @FXML
    private MenuButton uiMenuButton;

    @Autowired
    private FXRhomeoWizard wizard;

    @Autowired
    private Session session;

    public FXMainPane() {
        Rhomeo.loadFXML(this, FXMainPane.class);
        initProgressMonitor();

        uiMenuButton.setGraphic(new ImageView(Rhomeo.ICON_BARS_WHITE));
        uiMenuButton.setFocusTraversable(false);
    }

    /**
     * Initializes JavaFX components after spring components are done loading.
     */
    @PostConstruct
    private void postConstruct() {
        setCenter(wizard);
    }

    /**
     * Inserts the {@link ProgressMonitor} before the main menu button.
     * HACK : Add an item in progress monitor list, allowing to clear list of
     * tasks in error.
     */
    private void initProgressMonitor() {
        final ProgressMonitor progressMonitor = new ProgressMonitor(TaskManager.INSTANCE);
        progressMonitor.getStylesheets().add(FXDashboardPane.class.getResource("/fr/cenra/rhomeo/ProgressMonitor.css").toString());

        final HBox parent = ((HBox) uiMenuButton.getParent());
        parent.getChildren().add(parent.getChildren().size()-1, progressMonitor);
    }

    /**
     * Open preferences window.
     */
    @FXML
    private void showPreferences() {
        final Stage prefStage = Rhomeo.newStage();
        prefStage.setTitle(getResourceString("preferences"));
        prefStage.initModality(Modality.APPLICATION_MODAL);
        final Scene scene = new Scene(session.createPrototype(FXRhomeoPreferencePane.class));
        prefStage.setScene(scene);
        prefStage.setMinWidth(530);
        prefStage.setMinHeight(230);
        prefStage.show();
        prefStage.requestFocus();
    }

    /**
     * Open references documents page.
     */
    @FXML
    private void showRefDocuments() {
        final Stage prefStage = Rhomeo.newStage();
        prefStage.setTitle(getResourceString("ref-docs"));
        prefStage.initModality(Modality.APPLICATION_MODAL);
        final Scene scene = new Scene(session.createPrototype(FXRhomeoReferencePane.class));
        scene.getStylesheets().add(Rhomeo.CSS_THEME_PATH);
        prefStage.setScene(scene);
        prefStage.setMinWidth(800);
        prefStage.setMinHeight(600);
        prefStage.show();
        prefStage.requestFocus();
    }

    /**
     * Open the about dialog.
     */
    @FXML
    private void showAboutDialog() {
        final Stage popup = Rhomeo.createWelcomePopup();
        popup.initOwner(this.getScene().getWindow());

        popup.show();
        popup.requestFocus();
    }

    /**
     * Quit the application.
     */
    @FXML
    private void quit() {
        session.close();
        System.exit(0);
    }
}
