package niagara.api.parameters.parametersUI;

import javafx.beans.property.ReadOnlyProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import niagara.api.exceptions.IncorrectParameterValueException;
import niagara.api.internationalisation.ILocalisationService;
import niagara.api.parameters.IParameter;
import niagara.api.parameters.IParameterUI;

/**
 * Created by Youkool on 21/03/2017.
 *
 * A very simple class to handle the value of a String parameter.
 */
public class StringParameterUI extends IParameterUI<String> {

    private SimpleStringProperty value;
    private IParameter<String> parameter;
    private ILocalisationService localisationService;

    public StringParameterUI(IParameter<String> origin, ILocalisationService localisationService){
        super();

        this.localisationService = localisationService;
        value = new SimpleStringProperty(origin.getValue());
        parameter = origin;
        initUI();
    }

    /**
     * Initialize the user interface.
     */
    private void initUI() {
        HBox container = new HBox();

        Label name = new Label();
        name.textProperty().bind(localisationService.getPropertyFromInternationalText("", parameter.getName(), " : "));

        TextField tf = new TextField();
        tf.textProperty().bindBidirectional(value);

        container.getChildren().addAll(name,tf);
        container.alignmentProperty().setValue(Pos.BASELINE_CENTER);

        this.getChildren().add(container);
    }

    @Override
    public IParameter<String> getNewParameter() throws IncorrectParameterValueException {
        return parameter.getNewInstance(value.getValue());
    }

    @Override
    public ReadOnlyProperty<String> valueProperty() {
        return value;
    }



}
