package org.jfox.framework.management.http;

import java.util.Properties;

import org.jfox.framework.ComponentName;
import org.jfox.framework.Registry;

/**
 * @author <a href="mailto:yangyong@ufsoft.com.cn">Young Yang</a>
 */

public class IndexCommand implements Command {

    public String doGet(Registry registry, Properties queryMap) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append(PageUtil.head("agent view"));
        sb.append(PageUtil.banner("Agent View"));
        sb.append(table1(registry));

        sb.append(table2(registry));
        sb.append(PageUtil.foot());
        return sb.toString();
    }

    public static String table1(Registry registry) {
        StringBuffer sb = new StringBuffer();
        sb.append("<table width=\"760\" align=\"center\">");
        sb.append("<tr>");

        sb.append("<td>found <b>");
        sb.append(registry.countComponents());
        sb.append("</b> components with <b>")
                .append(registry.countComponentTypes())
                .append("</b> types")
                .append(" . </td>");
        sb.append("</tr>");
        sb.append("</table>");
        return sb.toString();
    }

    public static String table2(Registry registry) {
        ComponentName[] names = registry.getAllComponentNames();
        StringBuffer sb = new StringBuffer();
        sb.append("<table width=\"760\" align=\"center\">\n" +
                "  <tr>\n" +
                "    <td>\n" +
                "      <ul>\n");
        for(int i = 0; i < names.length; i++) {
            ComponentName name = names[i];
            sb.append("<li><a href=\"?action=ViewComponent&compname=")
                    .append(name.toString()).append("\" >")
                    .append(name.toString())
                    .append("</a> &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?action=UnregisterComponent&compname=")
                    .append(name.toString())
                    .append("\"><font size=\"-2\" face=\"verdana\" color=\"#666666\">Unregister</font></a>\n");
        }
        sb.append("        <p>\n" +
                "      </ul>\n" +
                "    </td>\n" +
                "  </tr>\n" +
                "</table>");
        return sb.toString();
    }
}

