/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.patterns.JAVA.CLASS.JSP;

/**
 *  This class provides basic HTML generation methods.
 *  @author TogetherSoft
 */
final class HTML {
    /** table colour for even rows */
    public final static String EVEN_ROW_COLOR = "#FFF8EE";

    /** table colour for odd rows */
    public final static String ODD_ROW_COLOR  = "#CCCCCC";

    /** page background colour */
    public final static String BACKGROUND_COLOR="#FFFFF0"; 

    /** font color */
    public final static String FONT_COLOR = "#000000";
    
    // Default constructor
    public HTML() {
        // initialize the styles here
    }

    public String makeHead(String comment, String title) {
        StringBuffer head = new StringBuffer("<html>");
        appendln(head);
        head.append("<!--\n");
        head.append(comment);
        appendln(head);
        head.append("-->\n");
        head.append("<head><title>");
        head.append(title);
        head.append("</title>");
        head.append("<body ");
        // append the basic style
        head.append("style=\""+ CSS + "\">");
        appendln(head);

        return head.toString();
    }

    public String makeTail(String include) {
        StringBuffer tail = new StringBuffer();
        if(include != null && include.length() > 0) {
            tail.append("<jsp:include page=\"" + include + "\" />");
        }
        tail.append("</body></html>");
        appendln(tail);

        return tail.toString();
    }

    public String makeTable(String tableContent, String border) {
        StringBuffer table = new StringBuffer("<table border=\"");
        if(border == null) {
            table.append("0");
        } else {
            table.append(border);
        }
        table.append("\">");
        table.append(tableContent);
        table.append("</table>");

        return table.toString();
    }

    // FIXME: altering the color might come out handy
    public String makeTableRow(String tableData, String color) {
        StringBuffer tableRow = new StringBuffer("<tr");

        if(color != null) {
            tableRow.append(" bgcolor=");
            tableRow.append(enquote(color));
            tableRow.append(">\n");
        }

        tableRow.append(tableData);
        tableRow.append("\n</tr>\n");

        return tableRow.toString();
    }

    public String makeTableCell(String tableData) {
        StringBuffer tableCell = new StringBuffer("<td>");
        tableCell.append(tableData);
        tableCell.append("</td>");

        return tableCell.toString();
    }

    /** 
     *  Make html header of level level.
     */
    public String makeHeading(String text, int level) {
        String levelString = "h"+String.valueOf(level);
        StringBuffer heading = new StringBuffer("<"+levelString+">");
        heading.append(text);
        heading.append("</"+levelString+">");

        return heading.toString();
    }

    public String makeBold(String boldText) {
        StringBuffer beBold = new StringBuffer("<b>");
        beBold.append(boldText);
        beBold.append("</b>");
        
        return beBold.toString();
    }

    public String makeItalic(String italicText) {
        StringBuffer beOblique = new StringBuffer("<i>");
        beOblique.append(italicText);
        beOblique.append("</i>");

        return beOblique.toString();
    }

    // ----------------------
    // -- JSP Related Tags --
    // ----------------------

    /** 
     *  Build the <jsp:useBean> tag. Bean name should be fully qualified for the 
     *  server would be able to locate the class.
     *  <jsp:useBean> tag must be closed!
     */
    public String makeUseBean(String beanName, String instanceName, String scope) {
        StringBuffer useBean = new StringBuffer("<jsp:useBean id=");
        useBean.append(enquote(instanceName));
        useBean.append(" ");
        useBean.append("scope=");
        useBean.append(enquote(scope));
        useBean.append(" ");
        useBean.append("class=");
        useBean.append(enquote(beanName));
        useBean.append(" />");

        return useBean.toString();
    }

    public String makeUseBeanClosing() {
        return "</jsp:useBean>";
    }

    /**
     *  Retrieve the property with name propertyName from the bean instantiated
     *  as instanceName. (Puts the tag, doesn't retrieve ;-)
     */
    public String makeGetProperty(String instanceName, String propertyName) {
        StringBuffer access = new StringBuffer("<jsp:getProperty name=");
        access.append(enquote(instanceName));
        access.append(" property=");
        access.append(enquote(propertyName));
        access.append(" />");

        return access.toString();
    }

    /** 
     *  Make an include page directive (without parsing)
     */
    public String makeIncludePage(String url) {
        StringBuffer includePage = new StringBuffer("<jsp:include page=");
        includePage.append(enquote(url));
        includePage.append(" flush=\"true\"/>");
        
        return includePage.toString();
    }

    private void appendln(StringBuffer sb) {
        sb.append("\n");
    }

    private String enquote(String anything) {
        return "\"" + anything + "\"";
    }

    private String CSS = "font-size:12pt; background-color:" + BACKGROUND_COLOR + "; color:" + FONT_COLOR;
}
