package creamtec.webcream.examples;

import java.awt.*;
import javax.swing.JDialog;
import creamtec.webcream.core.*;
import creamtec.core.TraceMgr;

/**
 * Demonstrates the usage of AgentRequestFilter interface to intercept standard WebCream
 * "resize" action and to modify the generated HTML pages.
 */
public class AgentRequestFilterExample
    implements AgentRequestFilter {

    public AgentRequestFilterExample() {
        TraceMgr.trace(this, "Filter loaded");
    }

    /**
     * Overridden to intercept "resize" action.
     * @param agent ClientAgent
     * @return HttpResponseData
     */
    public HttpResponseData preProcessRequest(ClientAgent agent) {
        TraceMgr.trace(this, "preProcessRequest");
        try {
            if ("About WebCream".equals(agent.getUIManager().getWindowInFocusTitle())) {
                // Only apply post processing to the About dialog
                String action = agent.getRequestData().getAction();
                if (action != null && action.startsWith("resize:")) {
                    // Override the processing of resize with our own cool page
                    TraceMgr.trace(this, "Intercepted resize of About dialog");
                    Window aboutWindow = agent.getUIManager().getWindowInFocus();
                    aboutWindow.setSize(300, 300);
                    if (aboutWindow instanceof JDialog) {
                        JDialog dlg = (JDialog) aboutWindow;
                        dlg.setTitle("Filtered Resize");
                    }
                    return agent.renderCurrentWindow(agent.getRequestData());
                }
            }
        }
        catch (Exception x) {
            TraceMgr.trace(this, "Internal error", x);
        }
        return null; // Let the processing continue
    }


    /**
     * Overridden to modify the generated HTML page by adding the user IP address.
     * @param agent ClientAgent
     * @param responseData HttpResponseData
     */
    public void postProcessRequest(ClientAgent agent, HttpResponseData responseData) {
        TraceMgr.trace(this, "postProcessRequest");

        try {
            if ("About WebCream".equals(agent.getUIManager().getWindowInFocusTitle())) {
                String page = responseData.getHtmlPage();
                String ipAddress = (String) agent.getRequestData().getParams().get("custom.ip_address");
                StringBuffer buf = new StringBuffer(page.length() + 100);
                int endOfBodyIndex = page.indexOf("</body>");
                buf.append(page.substring(0, endOfBodyIndex));
                buf.append("<p><i>AgentRequestFilter</i>: IP address is ");
                buf.append(ipAddress);
                buf.append("</p>\r\n");
                buf.append(page.substring(endOfBodyIndex));
                responseData.setHtmlPage(buf.toString());
            }
        }
        catch (Exception x) {
            TraceMgr.trace(this, "Failed to post-process the request", x);
        }
    }

    /**
     * Called after the controls have been updated with the data but before the action
     * is emulated. If there is no action to be emulated (for instance, if "update" was
     * submitted, this method will not be called.
     * @param agent client agent
     * @param data contains the information about the action to be emulated
     * @return true to emulate the action, false to not emulate
     */
    public boolean preEmulateAction(ClientAgent agent, ActionData data) {
        TraceMgr.trace(this, "preEmulateAction");
        return true; // Let the processing continue
    }

    /**
     * Called after the action emulation is complete. (even if preEmulateAction returned false).
     * @param agent client agent
     * @param data contains the information about the action to be emulated
     */
    public void postEmulateAction(ClientAgent agent, ActionData data) {
        TraceMgr.trace(this, "postEmulateAction");
    }

    /**
     * Called before the given window is rendered as HTML page
     * @param agent client agent
     * @param window Window to be rendered
     */
    public void preRenderWindow(ClientAgent agent, Window window) {
        TraceMgr.trace(this, "preRenderWindow");
    }

    /**
     * Called after the given window is rendered as HTML page
     * @param agent client agent
     * @param window Window that was rendered
     * @param htmlCode The HTML code for the page produced by WebCream engine. Modifications
     * to the HTMLPage object associated with the client agent will not be reflected in the
     * generated HTML code
     * @return HTML code of the page that will be returned to the browser. The filter has
     * to return the HTML code whether it modified it or not, or
     */
    public String postRenderWindow(ClientAgent agent, Window window, String htmlCode) {
        TraceMgr.trace(this, "postRenderWindow");
        return htmlCode;
    }

}
