package fi.lpam.gui.elementit;

import com.dlsc.gemsfx.MultiColumnListView;
import fi.lpam.dataluokat.Asiakas;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Node;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;


public class AsiakasListCell extends MultiColumnListView.ColumnListCell<Asiakas> {
    private final StackPane wrapper;

    public AsiakasListCell(MultiColumnListView multiColumnListView) {
        //noinspection unchecked
        super(multiColumnListView);
        this.setHeight(150);
        this.setPrefHeight(50);


        VBox content = new VBox();
        content.visibleProperty().bind(placeholder.not().and(emptyProperty().not()));
        content.managedProperty().bind(placeholder.not().and(emptyProperty().not()));

        VBox contentPlaceholder = new VBox();
        contentPlaceholder.visibleProperty().bind(placeholder);
        contentPlaceholder.managedProperty().bind(placeholder);

        Label label = new Label();
        label.textProperty().bind(textProperty());

        wrapper = new StackPane(content, contentPlaceholder, label);
        setGraphic(wrapper);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    }

    @Override
    protected Node getSnapshotNode() {
        return wrapper;
    }

    private final BooleanProperty placeholder = new SimpleBooleanProperty(this, "placeholder", false);

    @Override
    protected void updateItem(Asiakas Asiakas, boolean empty) {
        super.updateItem(Asiakas, empty);

        placeholder.set(false);


        if (Asiakas != null && !empty) {
            if (Asiakas == getMultiColumnListView().getPlaceholderFrom()) {
                placeholder.set(true);
                setText(Asiakas.getNimi() + ": " + Asiakas.getOsoite());
            } else if (Asiakas == getMultiColumnListView().getPlaceholderTo()) {
                placeholder.set(true);
                setText(Asiakas.getNimi() + ": " + Asiakas.getOsoite());
            } else {
                setText(Asiakas.getNimi() + ": " + Asiakas.getOsoite());
            }
        } else {
            setText("");
        }
    }
}