package com.ecep.contract.util;

import javafx.beans.property.Property;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;

public class StringConfig extends AbstractConfigBounder<String> {

    public StringConfig(String string) {
        super(string);
    }

    // @Override
    // public TextField getControl() {
    // return (TextField) super.getControl();
    // }

    @Override
    public StringConverter<String> getConverter() {
        StringConverter<String> converter = super.getConverter();
        if (converter == null) {
            converter = new StringConverter<String>() {
                @Override
                public String toString(String object) {
                    return object;
                }

                @Override
                public String fromString(String string) {
                    return string;
                }
            };
            setConverter(converter);
        }
        return converter;
    }

    @Override
    void bindBidirectional(Property<String> property) {
        Control ctrl = getControl();
        if (ctrl == null) {
            return;
        }
        if (ctrl instanceof TextField textField) {
            textField.textProperty().bindBidirectional(property);
        } else if (ctrl instanceof Label label) {
            label.textProperty().bindBidirectional(property);
        }
    }
}
