package leigh.elements.javafx.type1;

import javafx.scene.Node;
import javafx.scene.layout.VBox;
import leigh.elements.ElementsSort;
import leigh.elements.ParentElement;
import leigh.elements.sdk.AppScreenInstance;
import leigh.elements.sdk.type1.Element;
import leigh.elements.sdk.type1.PanelGroupElement;
import leigh.eo.EO;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;

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

public class PanelGroupElementJFX extends ParentElement implements ElementJFX {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(PanelGroupElementJFX.class);
    private VBox root;

    protected PanelGroupElementJFX(EO cfg, AppScreenInstance controller) {
        super(cfg, controller);
        buildChildren(PanelGroupElement.KEY_CONTENT);
    }

    @Override
    public String toString() {
        return "PanelGroupElementJFX{" +
                "root=" + root +
                "} " + super.toString();
    }

    @Override
    public Node getJavaFXNode() {
        root = new VBox();

        // FIXME: Set Locale properly
        ArrayList<Element> children = ElementsSort.sort(
                getChildren().get(PanelGroupElement.KEY_CONTENT),
                "index", Locale.ENGLISH);

        for (Element child : children) {
            if (child instanceof ElementJFX) {
                logger.debug("Adding child node: {}", child);
                root.getChildren().add(((ElementJFX) child).getJavaFXNode());
            }
        }

        return root;
    }
}
