package be.iminds.ilabt.jfed.experimenter_gui.ui.ribbon;

import be.iminds.ilabt.jfed.ui.javafx.FXMLUtil;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;

/**
 * User: twalcari
 * Date: 11/6/13
 * Time: 9:45 AM
 */
public class RibbonComponentGroup extends VBox {
    private final ObservableList<Node> buttonChildren = FXCollections.observableArrayList();

    private static final int MAX_VERTICAL_SMALL_BUTTONS = 2;

    private int columnIndex = 0;
    private int rowIndex = 0;


    @FXML
    private GridPane buttonPane;

    @FXML
    private Label label;

    public RibbonComponentGroup() {
        super();

        FXMLUtil.injectFXML(this);

        buttonChildren.addListener((ListChangeListener<Node>) change -> {
            while (change.next()) {

                for (Node node : change.getAddedSubList()) {
                    boolean small = false;
                    if (node instanceof RibbonButton)
                        small = ((RibbonButton) node).isSmall();
                    else if (node instanceof RibbonToggleButton)
                        small = ((RibbonToggleButton) node).isSmall();

                    //the last added button was small, but this one isn't. We reset the position to the next column
                    if (!small && rowIndex > 0) {
                        columnIndex++;
                        rowIndex = 0;
                    }

                    if (small) {
                        buttonPane.add(node, columnIndex, rowIndex);
                    } else {
                        buttonPane.add(node, columnIndex, rowIndex, 1, MAX_VERTICAL_SMALL_BUTTONS);
                    }

                    //we determine the position for the next button
                    if (small) {
                        rowIndex++;

                        if (rowIndex == MAX_VERTICAL_SMALL_BUTTONS) {
                            columnIndex++;
                            rowIndex = 0;
                        }
                    } else {
                        columnIndex++;
                        rowIndex = 0;
                    }
                }

                for (Node node : change.getRemoved()) {
                    buttonPane.getChildren().remove(node);
                }
            }
        });

    }

    public GridPane getButtonPane() {
        return buttonPane;
    }

    public Label getLabel() {
        return label;
    }

    public String getLabelText() {
        return label.getText();
    }

    public void setLabelText(String text) {
        label.setText(text);
    }

    public ObservableList<Node> getButtonChildren() {
        return buttonChildren;
    }

    @Override
    public String toString() {
        return "RibbonComponentGroup{" +
                "buttonPane=" + buttonPane +
                ", label=" + label +
                '}';
    }
}
