/*
 * 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 InlineMenuRendering implements MenuRenderObject, SelfRenderable
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

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

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

  InlineMenuRendering(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();
    wr.write("<li");
    if (selected)
      wr.write(" class=\"down\"");
    wr.write('>');
    if (selected)
      wr.write("<b>");
    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("</b>");
    else if (item.isEnabled())
      wr.write("</a>");
    else
      wr.write("</span>");
    wr.write("</li>\n");

  } // end renderTextItem

  private final void renderHeaderItem(TextRenderControl control, MenuItemDefinition item, boolean selected)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<li");
    if (selected)
      wr.write(" class=\"down\"");
    wr.write("><b>");
    if (!selected)
    { // write the sequence beginning
      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;\">");

    } // end if

    wr.write(StringUtils.encodeHTML(StringUtils.replaceAllVariables(item.getText(),m_local_vars)));
    if (!selected)
    { // write the sequence end
      if (item.isEnabled())
	wr.write("</a>");
      else
	wr.write("</span>");

    } // end if

    wr.write("</b></li>\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();
    wr.write("<li");
    if (selected)
      wr.write(" class=\"down\"");
    wr.write('>');
    if (!selected)
    { // write the opening sequence
      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;\">");

    } // end if

    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)
    { // write the closing sequence
      if (item.isEnabled())
	wr.write("</a>");
      else
	wr.write("</span>");

    } // end if

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

  } // end renderImageItem

  private final void renderSeparatorItem(TextRenderControl control, MenuItemDefinition item)
      throws IOException, RenderingException
  {
    PrintWriter wr = control.getWriter();
    wr.write("<li class=\"separator\">&nbsp;&nbsp;</li>");

  } // 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
  {
    TextRenderControl tctrl = control.getTextRender();
    PrintWriter wr = tctrl.getWriter();
    wr.write("<div class=\"imenu\"><ul>\n");
    if (StringUtils.isNotEmpty(m_menudef.getTitle()))
    { // write the title and subtitle
      wr.write("<li class=\"separator\"><b>"
	       + StringUtils.encodeHTML(StringUtils.replaceAllVariables(m_menudef.getTitle(),m_local_vars)) + "</b>");
      if (m_menudef.getSubtitle()!=null)
	wr.write("&nbsp;"
		 + StringUtils.encodeHTML(StringUtils.replaceAllVariables(m_menudef.getSubtitle(),m_local_vars)));
      wr.write(":</li>\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("MARKER"))
	  continue;  // ignore markers
	if (s.equalsIgnoreCase("TEXT"))
	  renderTextItem(tctrl,item,(i==m_selected_index));
	else if (s.equalsIgnoreCase("HEADER"))
	  renderHeaderItem(tctrl,item,(i==m_selected_index));
	else if (s.equalsIgnoreCase("IMAGE"))
	  renderImageItem(tctrl,item,(i==m_selected_index));
	else if (s.equalsIgnoreCase("SEPARATOR"))
	  renderSeparatorItem(tctrl,item);

      } // end if

    } // end for

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

  } // end render

} // end class InlineMenuRendering
