/**
 * 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 java.net.MalformedURLException;
import java.net.URL;
import java.util.prefs.Preferences;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.geotoolkit.gui.javafx.util.TaskManager;

/**
 * Panel for the initial dialog when opening the application.
 *
 * @author Cédric Briançon (Geomatys)
 */
public class FXInitialDialogPane extends BorderPane {

    public static final String DISPLAY_RULE_KEY = "display_disclaimer_on_startup";

    @FXML
    private Label uiVersion;

    @FXML
    private ImageView uiWarning;

    @FXML
    private CheckBox displayOnStartup;

    @FXML
    private Hyperlink uiEMail;

    @FXML
    private Hyperlink uiHref;

    @FXML
    private Button uiClose;

    private final Preferences displayPref;

    public FXInitialDialogPane() {
        Rhomeo.loadFXML(this, FXInitialDialogPane.class);
        displayPref = Preferences.userNodeForPackage(FXInitialDialogPane.class);
        displayOnStartup.setSelected(!displayPref.getBoolean(DISPLAY_RULE_KEY, true));
        displayOnStartup.selectedProperty().addListener((obs, oldValue, newValue) -> {
            displayPref.putBoolean(DISPLAY_RULE_KEY, !newValue);
        });

        uiWarning.setImage(Rhomeo.ICON_WARNING_ORANGE);

        uiEMail.setOnAction(evt -> Rhomeo.mail(uiEMail.getText()));
        uiHref.setOnAction(evt -> {
            try {
                Rhomeo.browse(new URL(uiHref.getText()));
            } catch (final MalformedURLException e) {
                // mimic task failure
                TaskManager.INSTANCE.submit("Ouverture d'un lien", () -> {
                    if (e != null)
                        throw e;
                    else
                        return false;
                });
            }
        });

        final String version = Rhomeo.getVersion();
        if (version == null || version.isEmpty())
            uiVersion.setText("Version de développement");
        else
            uiVersion.setText("Version ".concat(version));

        Platform.runLater(() -> uiClose.requestFocus());
    }

    /**
     * Close the dialog box when clicking on the accept button.
     */
    @FXML
    private void accept() {
        final Stage stage = (Stage) this.getScene().getWindow();
        stage.close();
    }
}
