package p.studio.controls.banks;

import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;

public final class StudioAssetCapacityMeter extends VBox {
    private static final String BASE_SEVERITY_CLASS = "studio-asset-capacity-meter-fill-";

    private final AnchorPane track = new AnchorPane();
    private final Region fill = new Region();
    private final Label label = new Label();
    private final Label hint = new Label();

    private double progress;
    private StudioAssetCapacitySeverity severity = StudioAssetCapacitySeverity.GREEN;

    public StudioAssetCapacityMeter() {
        setAlignment(Pos.TOP_CENTER);
        setSpacing(8);
        getStyleClass().add("studio-asset-capacity-meter");

        track.getStyleClass().add("studio-asset-capacity-meter-track");
        track.setMinWidth(28);
        track.setPrefWidth(28);
        track.setPrefHeight(180);
        VBox.setVgrow(track, Priority.ALWAYS);

        fill.getStyleClass().addAll("studio-asset-capacity-meter-fill", BASE_SEVERITY_CLASS + severity.name().toLowerCase());
        AnchorPane.setLeftAnchor(fill, 0.0d);
        AnchorPane.setRightAnchor(fill, 0.0d);
        AnchorPane.setBottomAnchor(fill, 0.0d);
        track.getChildren().add(fill);

        label.getStyleClass().add("studio-asset-capacity-meter-label");
        hint.getStyleClass().add("studio-asset-capacity-meter-hint");
        hint.setWrapText(true);

        track.heightProperty().addListener((ignored, oldValue, newValue) -> refreshFillHeight(newValue.doubleValue()));

        getChildren().setAll(track, label, hint);
        refreshFillHeight(track.getPrefHeight());
    }

    public void setProgress(double progress) {
        this.progress = Math.max(0.0d, Math.min(1.0d, progress));
        refreshFillHeight(track.getHeight() <= 0.0d ? track.getPrefHeight() : track.getHeight());
    }

    public void setSeverity(StudioAssetCapacitySeverity severity) {
        this.severity = severity == null ? StudioAssetCapacitySeverity.GREEN : severity;
        fill.getStyleClass().removeIf(styleClass -> styleClass.startsWith(BASE_SEVERITY_CLASS));
        fill.getStyleClass().add(BASE_SEVERITY_CLASS + this.severity.name().toLowerCase());
    }

    public void setLabelText(String text) {
        label.setText(text == null ? "" : text);
    }

    public void setHintText(String text) {
        final String safeText = text == null ? "" : text;
        hint.setText(safeText);
        hint.setVisible(!safeText.isBlank());
        hint.setManaged(!safeText.isBlank());
    }

    private void refreshFillHeight(double trackHeight) {
        final double effectiveTrackHeight = Math.min(Math.max(0.0d, trackHeight), track.getPrefHeight());
        final double fillHeight = effectiveTrackHeight * progress;
        fill.setMinHeight(fillHeight);
        fill.setPrefHeight(fillHeight);
        fill.setMaxHeight(fillHeight);
    }
}
