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

import java.io.Writer;
import java.io.IOException;
import java.util.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;

public class MenuCommunity implements ComponentRender, ColorSelectors
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String image_url;                       // path to actual image
  private boolean image_url_needs_fixup = false;  // do we need to fix image path up?
  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
   *--------------------------------------------------------------------------------
   */

  public MenuCommunity(CommunityContext ctxt)
  {
    try
    { // retrieve the contact info for this puppy
      ContactInfo ci = ctxt.getContactInfo();
      image_url = ci.getPhotoURL();
      if (StringUtil.isStringEmpty(image_url))
      { // just hit the default
	image_url = "sig_other.jpg";
	image_url_needs_fixup = true;

      } // end if

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

    } // end catch

    title = ctxt.getName();
    items_list = ctxt.getCommunityFeaturesList();
    cid = ctxt.getCommunityID();
    show_unjoin = ctxt.canUnjoin();

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentRender
   *--------------------------------------------------------------------------------
   */

  public void renderHere(Writer out, RenderData rdat) throws IOException
  {
    String hilite = "<FONT COLOR=\"" + rdat.getStdColor(LEFT_LINK) + "\">";
    if (image_url!=null)
    { // display the image by URL
      if (image_url_needs_fixup)
      { // fix up the URL...
	image_url = rdat.getFullImagePath(image_url);
	image_url_needs_fixup = false;

      } // end if

      out.write("<DIV ALIGN=\"LEFT\"><IMG SRC=\"" + image_url + "\" WIDTH=110 HEIGHT=65 BORDER=0></DIV>\n");

    } // end if

    // display the title
    out.write("<B>" + StringUtil.encodeHTML(title) + "</B>");

    // display the menu items
    Iterator it = items_list.iterator();
    String cparm = "sig=" + cid;
    while (it.hasNext())
    { // display each menu item in turn
      CommunityFeature ftr = (CommunityFeature)(it.next());
      out.write("<BR>\n<A CLASS=\"lbar\" HREF=\""
		+ rdat.getEncodedServletPath(ftr.getApplet() + "?" + cparm) + "\">" + hilite
		+ StringUtil.encodeHTML(ftr.getName()) + "</FONT></A>\n");

    } // end while

    if (show_unjoin)
      out.write("<P>\n<A CLASS=\"lbar\" HREF=\""
		+ rdat.getEncodedServletPath("sigops?cmd=U&" + cparm) + "\">"
		+ hilite + "Unjoin</FONT></A>\n");

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

  } // end renderHere
  
  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public int getID()
  {
    return cid;

  } // end getID

} // end class MenuCommunity
