package io.xpipe.app.pwman;

import io.xpipe.app.comp.SimpleRegionBuilder;
import io.xpipe.app.comp.base.IconButtonComp;

import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;

import atlantafx.base.theme.Styles;

public class KeePassXcAssociationComp extends SimpleRegionBuilder {

    private final KeePassXcAssociationKey associationKey;
    private final Runnable onRemove;

    public KeePassXcAssociationComp(KeePassXcAssociationKey associationKey, Runnable onRemove) {
        this.associationKey = associationKey;
        this.onRemove = onRemove;
    }

    @Override
    protected Region createSimple() {
        var key = associationKey.getKey().getSecretValue();
        var censoredKey = key.substring(0, 6) + "*".repeat(key.length() - 6);

        var nameLabel = new Label(associationKey.getId());
        nameLabel.getStyleClass().add(Styles.TEXT_BOLD);
        nameLabel.setPrefWidth(150);
        var keyLabel = new Label(censoredKey);
        keyLabel.setMaxWidth(2000);
        HBox.setHgrow(keyLabel, Priority.ALWAYS);
        var delButton = new IconButtonComp("mdi2t-trash-can-outline", onRemove).build();
        var box = new HBox(nameLabel, keyLabel, delButton);
        box.setSpacing(8);
        box.setPadding(new Insets(5, 0, 5, 0));
        return box;
    }
}
