/*******************************************************************************
 * Copyright (c), 2001, 2002 N2 Broadband, Inc.  All Rights Reserved.
 *
 * This module contains unpublished, confidential, proprietary
 * material.  The use and dissemination of this material are
 * governed by a license.  The above copyright notice does not
 * evidence any actual or intended publication of this material.
 *
 * Author:  Drake H. Henderson
 * Created:  11-12-01
 *
 ******************************************************************************/
package com.n2bb.web.util;

import java.io.InputStream;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.*;

/**
 * Application switcher popup builder.<p>
 * Loads all XML files in a given directory and parses them for menu items,
 * which are added to a menu.
 */
public class AppPopup {

    private static final String POPUP_NAME = "system links";

    private static Log n2bbLog = LogFactory.getLog(AppPopup.class);

    public static String buildPopup(ServletContext servletContext, String popupDir) {
        try {
            String popup = getPopupStart();
            Set popupPaths = servletContext.getResourcePaths(popupDir);
            for (Iterator iterator = popupPaths.iterator(); iterator.hasNext();) {
                String path = (String) iterator.next();
                if (path.endsWith(".xml")) {
                    popup += getPopupItems(servletContext, path);
                }
            }
            return popup + "\n</div>\n";
        }
        catch (Exception e) {
            n2bbLog.error(e.getMessage(), e);
            return POPUP_NAME;
        }
    }

    private static String getPopupItems(ServletContext servletContext, String path) {
        try {
            InputStream is = servletContext.getResourceAsStream(path);
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
            Document doc = domBuilder.parse(is);
            return getPopupItems(doc);
        }
        catch (Exception e) {
            n2bbLog.error("", e);
            return "";
        }
    }


    private static String getPopupStart() {
        return "<a href=\"javascript:void(0)\" onClick=\"javascript:showPop(event)\" " +
                "target=\"_top\">" + POPUP_NAME + "</a>\n" +
                "<div id=\"apps\" class=\"appswitcher-hide\" onMouseOver=\"holdPop=true\" " +
                "onMouseOut=\"holdPop=false;showPop()\">\n";
    }

    private static String getPopupItems(Document doc) {
        Node appPopupNode = doc.getFirstChild();
        NodeList list = appPopupNode.getChildNodes();

        String popup = "";

        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);

            // if statement used to filter text nodes
            if (node.getNodeName().equals("menuItem")) {

                String display = ((Element) node).getAttribute("display");
                String path = ((Element) node).getAttribute("path");
                if (display != null && !display.trim().equals("") &&
                        path != null && !path.trim().equals("")) {
                    popup += "<a href=\"" + path +
                            "\" onMouseOver=\"holdPop=true\" target=\"_top\" " +
                            "class=\"appswitcher-link\">" + display + "</a><br>";
                }
            }
        }
        return popup;
    }

}
