package be.iminds.ilabt.jfed.experimenter_gui.slice.ribbon_tabs;

import be.iminds.ilabt.jfed.experiment.Experiment;
import be.iminds.ilabt.jfed.experimenter_gui.slice.ExperimentViewController;
import be.iminds.ilabt.jfed.experimenter_gui.slice.events.EditSshKeysEvent;
import be.iminds.ilabt.jfed.experimenter_gui.slice.events.RebootExperimentEvent;
import be.iminds.ilabt.jfed.experimenter_gui.slice.events.ShareExperimentEvent;
import be.iminds.ilabt.jfed.experimenter_gui.ui.ribbon.RibbonButton;
import be.iminds.ilabt.jfed.highlevel.model.Slice;
import be.iminds.ilabt.jfed.preferences.GuiPreferenceKey;
import be.iminds.ilabt.jfed.preferences.JFedGuiPreferences;
import com.google.common.eventbus.EventBus;
import javafx.fxml.FXML;
import org.reactfx.value.Val;

import java.util.Objects;

/**
 * User: twalcari
 * Date: 7/1/2014
 * Time: 17:20
 */
public class AdvancedSliceRibbonComponentGroupController {
    private final SliceRibbonTab sliceRibbonTab;
    private final EventBus eventBus;

    @FXML
    private RibbonButton restartExperimentButton;
    @FXML
    private RibbonButton editSshKeysExperimentButton;
    @FXML
    private RibbonButton shareSliceButton;

    private final boolean isDemo = new JFedGuiPreferences().getBoolean(GuiPreferenceKey.PREF_IS_DEMO, false);

    public AdvancedSliceRibbonComponentGroupController(SliceRibbonTab sliceRibbonTab, EventBus eventBus) {
        this.sliceRibbonTab = sliceRibbonTab;
        this.eventBus = eventBus;


    }

    @FXML
    protected void initialize() {
        if (!isDemo) {
            final Val<Boolean> manifestUnavailableVal =
                    Val.map(sliceRibbonTab.activeExperimentViewControllerProperty(), ExperimentViewController::getExperiment)
                            .flatMap(Experiment::sliceProperty)
                            .flatMap(Slice::manifestRspecProperty)
                            .map(Objects::isNull).orElseConst(true);

            restartExperimentButton.disableProperty().bind(manifestUnavailableVal);
            editSshKeysExperimentButton.disableProperty().bind(manifestUnavailableVal);
            shareSliceButton.disableProperty().bind(manifestUnavailableVal);
        } else {
            restartExperimentButton.setDisable(true);
            editSshKeysExperimentButton.setDisable(true);
            shareSliceButton.setDisable(true);
        }
    }


    @FXML
    private void onRebootExperimentAction() {
        eventBus.post(new RebootExperimentEvent(restartExperimentButton.getScene().getWindow(),
                sliceRibbonTab.getActiveExperimentViewController().getExperimentController()));
    }

    @FXML
    private void onEditSshKeysExperimentAction() {
        eventBus.post(new EditSshKeysEvent(editSshKeysExperimentButton.getScene().getWindow(),
                sliceRibbonTab.getActiveExperimentViewController().getExperimentController()));
    }

    @FXML
    private void onShareSliceAction() {
        eventBus.post(new ShareExperimentEvent(shareSliceButton.getScene().getWindow(),
                sliceRibbonTab.getActiveExperimentViewController().getExperimentController()));
    }
}
