/**
 * 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.api.InternationalResource;
import fr.cenra.rhomeo.api.preferences.PreferenceGroup;
import fr.cenra.rhomeo.api.ui.PreferencePane;
import fr.cenra.rhomeo.core.Session;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Preference editor. This panel aggregate editors for all available
 * {@link PreferenceGroup}. A discovery system allows to find the most
 * appropriate editor for a given group. If we cannot find any, we use a default
 * editor.
 *
 * @author Samuel Andrés (Geomatys)
 */
@Component("fr.cenra.rhomeo.fx.FXRhomeoPreferencePane")
@Scope("prototype")
public class FXRhomeoPreferencePane extends BorderPane implements InternationalResource {

    @Autowired
    private Map<String, PreferenceGroup> preferenceGroups;

    @Autowired
    private List<PreferencePane> availableEditors;

    @Autowired
    private Session session;

    private final String css;

    private final List<PreferencePane> preferencePanes = new ArrayList<>();

    public FXRhomeoPreferencePane() {
        css = FXDefaultPreferenceGroupPane.class.getResource(FXRhomeoPreferencePane.class.getSimpleName() + ".css").toExternalForm();
        getStylesheets().add(css);
    }

    @PostConstruct
    private void postConstruct() {

        // Creating one tab for each preference group.
        final TabPane tabPane = new TabPane();
        final List<PreferenceGroup> prefGroups = new ArrayList<>(preferenceGroups.values());
        Collections.sort(prefGroups);

        PreferencePane prefEditor;
        for (final PreferenceGroup preferenceGroup : prefGroups) {
            prefEditor = null;
            for (final PreferencePane editor : availableEditors) {
                if (editor.getPreferenceGroup().equals(preferenceGroup)) {
                    prefEditor = editor;
                    break;
                }
            }

            // If no specific editor has been found, a default one is used.
            if (prefEditor == null) {
                if (preferenceGroup.getKeys().isEmpty()) {
                    // A group without keys does not need to be displayed
                    continue;
                }

                final FXDefaultPreferenceGroupPane preferencePane = new FXDefaultPreferenceGroupPane();
                preferencePane.setPreferenceGroup(preferenceGroup);
                prefEditor = preferencePane;
            }

            preferencePanes.add(prefEditor);
            final Tab tab = new Tab(preferenceGroup.getLabel(), prefEditor.getEditor());
            tab.setTooltip(new Tooltip(preferenceGroup.getDescription()));
            tabPane.getTabs().add(tab);
        }
        setCenter(tabPane);

        // Creating preference controls
        final Button ok = new Button(getResourceString("validate"));
        ok.setOnAction(e -> {
            for (final PreferencePane preferencePane : preferencePanes) {
                preferencePane.save();
            }
            getScene().getWindow().hide();
        });

        final Button cancel = new Button(getResourceString("cancel"));
        cancel.setOnAction(e -> getScene().getWindow().hide());

        final HBox hBox = new HBox(cancel, ok);
        hBox.getStylesheets().add(css);
        hBox.getStyleClass().add("hBox");
        setBottom(hBox);
    }
}
