package components;

import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import java.util.HashMap;
import java.util.Map;

public class IntTableStatRow extends VBox {
    String title;

    Label titleLabel = new Label();
    HBox keys = new HBox(5);
    HBox values = new HBox(5);

    Map<Integer, Integer> valueMap = new HashMap<>();

    public IntTableStatRow(String title) {
        this.title = title;

        titleLabel.setText(this.title);
        this.getChildren().add(titleLabel);
        this.getChildren().add(keys);
        this.getChildren().add(values);
        updateValueGui();
    }

    public void increaseValue(int value) {
        valueMap.put(value, valueMap.getOrDefault(value, 0) + 1);
        updateValueGui();
    }

    public void decreaseValue(int value) {
        valueMap.put(value, valueMap.getOrDefault(value, 0) - 1);
        updateValueGui();
    }

    public void setValue(int key, int value) {
        valueMap.put(key, value);
        updateValueGui();
    }

    private void updateValueGui() {
        keys.getChildren().clear();
        values.getChildren().clear();
        valueMap.forEach((key, value) -> {
            keys.getChildren().add(new Label(String.valueOf(key)));
            values.getChildren().add(new Label(String.valueOf(value)));
        });
    }


    public int getValue(int key) {
        return valueMap.getOrDefault(key, 0);
    }
}