/*
 * 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.Writer;
import java.io.IOException;
import java.util.*;
import com.silverwrist.util.*;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;
import com.silverwrist.venice.ui.*;
import com.silverwrist.venice.ui.helpers.HTMLRendering;

public class CommunityMenu implements MenuComponent
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String image_url;                       // path to actual image
  private String title;                           // title for menu
  private List items_list;                        // list of menu items
  private int cid;                                // community ID
  private boolean show_unjoin;                    // show the "Unjoin" menu choice?

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

  CommunityMenu(CommunityContext ctxt, List items)
  {
    try
    { // retrieve the contact info for this puppy
      ContactInfo ci = ctxt.getContactInfo();
      image_url = ci.getPhotoURL();

    } // end try
    catch (DataException e)
    { // if we couldn't get the contact info, screw it
      image_url = null;

    } // end catch

    title = StringUtil.encodeHTML(ctxt.getName());
    items_list = items;
    cid = ctxt.getCommunityID();
    show_unjoin = ctxt.canUnjoin();

  } // 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 (wr==null)
      wr = out.getWriter();

    wr.write("<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0><TR VALIGN=BOTTOM>\n<TD ALIGN=LEFT WIDTH=110>"
	     + html.getCommunityLogoTag(image_url) + "</TD>\n<TD ALIGN=LEFT>\n");

    // display the title
    out.writeContentHeader(wr,title,null);
    wr.write("</TD>\n</TR></TABLE><BR>\n");

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

    } // end while

    if (show_unjoin)  // display the "Unjoin" link
      wr.write("<P><IMG SRC=\"" + html.getImagePath("purple-ball.gif")
	       + "\" ALT=\"*\" WIDTH=14 HEIGHT=14 BORDER=0><A HREF=\""
	       + html.formatURL("comm/unjoin.js.vs?cc=" + cid,html.SERVLET) + "\">Unjoin</A><BR>\n");

    wr.write("\n");  // all done...

  } // 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
  {
    HTMLRendering html = (HTMLRendering)(out.queryService(HTMLRendering.class));

    if (wr==null)
      wr = out.getWriter();
    String hilite = html.getFontTag(html.LEFT_LINK,null);

    // display the image URL and title
    wr.write("<DIV ALIGN=\"LEFT\">" + html.getCommunityLogoTag(image_url) + "</DIV>\n<B>"
	     + StringUtil.encodeHTML(title) + "</B><BR>\n");

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

    } // end while

    if (show_unjoin)
      wr.write("<BR>\n<A CLASS=\"lbar\" HREF=\"" + html.formatURL("comm/unjoin.js.vs?cc=" + cid,html.SERVLET)
	       + "\">" + hilite	+ "Unjoin</FONT></A>\n");

    wr.write("\n");  // all done...

  } // end renderOnLeft

  public String getTitle(RequestOutput out)
  {
    return title;

  } // end getTitle

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

  public final int getID()
  {
    return cid;

  } // end getID

} // end class CommunityMenu
