package be.iminds.ilabt.jfed.experimenter_gui.editor.properties;

import be.iminds.ilabt.jfed.fedmon.webapi.service.json.Server;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecNode;
import be.iminds.ilabt.jfed.ui.javafx.FXMLUtil;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

/**
 * User: twalcari
 * Date: 9/10/2014
 * Time: 09:44
 */
public class ExoGeniConfigTab extends AdaptivePropertiesTab<FXRspecNode> {
    private static final Logger LOG = LoggerFactory.getLogger(ExoGeniConfigTab.class);

    private final AdaptivePropertiesDialog adaptivePropertiesDialog;

    @FXML
    private ComboBox<String> exogeniResourceTypeComboBox;

    private static final String[] CORRECT_TYPES =
            new String[]{"default-vm", "xo.small", "xo.medium", "xo.large", "xo.xlarge", "exogeni-m4"};

    public ExoGeniConfigTab(AdaptivePropertiesDialog adaptivePropertiesDialog) {
        super("ExoGeni");
        this.adaptivePropertiesDialog = adaptivePropertiesDialog;

        setContent(FXMLUtil.createFromFXML(this));

        exogeniResourceTypeComboBox.getItems().addAll(CORRECT_TYPES);
    }

    @Override
    public void load(FXRspecNode rspecNode) {
        //initialize tables with information from node
        String sliverTypeName = rspecNode.getSliverTypeName();
        if (sliverTypeName == null || (!Arrays.asList(CORRECT_TYPES).contains(sliverTypeName) &&
                !(sliverTypeName.startsWith("xo.") || sliverTypeName.startsWith("exogeni"))))  //also allow things like xo.tiny
            sliverTypeName = "default-vm";
        exogeniResourceTypeComboBox.getSelectionModel().select(sliverTypeName);
    }

    @Override
    public void save(FXRspecNode rspecNode) {
        String selectedTypeName = exogeniResourceTypeComboBox.getSelectionModel().getSelectedItem();
        if (selectedTypeName != null)
            rspecNode.setSliverTypeName(selectedTypeName);
        LOG.debug("After setting sliver type name, slivertype it is: " + rspecNode.getSliverType());
    }

    @Override
    public void onTabShown() {

    }

    @Override
    public boolean shouldBeVisible(FXRspecNode rspecNode) {
        if (adaptivePropertiesDialog.getSelectedResource() == null) return false;
        Server server = adaptivePropertiesDialog.getSelectedResource().getServer();
        if (server == null) return false;
        return server.isServerType("exogeni") || server.isServerType("orca");
    }

    @Override
    public String validateConfiguration() {
        return null;
    }


    public void onExoResourceHelpLinkClicked() {
        if (java.awt.Desktop.isDesktopSupported()) {
            try {
                java.awt.Desktop.getDesktop().browse(new URI("https://wiki.exogeni.net/doku.php?id=public:experimenters:resource_types:start"));
            } catch (IOException | URISyntaxException e) {
                LOG.warn("Exception starting browser", e);
            }
        }
    }
}
