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

import be.iminds.ilabt.jfed.experimenter_gui.canvas.rspec.RspecCanvasNode;
import be.iminds.ilabt.jfed.experimenter_gui.editor.views.EditorViewType;
import be.iminds.ilabt.jfed.experimenter_gui.gts_editor.views.EditableGTSCanvas;
import be.iminds.ilabt.jfed.experimenter_gui.gts_editor.views.EditableGTSCanvasEditorView;
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 GTSTopologyEditorRibbonTab extends GTSEditorRibbonTab {

    private final GTSRawEditorRibbonTab gtsRawEditorRibbonTab;
    @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 GTSTopologyEditorRibbonTab(GTSRawEditorRibbonTab gtsRawEditorRibbonTab) {
        this.gtsRawEditorRibbonTab = gtsRawEditorRibbonTab;

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

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

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


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

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

        });

    }

    private EditableGTSCanvasEditorView getEditableGTSCanvasEditorView() {
        assert getActiveGtsEditor().getCurrentEditorViewType() == EditorViewType.CANVAS;

        return (EditableGTSCanvasEditorView) getActiveGtsEditor().getCurrentEditorView();
    }

    private void bindButtons() {
        assert (getActiveGtsEditor() != null);
        assert (getEditableGTSCanvasEditorView() != null);
        EditableGTSCanvas canvas = getEditableGTSCanvasEditorView().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() {
        getEditableGTSCanvasEditorView().getCanvas().copySelected();
    }

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

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

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

    /*
        Zoom section
     */

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

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

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