package p.studio.controls.forms;

import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import p.studio.controls.lifecycle.StudioControlLifecycle;
import p.studio.controls.lifecycle.StudioControlLifecycleSupport;

public abstract class StudioSection extends VBox implements StudioControlLifecycle {
    protected StudioSection() {
        StudioControlLifecycleSupport.install(this, this);
    }

    public static VBox createSection(String title, Node content) {
        return createSection(title, null, content, false, false, true);
    }

    public static VBox createSectionWithoutHeader(Node content) {
        return createSection(null, null, content, false, false, false);
    }

    protected static VBox createSection(
            String title,
            Node headerActions,
            Node content,
            boolean editable,
            boolean editing,
            boolean showHeader) {
        final VBox section = new VBox(10);
        section.getStyleClass().add("studio-form-section");
        if (editable) {
            section.getStyleClass().add("studio-form-section-editable");
        }
        if (editing) {
            section.getStyleClass().add("studio-form-section-editing");
        }

        if (showHeader) {
            final Label titleLabel = new Label(title == null ? "" : title);
            titleLabel.getStyleClass().add("studio-form-section-title");

            final Region spacer = new Region();
            HBox.setHgrow(spacer, Priority.ALWAYS);

            final HBox header = new HBox(12, titleLabel, spacer);
            header.getStyleClass().add("studio-form-section-header");
            if (headerActions != null) {
                header.getChildren().add(headerActions);
            }
            section.getChildren().add(header);
        }

        section.getChildren().add(content);
        return section;
    }
}
