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

import be.iminds.ilabt.jfed.experimenter_gui.timeline.controller.TimelineController;
import be.iminds.ilabt.jfed.experimenter_gui.editor.views.EditableControllerView;
import be.iminds.ilabt.jfed.experimenter_gui.editor.views.EditorViewType;
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.ui.javafx.FXMLUtil;
import javafx.fxml.FXML;

/**
 * User: twalcari
 * Date: 7/1/2014
 * Time: 11:10
 */
public class ControlEditorRibbonTab extends EditorRibbonTab {
    private final RspecEditorRibbonTab rspecEditorRibbonTab;

    @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 ControlEditorRibbonTab(RspecEditorRibbonTab rspecEditorRibbonTab) {
        this.rspecEditorRibbonTab = rspecEditorRibbonTab;

        FXMLUtil.injectFXML(this, aClass -> {
            if (aClass == GenericEditorRibbonComponentGroupController.class) {
                return new GenericEditorRibbonComponentGroupController(ControlEditorRibbonTab.this);
            }
            throw new RuntimeException("Unexpected class in ControllerFactory: " + aClass.getName());
        });


        setOnSelectionChanged(event -> {
            if (isSelected() && getActiveExperimentEditor().getCurrentEditorViewType() != EditorViewType.CONTROLLER) {
                boolean success = getActiveExperimentEditor().trySwitchToEditorViewByType(EditorViewType.CONTROLLER);

                if (!success) {
                    //revert back to raw editor
                    getTabPane().getSelectionModel().select(ControlEditorRibbonTab.this.rspecEditorRibbonTab);
                }
            }
        });


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

    private EditableControllerView getActiveEditableControllerView() {
        assert (getActiveExperimentEditor().getCurrentEditorViewType() == EditorViewType.CONTROLLER);

        return (EditableControllerView) getActiveExperimentEditor().getCurrentEditableRspecView();
    }

    private TimelineController getActiveController() {
        return getActiveEditableControllerView().getTimelineView().getController();
    }

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

    @FXML
    public void onZoomOutAction() {
        getActiveController().zoomOut();
    }

    @FXML
    public void onResetZoomAction() {
        getActiveController().resetZoom();
    }

    /*
        Control Actions
     */

    @FXML
    private void onAddCommandAction() {
        getActiveEditableControllerView().showAddCommandDialog();
    }

    @FXML
    private void onAddBarrierAction() {
        getActiveController().addBarrierSegment();
    }
}
