/*
 * 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.Namespaces;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.security.SecurityReferenceMonitor;
import com.silverwrist.venice.frame.FramedContent;
import com.silverwrist.venice.iface.*;

class StandardMenuRendering implements MenuRenderObject, SelfRenderable, FramedContent
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private MenuDefinition m_menudef;
  private boolean[] m_showitem;
  private HashMap m_local_vars;
  private ContentBlock m_bullet = null;
  private int m_selected_index = -1;

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

  StandardMenuRendering(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, boolean selected)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    for (int i=0; i<item.getIndentLevel(); i++)
      wr.write("&nbsp;&nbsp;&nbsp;&nbsp;");
    control.renderSubObject(m_bullet);
    wr.write("&nbsp;");
    if (selected)
      wr.write("<span class=\"selectedItem\">");
    else 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(">");

    } // 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 (selected)
      wr.write("</span>");
    else if (item.isEnabled())
      wr.write("</a>");
    else
      wr.write("</span>");
    wr.write("<br />\n");

  } // end renderTextItem

  private final void renderHeaderItem(TextRenderControl control, MenuItemDefinition item, boolean selected)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    for (int i=0; i<item.getIndentLevel(); i++)
      wr.write("&nbsp;&nbsp;&nbsp;&nbsp;");
    wr.write("<span style=\"font-weight: bold; text-decoration: underline;\">");
    if (selected)
      wr.write("<span class=\"selectedItem\">");
    else 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(">");

    } // 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 (selected)
      wr.write("</span>");
    else if (item.isEnabled())
      wr.write("</a>");
    else
      wr.write("</span>");
    wr.write("</span><br />\n");

  } // end renderHeaderItem

  private final void renderImageItem(TextRenderControl control, MenuItemDefinition item, boolean selected)
      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;&nbsp;&nbsp;&nbsp;");
    if (selected)
      wr.write("<span class=\"selectedItem\">");
    else 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(">");

    } // 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 (selected)
      wr.write("</span>");
    else 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 int getItemCount()
  {
    return m_menudef.getItemCount();

  } // end getItemCount

  public int getItemContainingLinkText(String text)
  {
    return m_menudef.getItemContainingLinkText(text);

  } // end getItemContainingLinkText

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

  } // end setVariable

  public void setSelectedIndex(int index)
  {
    m_selected_index = index;

  } // end setSelectedIndex

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

  public void render(SelfRenderControl control) throws IOException, RenderingException
  {
    ObjectProvider op = (ObjectProvider)(control.queryService(ObjectProvider.class));
    ServiceProvider tmp_sp = (ServiceProvider)(op.getObject(Namespaces.DYNAMO_OBJECT_NAMESPACE,
							    "venice-content"));
    ContentBlockProvider cblkp = (ContentBlockProvider)(tmp_sp.queryService(ContentBlockProvider.class));
    ContentBlock header = null;
    try
    { // get a header block
      header = cblkp.getContentBlock("content.header");
      if (m_bullet==null)
	m_bullet = cblkp.getContentBlock("std.bullet");

    } // end try
    catch (DatabaseException e)
    { // header block failed
      throw new RenderingException(e);

    } // end catch

    // set the parameters for the content header
    header.setContentParameter("title",StringUtils.replaceAllVariables(m_menudef.getTitle(),m_local_vars));
    String s = m_menudef.getSubtitle();
    if (s!=null)
      header.setContentParameter("subtitle",StringUtils.replaceAllVariables(s,m_local_vars));

    // render the header
    TextRenderControl tctl = control.getTextRender();
    tctl.renderSubObject(header);

    PrintWriter wr = tctl.getWriter();
    wr.write("<div class=\"content\">\n");

    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
	s = item.getItemType();
	if (s.equalsIgnoreCase("TEXT"))
	  renderTextItem(tctl,item,(m_selected_index==i));
	else if (s.equalsIgnoreCase("HEADER"))
	  renderHeaderItem(tctl,item,(m_selected_index==i));
	else if (s.equalsIgnoreCase("IMAGE"))
	  renderImageItem(tctl,item,(m_selected_index==i));
	else if (s.equalsIgnoreCase("SEPARATOR"))
	  renderSeparatorItem(tctl,item);

      } // end if

    } // end for

    wr.write("</div>\n");

  } // end render

  /*--------------------------------------------------------------------------------
   * Implementations from interface FramedContent
   *--------------------------------------------------------------------------------
   */

  public String getMenuSelector()
  {
    return null;

  } // end getMenuSelector

  public String getPageTitle()
  {
    return m_menudef.getTitle();

  } // end getPageTitle

  public String getPageQID()
  {
    return null;

  } // end getPageQID

} // end class StandardMenuRendering
