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

import be.iminds.ilabt.jfed.experiment.Experiment;
import be.iminds.ilabt.jfed.experiment.ExperimentState;
import be.iminds.ilabt.jfed.experimenter_gui.slice.ExperimentViewController;
import be.iminds.ilabt.jfed.experimenter_gui.slice.events.RenewExperimentEvent;
import be.iminds.ilabt.jfed.experimenter_gui.slice.events.TerminateExperimentEvent;
import be.iminds.ilabt.jfed.experimenter_gui.slice.events.UpdateExperimentEvent;
import be.iminds.ilabt.jfed.experimenter_gui.ui.ribbon.RibbonButton;
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 GenericSliceRibbonComponentGroupController {
    private final SliceRibbonTab sliceRibbonTab;

    @FXML
    private RibbonButton updateExperimentButton;
    @FXML
    private RibbonButton renewSliceButton;
    @FXML
    private RibbonButton stopExperimentButton;

    private final EventBus eventBus;

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

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

    @FXML
    protected void initialize() {
        final Val<Experiment> experimentVal = Val.map(sliceRibbonTab.activeExperimentViewControllerProperty(),
                ExperimentViewController::getExperiment);

        Val<Boolean> sliceIsNullVal = experimentVal.flatMap(Experiment::sliceProperty)
                .map(Objects::isNull)
                .orElseConst(true);

        updateExperimentButton.disableProperty().bind(sliceIsNullVal);

        if (!isDemo) {
            final Val<Boolean> terminatedVal =
                    experimentVal.flatMap(Experiment::experimentStateProperty)
                            .map(ExperimentState::isTerminated);

            final Val<Boolean> sliceIsNullOrTerminatingVal = sliceIsNullVal.orElse(terminatedVal);
            renewSliceButton.disableProperty().bind(sliceIsNullOrTerminatingVal);
            stopExperimentButton.disableProperty().bind(sliceIsNullOrTerminatingVal);
        } else {
            renewSliceButton.setDisable(true);
            stopExperimentButton.setDisable(true);
        }
    }

    @FXML
    public void onUpdateExperimentAction() {
        eventBus.post(new UpdateExperimentEvent(updateExperimentButton.getScene().getWindow(),
                sliceRibbonTab.getActiveExperimentViewController().getExperimentController()));
    }

    @FXML
    public void onStopExperimentAction() {
        eventBus.post(new TerminateExperimentEvent(updateExperimentButton.getScene().getWindow(),
                sliceRibbonTab.getActiveExperimentViewController().getExperimentController()));
    }


    @FXML
    private void onRenewSliceAction() {
        eventBus.post(new RenewExperimentEvent(updateExperimentButton.getScene().getWindow(),
                sliceRibbonTab.getActiveExperimentViewController().getExperimentController()));
    }
}
