package be.iminds.ilabt.jfed.experimenter_gui.dialogs;

import be.iminds.ilabt.jfed.fedmon.webapi.service.json.Server;
import be.iminds.ilabt.jfed.ui.javafx.GlyphUtils;
import be.iminds.ilabt.jfed.ui.javafx.GuiceFXMLUtil;
import javafx.scene.Node;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.paint.Color;
import org.controlsfx.glyphfont.FontAwesome;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.Set;

/**
 * User: twalcari
 * Date: 12/5/13
 * Time: 9:21 PM
 */
public class SelectAuthoritiesDialog extends Dialog<Set<Server>> {
    private static final Logger LOG = LoggerFactory.getLogger(SelectAuthoritiesDialog.class);

    private final GuiceFXMLUtil guiceFXMLUtil;
    private final SelectAuthoritiesController controller;


    @Inject
    SelectAuthoritiesDialog(GuiceFXMLUtil guiceFXMLUtil) {
        this.guiceFXMLUtil = guiceFXMLUtil;

        GuiceFXMLUtil.Result<?, SelectAuthoritiesController> result =
                guiceFXMLUtil.createFromFXML(SelectAuthoritiesController.class);
        this.controller = result.getController();

        DialogPane dialogPane = new DialogPane();
        dialogPane.setContent(result.getRoot());
        dialogPane.setHeaderText("Select the involved authorities");
        dialogPane.setGraphic(GlyphUtils.createDialogGlyph(FontAwesome.Glyph.AMBULANCE, Color.GREEN));
        dialogPane.getButtonTypes().setAll(ButtonType.OK, ButtonType.CANCEL);

        Node okButton = dialogPane.lookupButton(ButtonType.OK);

        okButton.disableProperty().bind(controller.selectedAuthoritiesCountProperty().isEqualTo(0));


        setDialogPane(dialogPane);
        setResizable(false);

        setResultConverter(buttonType -> {
            if (buttonType == ButtonType.CANCEL)
                return null;
            else {
                return controller.getSelectedServers();
            }
        });
    }
}
