/*
 * 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) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.menus;

import java.io.IOException;
import java.io.Writer;
import java.util.*;
import org.apache.log4j.*;
import org.w3c.dom.*;
import com.silverwrist.util.DOMElementHelper;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.config.RootConfig;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.util.XMLLoader;

public class Menu implements MenuComponent
{
  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  private String identifier;
  private List menu_items;
  private String title = null;
  private String subtitle = null;

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

  public Menu(Element elt, RootConfig rconf, String identifier) throws ConfigException
  {
    XMLLoader loader = XMLLoader.get();

    loader.configVerifyNodeName(elt,"menudef");  // verify the name of the root node

    ArrayList tmp_items = new ArrayList();
    NodeList nl = elt.getChildNodes();
    for (int i=0; i<nl.getLength(); i++)
    { // loop through to find node items
      Node n = nl.item(i);
      if (n.getNodeType()==Node.ELEMENT_NODE)
      { // figure out what kind of element this is...
	if (n.getNodeName().equals("title"))
	{ // save off the title
	  DOMElementHelper h = new DOMElementHelper((Element)n);
	  title = h.getElementText();

	} // end if (title specified)
	else if (n.getNodeName().equals("subtitle"))
	{ // save off the title
	  DOMElementHelper h = new DOMElementHelper((Element)n);
	  subtitle = h.getElementText();

	} // end else if (subtitle specified)
	else if (n.getNodeName().equals("header"))
	  tmp_items.add(new HeaderItem((Element)n));
	else if (n.getNodeName().equals("separator"))
	  tmp_items.add(SeparatorItem.getBlank());
	else if (n.getNodeName().equals("text"))
	{ // add a text item
	  DOMElementHelper h = new DOMElementHelper((Element)n);
	  tmp_items.add(new SeparatorItem(new TextItem(h.getElementText())));

	} // end else if
	else if (n.getNodeName().equals("image"))
	  tmp_items.add(new SeparatorItem(new ImageItem((Element)n)));
	else if (n.getNodeName().equals("link"))
	  tmp_items.add(new LinkItem((Element)n,rconf));
	else  // unknown menu item
	  throw new ConfigException("unknown element <" + n.getNodeName() + "/> inside <menudef/>",
				    (Element)n);

      } // end if (element found)
      // else just ignore it

    } // end for

    if (tmp_items.isEmpty())
      menu_items = Collections.EMPTY_LIST;
    else
      menu_items = Collections.unmodifiableList(tmp_items);

    this.identifier = identifier;

  } // end constructor

  public Menu(Menu other, Map vars)
  {
    this.identifier = other.identifier;
    this.title = StringUtil.replaceAllVariables(other.title,vars);
    this.subtitle = StringUtil.replaceAllVariables(other.subtitle,vars);

    if (other.menu_items.isEmpty())
      this.menu_items = Collections.EMPTY_LIST;
    else
    { // copy the menu items, 
      ArrayList tmp_items = new ArrayList(other.menu_items.size());
      Iterator it = other.menu_items.iterator();
      while (it.hasNext())
      { // copy the menu items
	MenuComponent mc = (MenuComponent)(it.next());
	if (mc instanceof LinkItem)
	  tmp_items.add(new LinkItem((LinkItem)mc,vars));
	else
	  tmp_items.add(mc);

      } // end while

      menu_items = Collections.unmodifiableList(tmp_items);

    } // end else

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface MenuComponent
   *--------------------------------------------------------------------------------
   */

  public void render(RequestOutput out, Writer wr) throws IOException
  {
    this.render(out,wr,Collections.EMPTY_SET);

  } // end render

  public void render(RequestOutput out, Writer wr, Set defines) throws IOException
  {
    HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));

    if (title!=null)
    { // write the header
      if (wr==null)
	out.writeContentHeader(title,subtitle);
      else
	out.writeContentHeader(wr,title,subtitle);

    } // end if

    if (wr!=null)
    { // write using specified writer
      wr.write(html.getFontTag(html.CONTENT_FOREGROUND,"content") + "\n");
      wr.flush();

    } // end if
    else
      out.write(html.getFontTag(html.CONTENT_FOREGROUND,"content") + "\n");

    Iterator it = menu_items.iterator();
    while (it.hasNext())
    { // render each menu item in turn
      MenuComponent mc = (MenuComponent)(it.next());
      mc.render(out,wr,defines);

    } // end while

    if (wr!=null)
    { // write using specified writer
      wr.write("</FONT>\n");
      wr.flush();

    } // end if
    else
      out.write("</FONT>\n");

  } // end render

  public void renderOnLeft(RequestOutput out, Writer wr) throws IOException
  {
    this.renderOnLeft(out,wr,Collections.EMPTY_SET);

  } // end renderOnLeft

  public void renderOnLeft(RequestOutput out, Writer wr, Set defines) throws IOException
  {
    if (wr==null)
      wr = out.getWriter();
    if (title!=null)
    { // write the title and subtitle
      wr.write("<B>" + StringUtil.encodeHTML(title) + "</B><BR>\n");
      if (subtitle!=null)
	wr.write(StringUtil.encodeHTML(subtitle) + "<BR>\n");

    } // end if

    Iterator it = menu_items.iterator();
    while (it.hasNext())
    { // render each menu item in turn
      MenuComponent mc = (MenuComponent)(it.next());
      mc.renderOnLeft(out,wr,defines);

    } // end while

  } // end renderOnLeft

  public String getTitle(RequestOutput out)
  {
    return title;

  } // end getTitle

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final String getIdentifier()
  {
    return identifier;

  } // end getIdentifier

  public final String getTitle()
  {
    return title;

  } // end getTitle

} // end class Menu
