package de.tud.kom.p2psim.impl.topology.views.visualization.ui;

import de.tud.kom.p2psim.impl.topology.views.VisualizationTopologyView;
import de.tud.kom.p2psim.impl.topology.views.visualization.world.NodeVisInteractionListener;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.NodeDebugMonitor;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.core.NodeDebugMonitorComponent;

import java.util.HashMap;

/**
 * Managing class for the presentation of the per node data. All the little "per-node-frames" are handled here.
 */
public class NodeDebugVisualisation implements NodeVisInteractionListener, NodeDebugMonitorComponent.NodeDebugUpdateListener
{
    /**
     * The info frames that currently exist.
     */
    private HashMap<Long, DebugNodeView> frames = new HashMap<>();

    public NodeDebugVisualisation()
    {
        Event.scheduleImmediately((content, type) -> {
            NodeDebugMonitor.addUpdateListener(NodeDebugVisualisation.this);
            VisualizationTopologyView.VisualizationInjector.addInteractionListener(NodeDebugVisualisation.this);
        }, null, 0);
    }

    @Override
    public void onHostClick(long hostID, boolean isActive)
    {
        if(isActive)
        {
            frames.put(hostID, new DebugNodeView(hostID));
        }
        else
        {
            DebugNodeView view = frames.remove(hostID);
            view.dispose();
        }
    }

    @Override
    public void onNodeDebugUpdate(Class subject, INodeID nodeId, String entry, Object value)
    {
        String string;
        if(value == null) string = "NULL";
        else string = value.toString();

        if(frames.containsKey(nodeId.value()))
        {
            frames.get(nodeId.value()).update(subject, entry, string);
        }

    }
}
