package be.iminds.ilabt.jfed.experimenter_gui.connectivity_tester;

import be.iminds.ilabt.jfed.connectivity_tester.ConnectivityTest;
import be.iminds.ilabt.jfed.ui.javafx.FXMLUtil;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * User: twalcari
 * Date: 1/6/14
 * Time: 11:58 AM
 */
public class ConnectivityTestPane extends GridPane {
    private static final Logger LOG = LoggerFactory.getLogger(ConnectivityTestPane.class);

    private final ObjectProperty<ConnectivityTest.ConnectivityTestResult> result = new SimpleObjectProperty<>();

   /* @FXML
    private TaskStatusIndicator taskStatusIndicator;          */
    @FXML
    private TextField statusTextField;
    @FXML
    private TextArea messageTextArea;
    @FXML
    private TextArea exceptionTextArea;

    public ConnectivityTestPane() {
        FXMLUtil.injectFXML(this);
    }

    @FXML
    private void initialize() {
        result.addListener(observable -> {
            final ConnectivityTest.ConnectivityTestResult connectivityTestResult = result.get();
            if(connectivityTestResult != null) {
                statusTextField.setText(connectivityTestResult.getStatus().toString());
                messageTextArea.setText(connectivityTestResult.getMessage());
                exceptionTextArea.setText(connectivityTestResult.getExceptionString());
                exceptionTextArea.setDisable(connectivityTestResult.getException() == null);
            } else{
                statusTextField.setText("");
                messageTextArea.setText("");
                exceptionTextArea.setText("");
            }
        });
    }

    public ConnectivityTest.ConnectivityTestResult getResult() {
        return result.get();
    }

    public ObjectProperty<ConnectivityTest.ConnectivityTestResult> resultProperty() {
        return result;
    }

    public void setResult(ConnectivityTest.ConnectivityTestResult result) {
        this.result.set(result);
    }
}
