package leigh.elements.javafx.type1;

import javafx.scene.Node;
import javafx.scene.text.Text;
import leigh.elements.ElementBase;
import leigh.elements.sdk.type1.TextElement;
import leigh.eo.EO;
import leigh.eo.EOListener;
import leigh.eo.EditEventGenerator;
import leigh.mecha.util.Snavig;

import java.io.Serializable;

public class TextElementJFX extends ElementBase implements ElementJFX, EOListener {
    private Text textNode = new Text();

    protected TextElementJFX(EO cfg) {
        super(cfg);
        ((EditEventGenerator) cfg.getEditTracker()).addListener(cfg, this);
    }

    @Override
    public Node getJavaFXNode() {
        textNode.setText(getCfg().getValueString(TextElement.KEY_VALUE));
        return textNode;
    }

    @Override
    public String toString() {
        return "TextElementJFX{" +
                "textNode=" + textNode +
                "} " + super.toString();
    }

    @Override
    public void valueUpdated(EO eo, String key, Serializable oldValue, Serializable newValue) {
        if (TextElement.KEY_VALUE.equals(key)) {
            textNode.setText(Snavig.convertToString(newValue));
        }
    }
}
