/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.menu;

import java.io.*;
import java.util.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.security.SecurityReferenceMonitor;
import com.silverwrist.venice.iface.*;

class LeftMenuRendering implements MenuRenderObject, SelfRenderable
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private MenuDefinition m_menudef;
  private boolean[] m_showitem;
  private HashMap m_local_vars;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  LeftMenuRendering(DynamoUser caller, MenuDefinition def, SecurityReferenceMonitor srm, int[] acl_ids)
      throws DatabaseException
  {
    m_menudef = def;
    int count = def.getItemCount();
    m_showitem = new boolean[count];
    for (int i=0; i<count; i++)
    { // get the "show item" flags
      MenuItemDefinition item = def.getItem(i);
      m_showitem[i] = item.itemAppears(caller,srm,acl_ids);

    } // end for

    m_local_vars = new HashMap(def.getVarDefaults());

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private final void renderTextItem(TextRenderControl control, MenuItemDefinition item)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    for (int i=0; i<item.getIndentLevel(); i++)
      wr.write("&nbsp;");
    if (item.isEnabled() && (item.getLink()!=null))
    { // write the link
      URLRewriter rewriter = (URLRewriter)(control.queryService(URLRewriter.class));
      wr.write("<a href=\""
	       + rewriter.rewriteURL(item.getLinkType(),
				     StringUtils.replaceAllVariables(item.getLink(),m_local_vars)) + "\"");
      String s = item.getTarget();
      if (s!=null)
	wr.write(" target=\"" + s + "\"");
      s = item.getTitle();
      if (s!=null)
	wr.write(" title=\"" + s + "\"");
      s = item.getOnClick();
      if (s!=null)
	wr.write(" onClick=\"" + s + "\"");
      wr.write("class=\"frameleft\" >");

    } // end if
    else  // just disable the text
      wr.write("<span style=\"color: silver;\">");
    wr.write(StringUtils.encodeHTML(StringUtils.replaceAllVariables(item.getText(),m_local_vars)));
    if (item.isEnabled())
      wr.write("</a>");
    else
      wr.write("</span>");
    wr.write("<br />\n");

  } // end renderTextItem

  private final void renderHeaderItem(TextRenderControl control, MenuItemDefinition item)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    for (int i=0; i<item.getIndentLevel(); i++)
      wr.write("&nbsp;");
    wr.write("<b>");
    if (item.isEnabled() && (item.getLink()!=null))
    { // write the link
      URLRewriter rewriter = (URLRewriter)(control.queryService(URLRewriter.class));
      wr.write("<a href=\""
	       + rewriter.rewriteURL(item.getLinkType(),
				     StringUtils.replaceAllVariables(item.getLink(),m_local_vars)) + "\"");
      String s = item.getTarget();
      if (s!=null)
	wr.write(" target=\"" + s + "\"");
      s = item.getTitle();
      if (s!=null)
	wr.write(" title=\"" + s + "\"");
      s = item.getOnClick();
      if (s!=null)
	wr.write(" onClick=\"" + s + "\"");
      wr.write("class=\"frameleft\" >");

    } // end if
    else  // just disable the text
      wr.write("<span style=\"color: silver;\">");
    wr.write(StringUtils.encodeHTML(StringUtils.replaceAllVariables(item.getText(),m_local_vars)));
    if (item.isEnabled())
      wr.write("</a>");
    else
      wr.write("</span>");
    wr.write("</b><br />\n");

  } // end renderHeaderItem

  private final void renderImageItem(TextRenderControl control, MenuItemDefinition item)
      throws IOException, RenderingException
  {
    URLRewriter rewriter = (URLRewriter)(control.queryService(URLRewriter.class));
    String s;
    PrintWriter wr = control.getWriter();
    for (int i=0; i<item.getIndentLevel(); i++)
      wr.write("&nbsp;");
    if (item.isEnabled() && (item.getLink()!=null))
    { // write the link
      wr.write("<a href=\""
	       + rewriter.rewriteURL(item.getLinkType(),
				     StringUtils.replaceAllVariables(item.getLink(),m_local_vars)) + "\"");
      s = item.getTarget();
      if (s!=null)
	wr.write(" target=\"" + s + "\"");
      s = item.getTitle();
      if (s!=null)
	wr.write(" title=\"" + s + "\"");
      s = item.getOnClick();
      if (s!=null)
	wr.write(" onClick=\"" + s + "\"");
      wr.write(" class=\"frameleft\">");

    } // end if
    else  // just disable the text
      wr.write("<span style=\"color: silver;\">");
    String[] data = StringUtils.split(item.getText(),",",4);
    wr.write("<img src=\"" + rewriter.rewriteURL(data[2],StringUtils.replaceAllVariables(data[3],m_local_vars))
	     + "\"");
    s = item.getTitle();
    if (s==null)
      wr.write(" alt=\"\"");
    else
      wr.write(" alt=\"" + s + "\" title=\"" + s + "\"");
    wr.write(" width=\"" + data[0] + "\" height=\"" + data[1] + "\" border=\"0\" />");
    if (item.isEnabled())
      wr.write("</a>");
    else
      wr.write("</span>");
    wr.write("<br />\n");

  } // end renderImageItem

  private final void renderSeparatorItem(TextRenderControl control, MenuItemDefinition item)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<br />\n");

  } // end renderSeparatorItem

  /*--------------------------------------------------------------------------------
   * Implementations from interface MenuRenderObject
   *--------------------------------------------------------------------------------
   */

  public void setVariable(String name, String value)
  {
    if (m_menudef.isVariable(name))
      m_local_vars.put(name,value);

  } // end setVariable

  /*--------------------------------------------------------------------------------
   * Implementations from interface SelfRenderable
   *--------------------------------------------------------------------------------
   */

  public void render(SelfRenderControl control) throws IOException, RenderingException
  {
    TextRenderControl tctrl = control.getTextRender();
    PrintWriter wr = tctrl.getWriter();
    if (m_menudef.getTitle()!=null)
    { // write the title and subtitle
      wr.write("<b>" + StringUtils.encodeHTML(m_menudef.getTitle()) + "</b><br />\n");
      if (m_menudef.getSubtitle()!=null)
	wr.write(StringUtils.encodeHTML(m_menudef.getSubtitle()) + "<br />\n");

    } // end if

    for (int i=0; i<m_showitem.length; i++)
    { // render each menu item in turn
      MenuItemDefinition item = m_menudef.getItem(i);
      if (m_showitem[i] && item.itemDefined(m_local_vars))
      { // render this item
	String s = item.getItemType();
	if (s.equalsIgnoreCase("TEXT"))
	  renderTextItem(tctrl,item);
	if (s.equalsIgnoreCase("HEADER"))
	  renderHeaderItem(tctrl,item);
	if (s.equalsIgnoreCase("IMAGE"))
	  renderImageItem(tctrl,item);
	if (s.equalsIgnoreCase("SEPARATOR"))
	  renderSeparatorItem(tctrl,item);

      } // end if

    } // end for

  } // end render

} // end class LeftMenuRendering
