package org.opennms.features.vaadin.nodemaps.internal.gwt.client;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.discotools.gwt.leaflet.client.popup.Popup;
import org.discotools.gwt.leaflet.client.popup.PopupImpl;
import org.discotools.gwt.leaflet.client.popup.PopupOptions;

import com.vaadin.terminal.gwt.client.VConsole;

final class NodeMarkerClusterCallback implements MarkerClusterEventCallback {
    NodeMarkerClusterCallback() {
    }

    @Override
    public final void run(final MarkerClusterEvent event) {
        final StringBuilder sb = new StringBuilder();
        final MarkerCluster cluster = event.getMarkerCluster();
        @SuppressWarnings("unchecked")
        final List<NodeMarker> markers = (List<NodeMarker>)cluster.getAllChildMarkers();
        VConsole.log("Clicked, processing " + markers.size() + " markers.");
        Collections.sort(markers, new Comparator<NodeMarker>() {
            final static int BEFORE = -1;
            final static int EQUAL = 0;
            final static int AFTER = 1;

            @Override
            public int compare(final NodeMarker left, final NodeMarker right) {
                if (left == right) return EQUAL;
                if (left.getSeverity() != right.getSeverity()) {
                    return left.getSeverity() > right.getSeverity()? BEFORE : AFTER;
                }
                if (left.getNodeLabel() != right.getNodeLabel()) {
                    if (left.getNodeLabel() == null) return AFTER;
                    if (right.getNodeLabel() == null) return BEFORE;
                    return left.getNodeLabel().toLowerCase().compareTo(right.getNodeLabel().toLowerCase());
                }
                return left.getNodeId().compareTo(right.getNodeId());
            }
        });

        if (markers.size() == 1) {
            final NodeMarker marker = markers.get(0);
            sb.append(getPopupTextForMarker(marker));
        } else {
            final StringBuilder nodeBuilder = new StringBuilder();
            int unacked = 0;
            for (final NodeMarker marker : markers) {
                unacked += marker.getUnackedCount();
                nodeBuilder.append("<li>");
                nodeBuilder.append(marker.getNodeLabel()).append(" ");
                nodeBuilder.append("(").append(marker.getIpAddress()).append(")").append(": ");
                nodeBuilder.append(marker.getSeverityLabel());
                nodeBuilder.append("</li>");
            }
            sb.append("<h2># of nodes: ").append(markers.size()).append(" ");
            sb.append("(").append(unacked).append(" Unacknowledged Alarms)");
            sb.append("</h2>");
            sb.append("<ul>").append(nodeBuilder).append("</ul>");
        }
        final PopupOptions options = new PopupOptions();
        options.setMaxWidth(500);
        options.setProperty("maxHeight", 250);
        final Popup popup = new Popup(options);
        popup.setContent(sb.toString());
        popup.setLatLng(cluster.getLatLng());
        VConsole.log("html = " + sb.toString());
        PopupImpl.openOn(popup.getJSObject(), cluster.getGroup().getMapObject());
    }

    public static String getPopupTextForMarker(final NodeMarker marker) {
        // TODO: THIS IS AWFUL
        final StringBuilder sb = new StringBuilder();
        sb.append("<h2><a href=\"/opennms/element/node.jsp?node=" + marker.getNodeId() + "\" target=\"_blank\">Node ").append(marker.getNodeLabel()).append("</a></h2>");
        sb.append("<p>");
        sb.append("Node ID: ").append(marker.getNodeId()).append("<br/>");
        sb.append("Foreign Source: ").append(marker.getForeignSource()).append("<br/>");
        sb.append("Foreign ID: ").append(marker.getForeignId()).append("<br/>");
        sb.append("IP Address: ").append(marker.getIpAddress()).append("<br/>");
        sb.append("Severity: ").append(marker.getSeverityLabel());
        sb.append("</p>");
        return sb.toString();
    }
}

@@ -452,479 +452,66 @@
     
-NodeMarkerClusterCallback() %7B%0A    %7D%0A%0A    @Override%0A    public final void run(final MarkerClusterEvent event) %7B%0A        final StringBuilder sb = new StringBuilder();%0A        final MarkerCluster cluster = event.getMarkerCluster();%0A        @SuppressWarnings(%22unchecked%22)%0A        final List%3CNodeMarker%3E markers = (List%3CNodeMarker%3E)cluster.getAllChildMarkers();%0A        VConsole.log(%22Clicked, processing %22 + markers.size() + %22 markers.%22);%0A        Collections.sort(markers, new
+private static final class NodeMarkerComparator implements
  Com
@@ -533,17 +533,11 @@
 ker%3E
-()
  %7B%0A
-    
     
@@ -570,20 +570,17 @@
 E = -1;%0A
-    
+%0A
         
@@ -607,26 +607,25 @@
 AL = 0;%0A
-  
+%0A
         
   final 
@@ -612,26 +612,24 @@
 0;%0A%0A        
-  
 final static
@@ -653,20 +653,16 @@
         
-  
-  
 @Overrid
@@ -663,20 +663,16 @@
 verride%0A
-    
         
@@ -735,28 +735,24 @@
 er right) %7B%0A
-    
             
@@ -788,36 +788,32 @@
 AL;%0A            
-    
 if (left.getSeve
@@ -841,28 +841,24 @@
 verity()) %7B%0A
-    
             
@@ -934,34 +934,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -1011,36 +1011,32 @@
                 
-   
- 
 if (left.getNode
@@ -1058,36 +1058,32 @@
 ) return AFTER;%0A
-    
                 
@@ -1131,20 +1131,16 @@
 BEFORE;%0A
-    
         
@@ -1251,107 +1251,814 @@
     
+%7D%0A
     
-%7D%0A                return left.getNodeId().compareTo(right.getNodeId());%0A            %7D%0A        %7D
+        if (left.getNodeId() != right.getNodeId()) %7B%0A                if (left.getNodeId() == null) return AFTER;%0A                if (right.getNodeId() == null) return BEFORE;%0A                return left.getNodeId().compareTo(right.getNodeId());%0A            %7D%0A            return EQUAL;%0A        %7D%0A    %7D%0A%0A    NodeMarkerClusterCallback() %7B%0A    %7D%0A%0A    @Override%0A    public final void run(final MarkerClusterEvent event) %7B%0A        final StringBuilder sb = new StringBuilder();%0A        final MarkerCluster cluster = event.getMarkerCluster();%0A        @SuppressWarnings(%22unchecked%22)%0A        final List%3CNodeMarker%3E markers = (List%3CNodeMarker%3E)cluster.getAllChildMarkers();%0A        VConsole.log(%22Clicked, processing %22 + markers.size() + %22 markers.%22);%0A        Collections.sort(markers, new NodeMarkerComparator()
 );%0A%0A

move comparator to an inner class, more null-checking