package leigh.elements.jfoenix.type1;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.layout.GridPane;
import leigh.elements.ElementsSort;
import leigh.elements.ParentElement;
import leigh.elements.javafx.type1.ElementJFX;
import leigh.elements.sdk.AppScreenInstance;
import leigh.elements.sdk.type1.Element;
import leigh.elements.sdk.type1.FormLayout;
import leigh.eo.EO;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

import java.util.ArrayList;
import java.util.Locale;

/**
 * Implementation of a FormLayoutElement for JavaFX. This is the JFoenix equivalent of the
 * {@link leigh.elements.javafx.type1.FormLayoutJFX} JFX implementation.
 *
 * @author Alex Leigh
 */
public class FormLayoutJFoenix extends ParentElement implements ElementJFX {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(FormLayoutJFoenix.class);
    GridPane grid = new GridPane();

    protected FormLayoutJFoenix(EO cfg, AppScreenInstance controller) {
        super(cfg, controller);
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(30);
        grid.setPadding(new Insets(30, 30, 30, 30));
        buildChildren(FormLayout.KEY_FIELDS);

        int row = 0;

        logger.debug("Found fields: {}", getChildren());

        ArrayList<Element> children = ElementsSort.sort(
                getChildren().get(FormLayout.KEY_FIELDS),
                "uiIndex", Locale.ENGLISH);

        for (Element field : children) {
            grid.add(((ElementJFX) field).getJavaFXNode(), 0, row);
            row++;
        }
    }

    @Override
    public Node getJavaFXNode() {
        return grid;
    }
}
