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

import be.iminds.ilabt.jfed.experimenter_gui.timeline.view.dialogs.AddMultipleCommandDialog;
import be.iminds.ilabt.jfed.experimenter_gui.timeline.view.dialogs.FastForwardDialog;
import be.iminds.ilabt.jfed.experimenter_gui.timeline.view.dialogs.InstantCommandDialog;
import be.iminds.ilabt.jfed.experimenter_gui.ui.ribbon.RibbonButton;
import be.iminds.ilabt.jfed.experimenter_gui.ui.ribbon.RibbonComponentGroup;
import be.iminds.ilabt.jfed.experimenter_gui.ui.ribbon.RibbonToggleButton;
import be.iminds.ilabt.jfed.rspec.model.controller.Timestamp;
import be.iminds.ilabt.jfed.ui.javafx.FXMLUtil;
import com.google.common.eventbus.EventBus;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;

/**
 * User: twalcari
 * Date: 7/1/2014
 * Time: 17:31
 */
public class ControlViewerRibbonTab extends SliceRibbonTab {

    @FXML
    private RibbonComponentGroup controllerGroup;
    @FXML
    private RibbonButton startControllerButton;
    @FXML
    private RibbonButton pauseControllerButton;
    @FXML
    private RibbonButton stopControllerButton;
    @FXML
    private RibbonToggleButton repeatButton;
    @FXML
    private RibbonButton fastForwardControllerButton;


    @FXML
    private RibbonButton zoomInButton;
    @FXML
    private RibbonButton zoomOutButton;
    @FXML
    private RibbonButton resetZoomButton;

    @FXML
    private RibbonComponentGroup definitionGroup;
    @FXML
    private RibbonButton addCommandButton;
    @FXML
    private RibbonButton addBarrierButton;


    public ControlViewerRibbonTab(EventBus eventBus) {
        FXMLUtil.injectFXML(this, aClass -> {
            if (aClass == GenericSliceRibbonComponentGroupController.class) {
                return new GenericSliceRibbonComponentGroupController(ControlViewerRibbonTab.this, eventBus);
            } else if (aClass == AdvancedSliceRibbonComponentGroupController.class) {
                return new AdvancedSliceRibbonComponentGroupController(ControlViewerRibbonTab.this, eventBus);
            }
            throw new RuntimeException("Unexpected class in ControllerFactory: " + aClass.getName());
        });

        setOnSelectionChanged(event -> {
            if (isSelected() && !getActiveExperimentViewController().isControllerVisible()) {
                getActiveExperimentViewController().switchToExperimentController();
            }
        });


        activeExperimentViewControllerProperty().addListener((observableValue, oldValue, newValue) -> {

            if (oldValue != null) {
                controllerGroup.disableProperty().unbind();
                startControllerButton.disableProperty().unbind();
                pauseControllerButton.disableProperty().unbind();
                stopControllerButton.disableProperty().unbind();
                repeatButton.selectedProperty().unbind();
            }

            if (newValue != null) {

                if (newValue.getExperimentController() != null) {
                    controllerGroup.disableProperty().bind(
                            Bindings.not(newValue.getTimelineView().onlineProperty()));

                    startControllerButton.disableProperty().bind(
                            newValue.getTimelineView().getModel().startedProperty());
                    pauseControllerButton.disableProperty().bind(
                            newValue.getTimelineView().getModel().stoppedProperty());
                    stopControllerButton.disableProperty().bind(
                            newValue.getTimelineView().getModel().elapsedTimeProperty().isEqualTo(0L));

                    repeatButton.selectedProperty().bindBidirectional(
                            newValue.getTimelineView().getModel().repeatingProperty());

                }
            }
        });




        //The CSS on this rotate seems to be ignored.. adding it here works
        addBarrierButton.getGraphic().setRotate(90);

    }

    /*
            Zoom Actions
         */
    @FXML
    public void onZoomInAction() {
        getActiveExperimentViewController().getTimelineView().getController().zoomIn();
    }

    @FXML
    public void onZoomOutAction() {
        getActiveExperimentViewController().getTimelineView().getController().zoomOut();
    }

    @FXML
    public void onResetZoomAction() {
        getActiveExperimentViewController().getTimelineView().getController().resetZoom();
    }

    /*
        Controller definition Actions
     */

    @FXML
    private void onAddCommandAction() {
        AddMultipleCommandDialog.showSimpleAddDialog(addCommandButton.getScene().getWindow(),
                getActiveExperimentViewController().getTimelineView().getController(),
                getActiveExperimentViewController().getTimelineView().getModelRspec().getNodes());

    }

    @FXML
    private void onAddBarrierAction() {
        getActiveExperimentViewController().getTimelineView().getController().addBarrierSegment();
    }

    /*
       Controller Viewer actions
     */

    @FXML
    private void onStartControllerAction() {
        getActiveExperimentViewController().getTimelineView().getController().startTime();
    }

    @FXML
    private void onPauseControllerAction() {
        getActiveExperimentViewController().getTimelineView().getController().stopTime();
    }

    @FXML
    private void onStopControllerAction() {
        getActiveExperimentViewController().getTimelineView().getController().stopTime();
        getActiveExperimentViewController().getTimelineView().getController().rewindTime();
    }

    @FXML
    private void onFastForwardControllerAction() {
        FastForwardDialog.showSimpleFastForwardDialog(this.getTabPane().getScene().getWindow(),
                getActiveExperimentViewController().getTimelineView().getController(),
                new Timestamp(getActiveExperimentViewController().getTimelineView().getModel().getElapsedTime()));

    }

    @FXML
    private void onInstantCommandAction() {
        InstantCommandDialog.showSimpleInstantCommandDialog(this.getTabPane().getScene().getWindow(),
                getActiveExperimentViewController().getTimelineView().getController(),
                getActiveExperimentViewController().getTimelineView().getModelRspec().getNodes());

    }

    @FXML
    private void onSaveExperimentResultsAction() {
        getActiveExperimentViewController().getTimelineView().getController().saveOutput();
    }
}
