package leigh.elements.javafx.type1;

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

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

public abstract class LayoutElementJFX extends ParentElement implements ElementJFX {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(HorizontalLayoutElementJFX.class);
    public static final String KEY_CONTENT = "content";

    protected LayoutElementJFX(EO cfg, AppScreenInstance screen) {
        super(cfg, screen);
        buildChildren(HorizontalLayoutElement.KEY_CONTENT);
    }

    public abstract Pane getPane();

    @Override
    public Node getJavaFXNode() {
        Pane root = getPane();

        logger.info("Children: {}", getChildren());

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

        for (Element child : children) {
            if (child instanceof ElementJFX) {
                logger.info("Adding child node: [idx: {}] [node: {}]", child.getCfg().get("uiIndex"), child);
                root.getChildren().add(((ElementJFX) child).getJavaFXNode());
            }
        }

        return root;
    }
}
