package org.openqa.grid.selenium.utils;

import java.util.Map;

import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.internal.TestSession;
import org.openqa.grid.internal.TestSlot;
import org.openqa.grid.internal.utils.HtmlRenderer;
import org.openqa.grid.selenium.proxy.WebRemoteProxy;
import org.openqa.grid.web.utils.BrowserNameUtils;
import org.openqa.selenium.remote.DesiredCapabilities;

public class WebProxyHtmlRenderer implements HtmlRenderer {

	private RemoteProxy proxy;

	@SuppressWarnings("unused")
	private WebProxyHtmlRenderer() {
	}

	public WebProxyHtmlRenderer(RemoteProxy proxy) {
		this.proxy = proxy;
	}

	public String renderSummary() {
		StringBuilder builder = new StringBuilder();
		builder.append("<fieldset>");
		builder.append("<legend>").append(proxy.getClass().getSimpleName()).append("</legend>");
		builder.append("listening on ").append(proxy.getRemoteURL());
		if (((WebRemoteProxy) proxy).isDown()) {
			builder.append("(cannot be reached at the moment)");
		}
		builder.append("<br>");
		if (proxy.getTimeOut() > 0) {
			int inSec = proxy.getTimeOut() / 1000;
			builder.append("test session time out after ").append(inSec).append(" sec.<br>");
		}

		builder.append("Supports up to <b>").append(proxy.getMaxNumberOfConcurrentTestSessions()).append("</b> concurrent tests from : </u><br>");

		builder.append("");
		for (TestSlot slot : proxy.getTestSlots()) {
			TestSession session = slot.getSession();
			
			String icon = getIcon(slot.getCapabilities());
			if (icon != null) {
				builder.append("<img ");
				builder.append("src='").append(icon).append("' ");
			}else {
				builder.append("<a href='#' ");
			}

			if (session != null) {
				builder.append(" class='busy' ");
				builder.append(" title='").append(session.get("lastCommand")).append("' ");
			} else {
				builder.append(" title='").append(slot.getCapabilities()).append("' ");
			}
			
			if (icon != null) {
				builder.append("/>");
			}else {
				builder.append(">");
				builder.append(slot.getCapabilities().get("browserName"));
				builder.append("</a>");
			}
			
		}
		builder.append("</fieldset>");

		return builder.toString();
	}

	private String getIcon(Map<String, Object> capabilities) {
		return BrowserNameUtils.getConsoleIconPath(new DesiredCapabilities(capabilities),proxy.getRegistry());
	}

}

@@ -32,24 +32,155 @@
 m.utils;%0D%0A%0D%0A
+import java.net.InetAddress;%0D%0Aimport java.net.MalformedURLException;%0D%0Aimport java.net.URL;%0D%0Aimport java.net.UnknownHostException;%0D%0A
 import java.
@@ -993,26 +993,32 @@
 legend%3E%22);%0D%0A
-%09%09
+        
 builder.appe
@@ -1064,24 +1064,26 @@
 oteURL());%0D%0A
+%0D%0A
 %09%09if (((WebR
@@ -1197,16 +1197,18 @@
 end(%22%3Cbr
+ /
 %3E%22);%0D%0A%09%09
@@ -1361,16 +1361,18 @@
  sec.%3Cbr
+ /
 %3E%22);%0D%0A%09%09
@@ -1506,18 +1506,15 @@
 from
- 
 : %3C
-/u%3E%3C
 br
+ /
 %3E%22);

Properly form the HTML output for console display.