package be.iminds.ilabt.jfed.experimenter_gui.util.ui;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;

/**
 * User: twalcari
 * Date: 11/19/13
 * Time: 1:39 PM
 */
public class TextFieldWithStatus extends TextField implements ChangeListener<TextFieldWithStatus.Status> {

    private final ObjectProperty<Status> status = new SimpleObjectProperty<>(Status.NONE);
    private final ObjectProperty<String> statusDetailString = new SimpleObjectProperty<>("");

    public TextFieldWithStatus() {
        initialize();
    }

    public TextFieldWithStatus(String s) {
        super(s);
        initialize();
    }

    private void initialize() {
        getStylesheets().add(getClass().getResource("textfield-with-status.css").toExternalForm());
        this.status.addListener(this);

        Tooltip tooltip = new Tooltip();
        this.setTooltip(tooltip);
        tooltip.textProperty().bind(statusDetailString);
    }

    public Status getStatus() {
        return status.get();
    }

    public void setStatus(Status status) {
        this.status.set(status);
    }

    public ObjectProperty<Status> statusProperty() {
        return status;
    }

    public String getStatusDetailString() {
        return statusDetailString.get();
    }

    public ObjectProperty<String> statusDetailStringProperty() {
        return statusDetailString;
    }

    public void setStatusDetailString(String statusDetailString) {
        this.statusDetailString.set(statusDetailString);
    }

    @Override
    public void changed(ObservableValue<? extends Status> observableValue, Status oldValue, Status newValue) {
        getStyleClass().remove(oldValue.styleName);
        getStyleClass().add(newValue.styleName);
    }

    public enum Status {
        NONE("textfield-normal"),
        OK("textfield-ok"),
        WARNING("textfield-warning"),
        ERROR("textfield-error");
        private final String styleName;

        Status(String styleName) {
            this.styleName = styleName;
        }
    }
}
