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

import be.iminds.ilabt.jfed.experimenter_gui.canvas.rspec.RspecCanvasNode;
import be.iminds.ilabt.jfed.experimenter_gui.editor.views.EditableCanvasEditorView;
import be.iminds.ilabt.jfed.experimenter_gui.editor.views.EditableExperimentCanvas;
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.ui.javafx.FXMLUtil;
import be.iminds.ilabt.jfed.ui.javafx.util.BindingsUtil;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;

/**
 * User: twalcari
 * Date: 7/1/2014
 * Time: 11:10
 */
public class TopologyEditorRibbonTab extends EditorRibbonTab {

    private final RspecEditorRibbonTab rspecEditorRibbonTab;

    @FXML
    private RibbonButton pasteButton;
    @FXML
    private RibbonButton copyButton;
    @FXML
    private RibbonButton duplicateButton;
    @FXML
    private RibbonButton zoomInButton;
    @FXML
    private RibbonButton zoomOutButton;
    @FXML
    private RibbonButton resetZoomButton;


    public TopologyEditorRibbonTab(RspecEditorRibbonTab rspecEditorRibbonTab) {
        this.rspecEditorRibbonTab = rspecEditorRibbonTab;

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

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

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

        activeExperimentEditor.addListener((observableValue, oldValue, newValue) -> {
            if (oldValue != null) {
                unbindButtons();
            }

            if (newValue != null && newValue.getCurrentEditorViewType() == EditorViewType.CANVAS) {
                //only bind if canvas is active
                bindButtons();
            }

        });

    }

    private EditableCanvasEditorView getActiveCanvasEditorView() {
        assert (getActiveExperimentEditor().getCurrentEditorViewType() == EditorViewType.CANVAS);

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

    private void bindButtons() {
        assert (getActiveCanvasEditorView() != null);
        assert (getActiveCanvasEditorView().getCanvas() != null);
        EditableExperimentCanvas canvas = getActiveCanvasEditorView().getCanvas();


        copyButton.disableProperty().bind(
                Bindings.not(BindingsUtil.instanceOfBinding(canvas.getSelectionProvider().selectedObjectProperty(), RspecCanvasNode.class)));
        duplicateButton.disableProperty().bind(
                Bindings.not(BindingsUtil.instanceOfBinding(canvas.getSelectionProvider().selectedObjectProperty(), RspecCanvasNode.class)));
        pasteButton.disableProperty().bind(canvas.getClipboard().emptyProperty());
    }

    private void unbindButtons() {
        copyButton.disableProperty().unbind();
        duplicateButton.disableProperty().unbind();
        pasteButton.disableProperty().unbind();
    }


    /*
        Edit section
     */

    @FXML
    private void onCopyAction() {
        getActiveCanvasEditorView().getCanvas().copySelected();
    }

    @FXML
    private void onPasteAction() {
        getActiveCanvasEditorView().getCanvas().paste();
    }

    @FXML
    private void onDuplicateAction() {
        getActiveCanvasEditorView().getCanvas().duplicateSelected();
    }

    @FXML
    private void onAutoLayoutAction() {
        getActiveCanvasEditorView().autoLayout();
    }

    /*
        Zoom section
     */

    @FXML
    public void onZoomInAction() {
        getActiveCanvasEditorView().getCanvas().zoomIn();
    }

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

    @FXML
    public void onResetZoomAction() {
        getActiveCanvasEditorView().getCanvas().setZoom(1);
    }
}
