package be.iminds.ilabt.jfed.experimenter_gui.timeline.view;

import be.iminds.ilabt.jfed.experiment.SshConnectionPool;
import be.iminds.ilabt.jfed.ui.javafx.util.StageUtils;
import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

/**
 * SshConnectionPoolDebugPanel
 */
public class SshConnectionPoolDebugPanel extends GridPane {
    private final SshConnectionPool sshConnectionPool;
    private final Map<String, Label> labelByNodeId = new HashMap<>();
    private int row = 0;

    public SshConnectionPoolDebugPanel(SshConnectionPool sshConnectionPool) {
        this.sshConnectionPool = sshConnectionPool;

        addLabel("All Connectable", null).textProperty().bind(Bindings.convert(sshConnectionPool.allConnectableProperty()));
        addLabel("All Connected", null).textProperty().bind(Bindings.convert(sshConnectionPool.allConnectedProperty()));
        addLabel("Any Connectable", null).textProperty().bind(Bindings.convert(sshConnectionPool.anyConnectableProperty()));
        addLabel("Any Connected", null).textProperty().bind(Bindings.convert(sshConnectionPool.anyConnectedProperty()));
        addLabel("Node Count", null).textProperty().bind(Bindings.convert(sshConnectionPool.getNodeIds().sizeProperty()));
        addLabel("Node ID", "Node State");

        sshConnectionPool.getNodeIds().addListener((InvalidationListener) observable -> update());
        update();
    }

    Label addLabel(String text1, String text2) {
        Label lab1 = new Label(text1);
        Label lab2 = text2 == null ? new Label() : new Label(text2);

        GridPane.setColumnIndex(lab1, 0);
        GridPane.setColumnIndex(lab2, 1);

        GridPane.setRowIndex(lab1, row);
        GridPane.setRowIndex(lab2, row);

        getChildren().addAll(lab1, lab2);

        row++;

        return lab2;
    }

    public void update() {
        for (String nodeId : sshConnectionPool.getNodeIds()) {
            Label stateLabel = labelByNodeId.get(nodeId);
            if (stateLabel == null) {
                stateLabel = addLabel(nodeId, "init "+sshConnectionPool.getNodeState(nodeId).get());

                stateLabel.textProperty().bind(Bindings.convert(sshConnectionPool.getNodeState(nodeId)));

                labelByNodeId.put(nodeId, stateLabel);
            } else {
                //nothing to do
            }
        }
    }

    public void show() {
        Stage sshConnectionPoolDebugStage = new Stage();
//        sshConnectionPoolDebugStage.initOwner(nullparentStage);
        sshConnectionPoolDebugStage.setTitle("Ssh Connection Pool Debug");
        StageUtils.setJFedStageIcons(sshConnectionPoolDebugStage);
        sshConnectionPoolDebugStage.setResizable(true);
        Scene scene = new Scene(this);
        sshConnectionPoolDebugStage.setScene(scene);
        sshConnectionPoolDebugStage.setMinWidth(300.0);
        sshConnectionPoolDebugStage.setMinHeight(300.0);
        sshConnectionPoolDebugStage.show();
    }
}
